Codeforces Round #372 +#373 部分题解

Round #372 :

Div 2 A:Crazy Computer

 1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #include<algorithm>
5 using namespace std;
6 int main(){
7     int n,c;
8     scanf("%d%d",&n,&c);
9     int last=0,cnt=0;
10     for (int i=1;i<=n;i++) {
11         int x;
12         scanf("%d",&x);
13         if (x-last>c) cnt=0;
14         last=x;cnt++;
15     }
16     printf("%d\n",cnt);
17     return 0;
18 }
Crazy Computer
Div 2 B：Complete the Word

 1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #include<algorithm>
5 using namespace std;
6 char s[50100];
7 int a[30],n;
8 int cnt=0;
9 int main(){
10     scanf("%s",s+1);
11     int n=strlen(s+1);
12     int l=1;
13     int cnt=0;
14     for (int i=1;i<=n;i++){
15         if (s[i]=='?') {
16             cnt++;
17         }else {
18             int t=s[i]-'A'+1;
19             if (a[t]==1) {
20                 while (s[i]!=s[l]) {
21                     if (s[l]=='?') {
22                         cnt--;
23                     }else a[s[l]-'A'+1]--;
24                     l++;
25                 }
26                 if (s[l]=='?') cnt--;
27                 else a[s[l]-'A'+1]--;
28                 l++;
29             }
30             a[t]++;
31         }
32         if (i-l+1==26) {
33             for (int j=1;j<l;j++) printf("%c",s[j]=='?'?'A':s[j]);
34             for (int j=l;j<=i;j++) {
35                 if (s[j]!='?') printf("%c",s[j]);
36                 else {
37                     for (int k=1;k<=26;k++) {
38                         if (a[k]) continue;
39                         printf("%c",k+'A'-1);
40                         a[k]++;
41                         break;
42                     }
43                 }
44             }
45             for (int j=i+1;j<=n;j++) printf("%c",s[j]=='?'?'A':s[j]);
46             return 0;
47         }
48     }
49     printf("-1\n");
50     return 0;
51 }
Complete the Word

Div 2 C and Div 1 A:Plus and Square Root

 1 #include<cstdio>
2 #include<iostream>
3 #include<algorithm>
4 #include<cstring>
5 using namespace std;
6 typedef long long ll;
7 ll n,x;
8 int main(){
9 cin>>n;
10 x=2;
11 for (ll i=1;i<=n;i++) {
12     ll ans=(i+1)*(i+1)*i-x/i;
13     cout<<ans<<endl;
14     x=i*(i+1);
15 }
16
17 }
Plus and Square Root

Div 2 D and Div 1 B：Complete The Graph

  1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #include<algorithm>
5 #include<queue>
6 using namespace std;
7 typedef long long ll;
8 typedef pair<int,int>ii;
9 typedef pair<ll,ii> iii;
10 #define maxn 1001000
11 #define maxm 20100
12 #define se second
13 struct edges{
14     int to,next;ll dis;int b;
15 }edge[maxm];
16 int id[1010][1010];
17 int nxt[maxn],l;
18 inline void addedge(int x,int y,ll z,int b) {
19     edge[++l]=(edges){y,nxt[x],z,b};nxt[x]=l;
20 }
21 #define INF 0x7fffffff
22 #define MAXINF INF*1ll*INF
23 #define fi first
24 #define inf 10000000000000ll
25 int clo=0;
26 int f[maxn],e[maxn];
27 ll dis[maxn];
28 bool b[maxn];
29 int n;
30 inline void dij(int x,int y) {
31     for (int i=1;i<=clo;i++) dis[i]=MAXINF;
32     dis[id[x][y]]=0;
33     static priority_queue<iii,vector<iii>,greater<iii> > q;
34     q.push(iii(0,ii(x,y)));
35     while (!q.empty()){
36         iii x=q.top();q.pop();
37         int u=x.se.fi,v=x.se.se;
38         if (b[id[u][v]]) continue;
39         b[id[u][v]]=1;
40         for (int i=nxt[u];i;i=edge[i].next){
41             ii t;
42             if (edge[i].dis==inf) t=ii(edge[i].to,v+1);
43             else t=ii(edge[i].to,v);
44             if (edge[i].dis==inf&& v==n-1) continue;
45             if (dis[id[t.fi][t.se]]>dis[id[u][v]]+edge[i].dis) {
46                 dis[id[t.fi][t.se]]=dis[id[u][v]]+edge[i].dis;
47                 f[id[t.fi][t.se]]=id[u][v];e[id[t.fi][t.se]]=i;
48                 q.push(iii(dis[id[t.fi][t.se]],t));
49             }
50         }
51     }
52     return ;
53 }
54 #define M 10010
55 #define N 1010
56 int x[M],y[M];
57 ll w[M];
58 int m,s,t;
59 int main(){
60     ll l;
61     scanf("%d%d%I64d%d%d",&n,&m,&l,&s,&t);
62     s++;t++;
63     for (int i=1;i<=n;i++)
64         for (int j=0;j<n;j++) id[i][j]=++clo;
65     for (int i=1;i<=m;i++) {
66         scanf("%d%d%I64d",x+i,y+i,w+i);
67         x[i]++;y[i]++;
68         if (w[i]==0){
71             }
72         else {
75         }
76     }
77     dij(s,0);
78         if (dis[id[t][0]]<l) {
79             printf("NO\n");
80             return 0;
81         }
82     for (int i=0;i<n;i++) {
83         if (dis[id[t][i]]==MAXINF) continue;
84         if (dis[id[t][i]]%inf>l-i) continue;
85         printf("YES\n");
86         int u=id[t][i],tmp=l-dis[id[t][i]]%inf;
87         int cnt=0;
88         while (u!=id[s][0]) {
89             if (edge[e[u]].dis==inf) {
90                 cnt++;
91                 if (cnt==i) w[edge[e[u]].b]=tmp;
92                 else {
93                     w[edge[e[u]].b]=1;
94                     tmp--;
95                 }
96             }
97             u=f[u];
98         }
99         for (int i=1;i<=m;i++) printf("%d %d %I64d\n",x[i]-1,y[i]-1,w[i]?w[i]:inf);
100         return 0;
101     }
102     printf("NO\n");
103     return 0;
104 }
Complete The Graph

