Codeforces水题集合[14/未完待续]

Codeforces Round #371 (Div. 2) A. Meeting of Old Friends |B. Filya and Homework

A. Meeting of Old Friends

 模拟
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
ll l1,r1,l2,r2,k;
int main(int argc, const char * argv[]) {
    cin>>l1>>r1>>l2>>r2>>k;
    ll l=max(l1,l2),r=min(r1,r2);
    ll ans=r-l+1;
    if(k>=l&&k<=r) ans--;
    if(ans<0) cout<<0;
    else cout<<ans;
    return 0;
}
B. Filya and Homework

 YES最多三个数,成等差数列

分情况讨论

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
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*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,a,cnt=0,t[5];
int main(int argc, const char * argv[]) {
    n=read();
    for(int i=1;i<=n;i++){
        a=read();
        int flag=0;
        for(int j=1;j<=cnt;j++)
            if(a==t[j]){flag=1;break;}
        if(cnt==3&&flag==0){cout<<"NO";return 0;}
        if(cnt<3&&flag==0) t[++cnt]=a;
    }
    if(cnt<3){cout<<"YES";return 0;}
    sort(t+1,t+1+3);
    if(t[2]-t[1]==t[3]-t[2]) printf("YES");
    else printf("NO");
    return 0;
}



Codeforces#372(Div.2)716A. Crazy Computer  |716B. Complete the Word[模拟]


A. Crazy Computer
非常水的模拟
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
int n,c,a,ans=0,last=0;
int main(int argc, const char * argv[]) {
    n=read();c=read();
    last=read();ans=1;
    for(int i=2;i<=n;i++){
        a=read();
        if(a-last<=c){last=a;ans++;}
        else {last=a;ans=1;}
    }
    printf("%d",ans);
    return 0;
}

B. Complete the Word 

可以枚举n然后扫26个,也可以用队列糊O(n)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e4+5;
char s[N];
int n,flag=0,lst[30],vis[30],cnt=0,st,ed;

int main(int argc, const char * argv[]) {
    scanf("%s",s+1);
    n=strlen(s+1);
    if(n<26){printf("-1");return 0;}
    for(int i=1;i<=n-25;i++){
        memset(vis,0,sizeof(vis));
        flag=1;cnt=0;
        for(int j=i;j<=i+25;j++)
            if(s[j]!='?'){
                int a=s[j]-'A';
                if(vis[a]){flag=0;break;}
                vis[a]=1;
            }
        if(flag){st=i;ed=i+25;break;}
    }
    if(!flag){printf("-1");return 0;}
    memset(vis,0,sizeof(vis));
    for(int i=st;i<=ed;i++) if(s[i]!='?') vis[s[i]-'A']=1;
    for(int i=0;i<26;i++) if(!vis[i]) lst[++cnt]=i;
    int p=0;
    for(int i=st;i<=ed;i++) if(s[i]=='?') s[i]=lst[++p]+'A';
    for(int i=1;i<=n;i++){
        if(s[i]=='?') printf("A");
        else printf("%c",s[i]);
    }
    return 0;
}



Codeforces#373(Div.2)719A. Vitya in the Countryside |719B. Anatoly and Cockroaches[模拟]

A. Vitya in the Countryside
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Every summer Vitya comes to visit his grandmother in the countryside. This summer, he got a huge wart. Every grandma knows that one should treat warts when the moon goes down. Thus, Vitya has to catch the moment when the moon is down.

Moon cycle lasts 30 days. The size of the visible part of the moon (in Vitya's units) for each day is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, and then cycle repeats, thus after the second 1 again goes 0.

As there is no internet in the countryside, Vitya has been watching the moon for n consecutive days and for each of these days he wrote down the size of the visible part of the moon. Help him find out whether the moon will be up or down next day, or this cannot be determined by the data he has.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 92) — the number of consecutive days Vitya was watching the size of the visible part of the moon. 

The second line contains n integers ai (0 ≤ ai ≤ 15) — Vitya's records.

