AmazingCounters.com

Codeforces Round #288 (Div. 2)

A. Pasha and Pixels

题目大意:给出n*m的棋盘,初始为全白,每次在上面使一个格子变黑,问多少次的时候使得棋盘上出现2×2的黑格

思路:模拟

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 #define maxn 900000
 6 #define LL long long
 7 using namespace std;
 8 bool map[1001][1001];
 9 int main()
10 {
11         int n,m,k,ans=0,x,y;
12         scanf("%d%d%d",&n,&m,&k);
13         for(int i=1;i<=k;i++)
14         {
15                 scanf("%d%d",&x,&y);
16                 map[x][y]=1;
17                 if(map[x-1][y]&&map[x-1][y-1]&&map[x][y-1]){ans=i;break;}
18                 if(map[x+1][y]&&map[x+1][y+1]&&map[x][y+1]){ans=i;break;}
19                 if(map[x-1][y]&&map[x-1][y+1]&&map[x][y+1]){ans=i;break;}
20                 if(map[x+1][y]&&map[x+1][y-1]&&map[x][y-1]){ans=i;break;}
21         }
22         printf("%d",ans);
23         return 0;
24 }
View Code
题目大意:给你一个奇数,让你交换数中两个数码,使得它变为偶数,并且形成的新数字最大,问能形成最大的数为多少,不行输出-1
思路:还是模拟,由于给的是奇数,只要找到最高位比最后一位小的偶数交换一下,其他情况乱搞一下
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 #include<map>
 6 #include<vector>
 7 #include<algorithm>
 8 #define maxn 900000
 9 #define LL long long
10 using namespace std;
11 char ch[maxn];
12 int main()
13 {
14         scanf("%s",ch+1);
15         int len=strlen(ch+1),j=-1,flag=0;
16         for(int i=1;i<len;i++)if(!((ch[i]-'0')&1))
17         {
18                 j=i;
19                 if(ch[i]<ch[len]){swap(ch[i],ch[len]);flag=1;break;}
20         }
21         if(j==-1){printf("-1\n");return 0;}
22         if(flag==0)swap(ch[j],ch[len]);
23         for(int i=1;i<=len;i++)
24         {
25                 printf("%c",ch[i]);
26         }
27         return 0;
28 }
View Code

C. Anya and Ghosts

题目大意:简单来说就是有m个点,每个时间点最多向右延伸一条线段,每条线段长为t,每个点要被r条线段覆盖,问最少加几条线段?

思路:暴力吧。。。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 #include<algorithm>
 6 #define maxn 900000
 7 #define LL long long
 8 #define T 401
 9 using namespace std;
10 int w[maxn],candle[maxn];
11 int main()
12 {
13         int m,t,r,ans=0;
14         scanf("%d%d%d",&m,&t,&r);
15         if(t<r)
16         {
17                 printf("-1\n");
18                 return 0;
19         }
20         for(int i=1;i<=m;i++)
21         {
22                 scanf("%d",&w[i]);
23                 int last=0;
24                 for(int j=w[i]-t;j<=w[i]-1;j++)
25                 {
26                         if(candle[j+T])last++;
27                 }
28                 for(int j=w[i]-r+last;j<=w[i]-1;j++)
29                 {
30                         candle[j+T]=1;
31                         ans++;
32                 }
33         }
34         printf("%d\n",ans);
35         return 0;
36 }
View Code

D. Tanya and Password

题目大意:n个三个字符的字符串,两个字符串可以拼起来当且仅当一个字符串的后两个字符等于另一个字符的前两个字符,然后就可以插在一起,问n个字符串能否插成n+2个字符的串

思路:把每两个字母看成点,然后跑欧拉路经

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 #include<map>
 6 #include<vector>
 7 #include<algorithm>
 8 #define maxn 900000
 9 #define LL long long