Round #373 ：

Div 2 A：Vitya in the Countryside

 1 #include<cstdio>
2 #include<iostream>
3 #include<algorithm>
4 #include<cstring>
5 using namespace std;
6 int n,a[1000];
7 int main(){
8     scanf("%d",&n);
9     for (int i=1;i<=n;i++) scanf("%d",a+i);
10     if (a[n]==0) {
11         printf("UP\n");
12         return 0;
13     }
14     if (a[n]==15) {
15         printf("DOWN\n");
16         return 0;
17     }
18     if (n==1) {
19         printf("-1\n");
20         return 0;
21     }
22     if (a[n-1]<a[n]) printf("UP\n");
23     else printf("DOWN\n");
24     return 0;
25 }
Vitya in the Countryside

Div 2 B:Anatoly and Cockroaches

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #include<iostream>
5 using namespace std;
6 int n,ans,tmp,cnt,sum;
7 char s[100100];
8 int main(){
9     scanf("%d",&n);
10     scanf("%s",s+1);
11     ans=0x7fffffff;
12     for (int i=1;i<=n;i++) if (s[i]=='b') cnt++;
13     tmp=n/2+n%2-cnt;
14         sum=0;
15         for (int i=1;i<=n;i++)
16             if (i%2==1&&s[i]=='r') sum++ ;
17         if (tmp>=0){
18         ans=min(ans,max(tmp,sum));
19     }
20     tmp-=n%2;
21     if (tmp>=0) {
22     ans=min(ans,max(tmp,n-cnt-sum));
23     }
24     cnt=n-cnt;
25     tmp=n/2-cnt+n%2;
26     sum=0;
27     for (int i=1;i<=n;i++)
28         if (i%2==1&&s[i]=='b') sum++;
29     if (tmp>=0) {
30     ans=min(ans,max(tmp,sum));
31     }
32     tmp-=n%2;
33     if (tmp>=0) {
34     ans=min(ans,max(tmp,n-cnt-sum));
35     }
36 //    printf("%d %d %d %d\n",tmp,sum,cnt,ans);
37     printf("%d\n",ans);
38     return 0;
39 }
Anatoly and Cockraches

Div 2 C and Div 1 A Efim and Strange Grade

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #include<iostream>
5 #include<queue>
6 using namespace std;
7 typedef pair<int,int> ii;
8 #define fi first
9 #define se second
10 priority_queue<int,vector<int>,greater<int> > q;
11 int n,m,flag=0,cnt;
12 char s[200010];
13 int main(){
14     scanf("%d%d",&n,&m);
15     scanf("%s",s+1);
16     for (int i=1;i<=n;i++) {
17         if (s[i]=='.') {cnt=i;flag=1;continue;}
18         if (flag==1 && s[i]>'4') q.push(i);
19     }
20     if (!flag) {printf("%s\n",s+1);return 0;}
21     flag=1;
22     int lst=n+1;
23     for (int i=1;i<=m;i++) {
24         if (q.empty()) break;
25         int u=q.top();q.pop();
26         if (u>lst ) break;
27         lst=u;
28         if (u==cnt+1) {
29             int t=u-2;
30             while (s[t]=='9') s[t]='0',t--;
31             if (t==0) {flag=0;s[t]='1';}
32             else s[t]++;
33         }
34         else {
35             s[u-1]++;
36             if (s[u-1]>'4') q.push(u-1);
37         }
38         s[u]=0;
39     }
40     int n=lst-1;
41     for (;s[n]=='0';n--);
42     s[n+1]=0;
43     if (s[n]=='.') s[n]=0;
44     printf("%s\n",s+flag);
45     return 0;
46 }
Efim and Strange Grade