It's guaranteed that the input data is consistent.

Output

If Vitya can be sure that the size of visible part of the moon on day n + 1 will be less than the size of the visible part on day n, then print "DOWN" at the only line of the output. If he might be sure that the size of the visible part will increase, then print "UP". If it's impossible to determine what exactly will happen with the moon, print -1.

Examples
input
5
3 4 5 6 7
output
UP
input
7
12 13 14 15 14 13 12
output
DOWN
input
1
8
output
-1
Note

In the first sample, the size of the moon on the next day will be equal to 8, thus the answer is "UP".

In the second sample, the size of the moon on the next day will be 11, thus the answer is "DOWN".

In the third sample, there is no way to determine whether the size of the moon on the next day will be 7 or 9, thus the answer is -1.


很水的模拟,注意0的话一定升,15的话一定降,单个也可以

因为这个被hack一次,跟灰哥讨论一会才发现 

//当时写的丑
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=105;
int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
int n,a[N];
int main(){
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    if(n<2){
        if(a[1]==0) printf("UP");
        else if(a[1]==15) printf("DOWN");
        else printf("-1");
        return 0;
    }
    
    int x=a[n-1],y=a[n];
    if(x<y){
        if(y!=15) printf("UP");
        else printf("DOWN");
    }else{
        if(y!=0) printf("DOWN");
        else printf("UP");
    }
    return 0;
}

B. Anatoly and Cockroaches

题意:最少次数使序列alternate

两种情况统计需要次数取最小

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+5,INF=1e9;
int n,cntr=0,cntb=0,ans=INF;
char s[N];

int main(){
    scanf("%d%s",&n,s+1);
    for(int i=1;i<=n;i++){
        if(i%2==1&&s[i]=='r')cntr++;
        if(i%2==0&&s[i]=='b')cntb++;
    }
    ans=max(cntr,cntb);
    cntr=cntb=0;
    for(int i=1;i<=n;i++){
        if(i%2==1&&s[i]=='b')cntb++;
        if(i%2==0&&s[i]=='r')cntr++;
    }
    ans=min(ans,max(cntr,cntb));
    printf("%d",ans);
}



Codeforces#374(Div.2)721A. One-dimensional Japanese Crossword |720B.Password 

 
扫描统计有几个B的连续快及长度,注意最后还要判断一次
//
//  main.cpp
//  a
//
//  Created by Candy on 9/30/16.
//  Copyright © 2016 Candy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=1e2+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x;
}
int n,cnt=0,lst[N],cur=0;
char s[N];
int main(int argc, const char * argv[]) {
    scanf("%d%s",&n,s+1);
    for(int i=1;i<=n;i++){
        if(s[i]=='B') {cur++;}
        if(s[i]=='W') {if(cur) lst[++cnt]=cur;cur=0;}
    }
    if(cur)lst[++cnt]=cur;
    printf("%d
",cnt);
    for(int i=1;i<=cnt;i++) printf("%d ",lst[i]);
    return 0;
}

B.Passwords

按长度排序,看比password短的有多少,统计时间,mn就是这个时间+1

mx还要继续统计和password一样长的时间

注意如果输入password后正好达到block次数,这个不能算

PS:官方推了公式也可以 let's count two variables — cntl (the number of passwords that are shorter than Vanya's Codehorses password) and cntle (the number of passwords that are not longer than Vanya's Codehorses password). Then it's easy to see that in the best case answer will be equal to , and in the worst case it will be .

//
//  main.cpp
//  b
//
//  Created by Candy on 9/30/16.
//  Copyright © 2016 Candy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
using namespace std;
const int N=105;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x;
}
int n,k,mn,mx;
vector<string> v;
vector<string>::iterator it;
string s,p;
bool cmp(string &a,string &b){
    return a.size()<b.size();
}
int main(int argc, const char * argv[]) {
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        cin>>s;
        v.push_back(s);
    }
    cin>>p;
    sort(v.begin(),v.end(),cmp);
    int len=p.size(),tmp=0,equ=0;
    for(it=v.begin();it<v.end();it++){
        if(it->size()<len){
            tmp++;mn++;mx++;if(tmp==k){mn+=5;mx+=5;tmp=0;}
        }
        if(it->size()==len){
            tmp++;mx++;if(tmp==k){mx+=5;tmp=0;}
        }
        if(it->size()>len) break;
    }
    if(tmp==0) mx-=5;
    printf("%d %d",mn+1,mx);
    return 0;
}
 


