Codeforces Round #376 (Div. 2)

Posted on 2016-10-17 20:14  yyjxx2010xyu  阅读(136)  评论(0编辑  收藏  举报

A 模拟

 1 #include <cstdio>
 2 #include <cstring>
 3 int Ans;
 4 char Str[110];
 5 inline int Abs(int x) {return x>0?x:-x;}
 6 inline int Min(int x,int y) {return x>y?y:x;}
 7 int main()
 8 {
 9     char u='a'; Ans=0;
10     scanf("%s",Str+1);
11     for (int i=1;i<=strlen(Str+1);i++)
12         Ans+=Min(Abs(Str[i]-u),26-Abs(Str[i]-u)),u=Str[i];
13     printf("%d\n",Ans);
14     return 0;
15 }
A

B 贪心

 1 #include <cstdio>
 2 int n,a[200100];
 3 bool Flag;
 4 int main()
 5 {
 6     scanf("%d",&n); Flag=true;
 7     for (int i=1;i<=n;i++) scanf("%d",&a[i]);
 8     for (int i=1;i<n;i++)
 9     {
10         if (a[i]&1)
11         {
12             if (a[i+1]==0) 
13             {
14                 Flag=false;
15                 break;
16             }
17             a[i+1]--;
18         }
19     }
20     if (a[n]&1) Flag=false;
21     if (Flag) puts("YES"); else puts("NO");
22     return 0;    
23 }
B

C 并查集+暴力 没写路径压缩T了好久..

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <vector>
 4 #define pb push_back
 5 using namespace std;
 6 const int Maxn=400100;
 7 int Father[Maxn],c[Maxn],Last[Maxn],l[Maxn],r[Maxn],n,m,k,tot,Cnt[Maxn],Ans,num[Maxn];
 8 vector<int> V[Maxn];
 9 int GetFather(int x)
10 {
11     if (x==Father[x]) return x;
12     return Father[x]=GetFather(Father[x]);
13 }
14 inline void Merge(int x,int y) {Father[GetFather(x)]=GetFather(y);}
15 inline int Max(int x,int y) {return x>y?x:y;}
16 int main()
17 {
18     scanf("%d%d%d",&n,&m,&k); tot=0;
19     for (int i=1;i<=n;i++) scanf("%d",&c[i]);
20     for (int i=1;i<=n;i++) Father[i]=i;
21     for (int i=1;i<=m;i++) scanf("%d%d",&l[i],&r[i]),Merge(l[i],r[i]);
22     for (int i=1;i<=n;i++) if (Father[i]==i) num[i]=++tot;
23     for (int i=1;i<=n;i++) V[num[GetFather(i)]].pb(c[i]);
24     Ans=0;
25     for (int i=1;i<=tot;i++)
26     {
27         int Ret=0,Tmp=V[i].size();
28         for (int j=0;j<Tmp;j++)    Cnt[V[i][j]]=0;
29         for (int j=0;j<Tmp;j++)
30         {
31             Cnt[V[i][j]]=Cnt[V[i][j]]+1;
32             Ret=Max(Ret,Cnt[V[i][j]]);
33         }
34         Ans+=Tmp-Ret;
35     }
36     printf("%d",Ans);
37     return 0;
38 }
C

D 模拟+看了好久的题意 就是一个操作是每个数+1,是的最终的序列为字典序,比较的是序列,比较相邻两个得出可以在的区间求交即可.

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <vector>
 6 #define pb push_back
 7 using namespace std;
 8 const int Maxn=1000100;
 9 int n,c,Len[Maxn],cnt,B[Maxn],x;
10 vector<int> V[Maxn];
11 inline int Min(int x,int y) {return x>y?y:x;}
12 void Work(int x,int y) 
13 {
14     int LEN=Min(Len[x],Len[y]);
15     for (int i=0;i<LEN;i++)
16     {
17         if (V[x][i]==V[y][i]) continue;
18         if (V[x][i]<V[y][i])
19         {
20             B[0]++,B[c-V[y][i]+1]--;
21             B[c-V[x][i]+1]++,B[c]--;
22         } else B[c-V[x][i]+1]++,B[c-V[y][i]+1]--;
23         cnt++; return;
24     }
25     if (Len[x]>Len[y])
26     {
27         puts("-1");
28         exit(0);
29     }
30 }
31 int main()
32 {
33     scanf("%d%d",&n,&c);
34     for (int i=1;i<=n;i++)
35     {
36         scanf("%d",&Len[i]);
37         for (int j=1;j<=Len[i];j++) scanf("%d",&x),V[i].pb(x);
38         if (i!=1) Work(i-1,i);        
39     }
40     int Sum=0;
41     for (int i=0;i<c;i++)
42     {
43         Sum+=B[i];
44         if (Sum==cnt) 
45         {
46             printf("%d\n",i);
47             return 0;
48         }
49     }
50     puts("-1");
51     return 0;
52 }
D

E 直接模拟..

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,tot,i,a[200010]; 
 4 inline int Max(int x,int y) {return x>y?x:y;}
 5 int main()
 6 {
 7     scanf("%d",&n);
 8     for(i=1;i<=n;i++) scanf("%d",&a[i]),a[i]+=a[i-1];
 9     tot=a[n];
10     for(i=n-1;i>1;i--) tot=Max(tot,a[i]-tot);
11     printf("%d\n",tot);
12 }
E

F 枚举前缀就可以了

 1 #include <cstdio>
 2 #define LL long long
 3 const LL Maxn=200000;
 4 LL n,x,vis[Maxn+100],Sum[Maxn+100],Ans;
 5 int main()
 6 {
 7     scanf("%I64d",&n);
 8     for (LL i=1;i<=n;i++) scanf("%I64d",&x),vis[x]++;
 9     for (LL i=1;i<=Maxn;i++) Sum[i]=Sum[i-1]+vis[i];
10     Ans=0;
11     for (LL i=1;i<=Maxn;i++) 
12         if (vis[i]>0)
13         {
14             LL Res=0,t=Maxn/i;
15             for (LL j=1;j<=t;j++) Res+=(Sum[j*i-1]-Sum[(j!=1)?j*i-i-1:j*i-i])*(j-1)*i;
16             Res+=(Sum[Maxn]-Sum[t*i-1])*t*i;
17             if (Res>Ans) Ans=Res;
18         }
19     printf("%I64d\n",Ans);
20     return 0;
21 }
F