Div 2 D and Div 1 B

Div 2 E and Div 1 C Sasha and Array

  1 #include<queue>
2 #include<cmath>
3 #include<cstdio>
4 #include<cstring>
5 #include<cstdlib>
6 #include<iostream>
7 #include<algorithm>
8 typedef  long long ll;
9 #define inf 0x7fffffff
10 #define mod 1000000007
11 using namespace std;
12 int n,m;
13 struct M{
14     ll v[3][3];
15     M(){
16         memset(v,0,sizeof(v));
17     }
18     friend M operator*(M a,M b){
19         M c;
20         for(int i=1;i<=2;i++)
21             for(int j=1;j<=2;j++)
22                 for(int k=1;k<=2;k++)
23                     c.v[i][j]=(c.v[i][j]+a.v[i][k]*b.v[k][j]%mod)%mod;
24         return c;
25     }
26     friend M operator+(M a,M b){
27         for(int i=1;i<=2;i++)
28             for(int j=1;j<=2;j++)
29                 (a.v[i][j]+=b.v[i][j])%=mod;
30         return a;
31     }
32     friend M operator^(M a,int b){
33         M ans;
34         for(int i=1;i<=2;i++)
35             ans.v[i][i]=1;
36         for(int i=b;i;i>>=1,a=a*a)
37             if(i&1)ans=ans*a;
38         return ans;
39     }
40 }T,U,V;
41 inline void init() {
42     memset(T.v,0,sizeof(T.v));
43     memset(U.v,0,sizeof(T.v));
44     memset(V.v,0,sizeof(T.v));
45     T.v[1][1]=T.v[1][2]=T.v[2][1]=1;
46     U.v[1][1]=V.v[1][1]=V.v[2][2]=1;
47 }
48 #define maxn 100100
49 struct node{
50     int l,r;M m,lz;
51 }t[maxn*4];
52 #define lc (x<<1)
53 #define rc (lc+1)
54 #define mid ((l+r)>>1)
55 int a[maxn];
56 inline void update(int x) {
57     t[x].m=t[lc].m+t[rc].m;
58 }
59 inline void pb(int x) {
60     t[lc].m=t[x].lz*t[lc].m;
61     t[lc].lz=t[x].lz*t[lc].lz;
62     t[rc].m=t[x].lz*t[rc].m;
63     t[rc].lz=t[x].lz*t[rc].lz;
64     t[x].lz=V;
65 }
66 void build(int x,int l,int r) {
67     t[x].l=l,t[x].r=r;t[x].lz=V;
68     if (l==r) {
69         t[x].m=(T^(a[l]-1))*U;
70         return ;
71     }
72     build(lc,l,mid);build(rc,mid+1,r);
73     update(x);
74     return ;
75 }
76 void add(int x,int le,int ri,M y) {
77     int l=t[x].l,r=t[x].r;
78     if (l>ri||r<le) return ;
79     if (le<=l&&r<=ri) {
80         t[x].lz=t[x].lz*y;
81         t[x].m=y*t[x].m;
82         return ;
83     }
84     pb(x);
86     update(x);
87     return ;
88 }
89 ll sum(int x,int le,int ri) {
90     int l=t[x].l,r=t[x].r;
91     if (l>ri||r<le) return 0;
92     if (le<=l&&r<=ri) return t[x].m.v[1][1];
93     pb(x);
94     return (sum(lc,le,ri)+sum(rc,le,ri))%mod;
95 }
96 int main(){
97     init();
98     scanf("%d%d",&n,&m);
99     for (int i=1;i<=n;i++) scanf("%d",a+i);
100     build(1,1,n);
101     while (m--) {
102         int opt,l,r,x;
103         scanf("%d",&opt);
104         switch (opt) {
105             case 1:
106                 scanf("%d%d%d",&l,&r,&x);
108                 break;
109             case 2:
110                 scanf("%d%d",&l,&r);
111                 printf("%I64d\n",sum(1,l,r));
112                 break;
113         }
114     }
115     return 0;
116 }
Sasha and Array

posted @ 2016-10-05 12:42  New_Godess  阅读(346)  评论(0编辑  收藏