Codeforces Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) 722A. Broken Clock |720B.Verse Pattern

 

 A.

贪心模拟

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=10;
int n;
char s[N];
int main(int argc, const char * argv[]) {
    scanf("%d%s",&n,s+1);
    if(n==12){
        if(s[1]>'1'){
            if(s[2]=='0') s[1]='1';
            else s[1]='0';
        }else{
            if(s[1]=='1'&&s[2]>'2') s[1]='0';
            if(s[1]=='0'&&s[2]=='0') s[2]='1';
        }
    }else{
        if(s[1]>'2') s[1]='0';
        if(s[1]=='2'&&s[2]>'3') s[1]='0';
    }
    if(s[4]>'5') s[4]='0';
    printf("%s",s+1);
    return 0;
}

B.

就是多少个元音字母

小心读入

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
const int N=105;
int n,p[N];
string s,mp="aeiouy";
bool verse(char c){
    for(int i=0;i<mp.size();i++)
        if(c==mp[i]) return 1;
    return 0;
}
int main(int argc, const char * argv[]) {
    cin>>n;int flag=1;
    for(int i=1;i<=n;i++) cin>>p[i];
    for(int i=1;i<=n;i++){
        getline(cin,s);while(s[0]<'a'||s[0]>'z') getline(cin,s);
        int cnt=0;
        for(int j=0;j<s.size();j++)
            if(verse(s[j])) cnt++;
        if(cnt!=p[i]) flag=0;
        //cout<<cnt<<"
";
    }
    if(flag) cout<<"YES";
    else cout<<"NO";
    return 0;
}

 CF723A|B

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=3e3+5,INF=1e9+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x;
}
int a[5];
int main(){
    for(int i=1;i<=3;i++) a[i]=read();
    sort(a+1,a+4);
    printf("%d",a[3]-a[1]);
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=300;
int n,flag=0,mx=0,cnt=0,len=0;
char s[N];
int main(){
    s[0]='_';
    scanf("%d%s",&n,s+1);s[n+1]='_';
    for(int i=1;i<=n+1;i++){
        if(s[i]=='_'||s[i]=='('||s[i]==')'){//printf("%d
",i);
            if((s[i-1]>='a'&&s[i-1]<='z')||(s[i-1]>='A'&&s[i-1]<='Z')){
                if(!flag) mx=max(mx,len);
                else cnt++;
                len=0;
            } 
        }
        if(s[i]=='(') flag=1;
        if(s[i]==')') flag=0;    
        if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')) len++;    
    }
    printf("%d %d",mx,cnt);
}


 CF733B.Parade

坑太多........提一点吧,三个数取最大值第一个数要同时和另两个比

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=1e5+5,INF=1e9+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int n,w,ans,mx=-INF,mxp,mn=INF,mnp;
int main(){
    n=read();
    for(int i=1;i<=n;i++){
        w=read()-read(); ans+=w;
        if(w>mx) mx=w,mxp=i;
        if(w<mn) mn=w,mnp=i;
    }
    int t1=abs(ans-2*mx),t2=abs(ans-2*mn);//printf("%d %d  %d %d  %d
",mx,t1,mn,t2,ans);
    ans=abs(ans);
     
    if(t1>ans&&t1>t2) printf("%d",mxp);
    else if(t2>ans) printf("%d",mnp);
    else printf("0");
}
原文地址:https://www.cnblogs.com/candy99/p/5925509.html