今天下午咖啡讲课是真的烦人啊,我都没有学进去,效率是真的低,真烦人!!!
就做了一道数论的题目而且还一直再改错,改了1个多小时,最后发现原来是自己的二分写错了。
mid=(left+right)/2写成mid=(left+mid)/2了;然后之后改错...
周五打了873,一小时过了ABC,创造了新的记录了!!!,排名2400名左右,太棒了!!!
Codeforces873:
A:Problem - A - Codeforces (Unofficial mirror by Menci)
A题还是很简单的,算是签到题目吧,等差数列1到n的前n项和为n*(n+1)/2,因为最后需要被n整除,所以我们直接乘2,即2到2*n的前n项和为n*(n+1)/n=n+1能整除n,所以答案就为每一项乘2即可
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef long long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
void print(__int128 num) {
if(num) {
print(num/10);
putchar(num%10+'0');
}
}
ll ex_gcd(ll a,ll b,ll& x,ll& y){
if(b==0){
x=1;
y=0;
return a;
}
ll d=ex_gcd(b,a%b,y,x);
y=y-a/b*x;
return d;
}
int t;
int main(){
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
printf("%d ",2*i);
}
printf("\n");
}
return 0;
}
B:Problem - B - Codeforces (Unofficial mirror by Menci)
一开始读题的时候我就感觉是gcd,结果还真是,果然,做的题目多了,思路就上来了
,答案为所有交换位置差值的绝对值的gcd,思路还是很好想到的
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef long long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
void print(__int128 num) {
if(num) {
print(num/10);
putchar(num%10+'0');
}
}
ll ex_gcd(ll a,ll b,ll& x,ll& y){
if(b==0){
x=1;
y=0;
return a;
}
ll d=ex_gcd(b,a%b,y,x);
y=y-a/b*x;
return d;
}
int t;
int n;
int a[100005];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int x;
for(int i=1;i<=n;i++){
scanf("%d",&x);
a[x]=i;
}
int ans=0;
for(int i=1;i<=n;i++){//枚举数字
if(a[i]==i)continue;
if(a[i]!=i){
if(ans==0){
ans=abs(a[i]-a[a[i]]);
int now=a[i];
a[i]=i;
a[a[i]]=now;
}else{
ans=__gcd(ans,abs(a[i]-a[a[i]]));
int now=a[i];
a[i]=i;
a[a[i]]=now;
}
}
}
printf("%d\n",ans);
}
return 0;
}
C:Problem - C - Codeforces (Unofficial mirror by Menci)
第一次在1小时之内做出C题,我们需要记录 b中每一个大于b[i]的a数组的个数,因为a和b的范围都很大,用数组开不开,所以我们开采用map
通过样例我们发现对于b数组中的每一个b[i],b[i]的可能的次数为(mp[b[i]]-大于b[i]的个数)
答案即可所以数的乘积
在统计大于b[i]的个数的时候,我们将a和b数组排序,然后我们采用了双指针,一定要注意边界!!!
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef long long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
void print(__int128 num) {
if(num) {
print(num/10);
putchar(num%10+'0');
}
}
ll ex_gcd(ll a,ll b,ll& x,ll& y){
if(b==0){
x=1;
y=0;
return a;
}
ll d=ex_gcd(b,a%b,y,x);
y=y-a/b*x;
return d;
}
ll mod=1e9+7;
ll ans;
ll a[200005];
ll b[200005];
map<ll,ll>mp;
bool cmp(ll a,ll b){
return a>b;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
ans=0;
mp.clear();
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++){
scanf("%lld",&b[i]);
}
sort(b+1,b+1+n,cmp);
int r=1;
for(int i=1;i<=n;i++){
while(a[r]>b[i]&&r<=n){
r++;
}
// if(a[r]<=b[i]||(a[r]>a[i]&&r==n+1)){
mp[b[i]]=r-1;
// }
}
ans=1;
for(int i=1;i<=n;i++){
ans=ans*(mp[b[i]]-i+1)%mod;
}
printf("%lld\n",ans%mod);
}
return 0;
}
Educational Codeforces Round 148 (Rated for Div. 2):
A:Problem - A - Codeforces (Unofficial mirror by Menci)
a题还是很好想的,如果在回文序列的前面有不相同的字符,那么符号条件,否则就不符合条件,但就是写的时候出了问题,我不知道为啥cin>>(s+1),和strlen(s+1)在g++为啥为出现编译错误...
所以只好用string了,我是通过i和i+1是否相同来判断的,其实比较简单的方法用map来统计某个字母是否出现,如果cnt》=2就符合条件
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef long long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
void print(__int128 num) {
if(num) {
print(num/10);
putchar(num%10+'0');
}
}
ll ex_gcd(ll a,ll b,ll& x,ll& y){
if(b==0){
x=1;
y=0;
return a;
}
ll d=ex_gcd(b,a%b,y,x);
y=y-a/b*x;
return d;
}
int t;
string s;
int main(){
scanf("%d",&t);
while(t--){
cin>>s;
s=" "+s;
int cnt=0;
for(int i=1;i<(s.length()-1)/2;i++){
if(s[i]!=s[i+1])cnt++;
}
if(cnt==0){
printf("NO\n");
}else{
printf("YES\n");
}
}
return 0;
}
B:Problem - B - Codeforces (Unofficial mirror by Menci)
b题我用了将近一个多小时,一开始我想直接贪心,但考虑了贪心是局部最优,不是全局最优,但我还是写了,然后没过样例,发现确实不是全局最优,只是局部最优,然后我就想了dp,然后我就开始纠结是线性dp呢还是区间dp,最后我觉得应该是区间dp,然后我发现二维dp不能开这么大的数组
2个1e5就不行了,区间dp样例还没做出来
然后我就突然想到了前缀和,然后想了想确实可以,想到前缀和这题就很简单了
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef long long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
void print(__int128 num) {
if(num) {
print(num/10);
putchar(num%10+'0');
}
}
ll ex_gcd(ll a,ll b,ll& x,ll& y){
if(b==0){
x=1;
y=0;
return a;
}
ll d=ex_gcd(b,a%b,y,x);
y=y-a/b*x;
return d;
}
int t,n,k;
ll a[200005];
ll s[200005];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
ll ans=0;
for(int r=0;r<=k;r++){
int l=(k-r)*2;
ans=max(ans,s[n-r]-s[l]);
}
printf("%lld\n",ans);
}
return 0;
}
C:Problem - C - Codeforces (Unofficial mirror by Menci)
一开始看了看没有思路,就不想做了,然后突然觉悟,自己不能摆烂,自己就研究了一会样例,发现好像跟递增和递减的区间的个数有关,就是区间个数+1,然后就开写了,也写出了,样例也过了,但是tm的数组少开了一个0,然后就错了....
真的烦...
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
using namespace std;
typedef long long ll ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if (ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x<<1) + (x<<3) + (ch^48);
ch = getchar();
}
return x * f;
}
void print(__int128 num) {
if(num) {
print(num/10);
putchar(num%10+'0');
}
}
ll ex_gcd(ll a,ll b,ll& x,ll& y){
if(b==0){
x=1;
y=0;
return a;
}
ll d=ex_gcd(b,a%b,y,x);
y=y-a/b*x;
return d;
}
ll ans;
int t;
int n;
ll a[300005];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int flag=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(i!=1&&a[i]!=a[i-1])flag=1;
}
if(flag==0){
printf("1\n");
continue;
}
ans=1;
int pos=0;//1代表递减,2代表递增
for(int i=1;i<n;i++){
if(pos==0){
if(a[i]>a[i+1]){
pos=1;
ans++;
}
if(a[i]<a[i+1]){
pos=2;
ans++;
}
}else{
if(pos==1){
if(a[i]>=a[i+1]){
continue;
}else{
i--;
pos=0;
}
}else{
if(a[i]<=a[i+1]){
continue;
}else{
i--;
pos=0;
}
}
}
}
printf("%lld\n",ans);
}
return 0;
}