10 using namespace std;
11 vector<vector<int> >G(maxn);
12 string str[maxn];
13 char ch[maxn];
14 int head[maxn],next[maxn],point[maxn],now;
15 int degree[maxn],ans[maxn],hh,selfc[maxn];
16 int mp[maxn];
17 bool visit[maxn];
18 void add(int x,int y)
19 {
20         G[x].push_back(y);
21         next[++now]=head[x];
22         head[x]=now;
23         point[now]=y;
24 }
25 int mhash(string s)
26 {
27         return s[0]*130+s[1];
28 }
29 void dfs(int s)
30 {
31         while(!G[s].empty())
32         {
33                 //cout<<str[point[i]];
34                 int v=G[s].back();
35                 G[s].pop_back();
36                 dfs(v);
37                 ans[++hh]=v;
38         }
39         for(int i=1;i<=selfc[s];i++)ans[++hh]=s;
40         selfc[s]=0;
41 }
42 int main()
43 {
44         int n,h=0,st;
45         scanf("%d",&n);
46         for(int i=1;i<=n;i++)
47         {
48                 string ss="",t="";
49                 scanf("%s",ch+1);
50                 ss=ss+ch[1]+ch[2];t=t+ch[2]+ch[3];
51                 int u=st=mp[mhash(ss)],v=mp[mhash(t)];
52                 if(u==0){u=mp[mhash(ss)]=++h;str[h]=ss;}
53                 if(v==0 && ss!=t){v=mp[mhash(t)]=++h;str[h]=t;}
54                 else if(ss==t)v=u;
55                 degree[u]++;degree[v]--;
56                 if(u==v)
57                 {
58                         selfc[u]++;
59                 }else add(u,v);
60         }
61         int d=0,t=0,s=0;
62         for(int i=1;i<=h;i++)
63         {
64                 if(degree[i]!=0)
65                 {
66                         if(degree[i]==1 && s==0){s=i;}
67                         else if(degree[i]==-1 && t==0){t=i;}
68                         else {printf("NO\n");return 0;}
69                 }
70         }
71         if(t==0)s=st;
72         //cout<<s<<endl;
73         //for(int i=head[1];i;i=next[i]) printf("%d\n",point[i]);
74         if(d<=2)
75         {
76                 dfs(s);
77                 if(hh!=n)
78                 {
79                         printf("NO\n");
80                         return 0;
81                 }
82                 printf("YES\n");
83                 cout<<str[s][0]<<str[s][1];
84                 for(int i=hh;i>=1;i--)
85                 {
86                         cout<<str[ans[i]][1];
87                 }
88         }
89         else printf("NO\n");
90         return 0;
91 }
View Code

E. Arthur and Brackets

题目大意:给你n个括号,已知从左到右第i个括号长度大约是li到ri,让你输出合法的括号序列

思路:很明显每个括号尽可能的小的括号序列越有可能,考虑进栈出栈来匹配括号,我们采取这样的贪心策略,每次栈顶元素的长度尽量取短即可

 1 #include <cstdio>
 2 #include <queue>
 3 #include <stack>
 4 #include <string>
 5 #include <iostream>
 6 #define maxn 1000
 7 using namespace std;
 8 stack <int>q;
 9 string s;
10 int pos[maxn],l[maxn],r[maxn];
11 int main()
12 {
13     int n,now=0;
14     scanf("%d",&n);
15     for(int i=1;i<=n;i++)
16     {
17         scanf("%d%d",&l[i],&r[i]);
18     }
19     for(int i=1;i<=n;i++)
20     {
21         while(!q.empty() && pos[q.top()]+l[q.top()]<=now+1)
22         {
23             if(pos[q.top()]+r[q.top()]<now+1)
24             {
25                 cout<<"IMPOSSIBLE"<<endl;
26                 return 0;
27             }
28             q.pop();
29             s.push_back(')');
30             now++;
31         }
32         q.push(i);
33         pos[i]=++now;
34         s.push_back('(');
35     }
36     while(!q.empty())
37     {
38         //cout<< pos[q.top()]+l[q.top()]<<" "<<now<<endl;
39         if(pos[q.top()]+r[q.top()]<now+1 || pos[q.top()]+l[q.top()]>now+1)
40         {
41           cout<<"IMPOSSIBLE"<<endl;
42           return 0;
43         }
44         q.pop();
45         s.push_back(')');
46         now++;
47     }
48     cout<< s<<endl;
49     return 0;
50 }
View Code

 

 

posted @ 2015-01-29 22:54  philippica  阅读(166)  评论(0编辑  收藏  举报