Codeforces Round #290 (Div. 2)
评测之前
[remind]总结:看到Div1的A题我就吓尿了、然后换小号做了Div2、最后1秒交了Div2的D题,然后Present Pass,我也是醉了,果然我太弱了、、然后没有了、、、[/remind]
好吧,那么让我们来分析题目>_<
[task]
[/task]
题目大意大概就是、、、卧槽、好吧、模拟、、看数据就能知道辣!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* Author:WNJXYK WebSite:http://wnjxyk.cn Weibo:http://weibo.com/WNJXYK */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m; int tmp=0; int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;i++){ if (i&1){ for (int j=1;j<=m;j++)printf("#"); }else{ ++tmp; if (tmp&1){ for (int j=1;j<m;j++) printf("."); printf("#"); }else{ printf("#"); for (int j=1;j<m;j++) printf("."); } } printf("n"); } return 0; } |
[remind]
Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.
The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, …, dk a cycle if and only if it meets the following condition:
- These k dots are different: if i ≠ j then di is different from dj.
- k is at least 4.
- All dots belong to the same color.
- For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge.
Determine if there exists a cycle on the field.
The first line contains two integers n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board.
Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.
Output “Yes” if there exists a cycle, and “No” otherwise.
1 2 3 4 |
3 4 AAAA ABCA AAAA |
1 |
Yes |
1 2 3 4 |
3 4 AAAA ABCA AADA |
1 |
No |
1 2 3 4 5 |
4 4 YYYR BYBY BBBY BBBY |
1 |
Yes |
1 2 3 4 5 6 7 8 |
7 6 AAAAAB ABBBAB ABAAAB ABABBB ABAAAB ABBBAB AAAAAB |
1 |
Yes |
1 2 3 |
2 13 ABCDEFGHIJKLM NOPQRSTUVWXYZ |
1 |
No |
In first sample test all ‘A‘ form a cycle.
In second sample there is no such cycle.
The third sample is displayed on the picture above (‘Y‘ = Yellow, ‘B‘ = Blue, ‘R‘ = Red).
[/remind]
题目大意就是我们要查找是否有长度大于4的环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
/* Author:WNJXYK WebSite:http://wnjxyk.cn Weibo:http://weibo.com/WNJXYK */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int Maxn=55; int n,m; char map[Maxn][Maxn]; string intmp; bool vis[Maxn][Maxn]; int ind[Maxn][Maxn]; int Ans; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; inline int remax(int a,int b){ if (a>b) return a; return b; } inline int zabs(int x){ if (x<0) return -x; return x; } void dfs(int x,int y,int st){ vis[x][y]=true; ind[x][y]=st; for (int k=0;k<4;k++){ int nx=x+dx[k],ny=y+dy[k]; if (!(1<=nx && nx<=n && 1<=ny && ny<=m))continue; if (map[nx][ny]==map[x][y]){ if (vis[nx][ny]) Ans=remax(Ans,zabs(ind[x][y]-ind[nx][ny])); else dfs(nx,ny,st+1); } } } int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;i++){ cin>>intmp; for (int j=1;j<=m;j++){ map[i][j]=intmp[j-1]; } } for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ if(!vis[i][j])dfs(i,j,0); if (Ans>=3) { printf("Yesn"); return 0; } } } printf("Non"); return 0; } |
[remind]
After checking some examples, she found out that sometimes it wasn’t true. On some papers authors’ names weren’t sorted inlexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters:si ≠ ti. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters siand ti according to their order in alphabet.
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters ‘a’–’z’ (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word “Impossible” (without quotes).
1 2 3 4 |
3 rivest shamir adleman |
1 |
bcdefghijklmnopqrsatuvwxyz |
1 2 3 4 5 6 7 8 9 10 11 |
10 tourist petr wjmzbmr yeputons vepifanov scottwu oooooooooooooooo subscriber rowdark tankengineer |
1 |
Impossible |
1 2 3 4 5 6 7 8 9 10 11 |
10 petr egor endagorion feferivan ilovetanyaromanova kostka dmitriyh maratsnowbear bredorjaguarturnik cgyforever |
1 |
aghjlnopefikdmbcqrstuvwxyz |
1 2 3 4 5 6 7 8 |
7 car care careful carefully becarefuldontforgetsomething otherwiseyouwillbehacked goodluck |
1 |
acbdefhijklmnogpqrstuvwxyz |
[/remind]
题目大意就是我们有一系列假装已经排好序的字符串!我们要更改abcdedfhigklmnopqrstuvwxyz的大小顺序,来使这些字符串成字典序。
我当时感觉就是小的到大的连边,然后做一遍SPFA,然后按照点的权值输出就好!不知道会不会fst、、、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
/* Author:WNJXYK WebSite:http://wnjxyk.cn Weibo:http://weibo.com/WNJXYK */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<queue> using namespace std; const int Maxn=105; int n; string name[Maxn]; bool map[30][30]; struct Edge{ int v; int nxt; Edge(){ } Edge(int a,int b){ v=a;nxt=b; } }; int head[30]; int nume; Edge e[Maxn*200]; inline void addEdge(int u,int v){ e[++nume]=Edge(v,head[u]); head[u]=nume; } inline bool compare(int x,int y){ int len=name[x].length()<name[y].length()?name[x].length():name[y].length(); //cout<<name[x]<<" "<<name[y]<<" => "<<endl; for (int i=0;i<len;i++){ if (name[x][i]!=name[y][i]){ int a=name[x][i]-'a'+1,b=name[y][i]-'a'+1; if (map[a][b]==false){ map[a][b]=true; addEdge(a,b); //printf("%d %c - %cn",i,name[x][i],name[y][i]); return true; }else{ return true; } }else{ //cout<<name[x][i]<<"="<<name[y][i]<<endl; } } if (name[x].length()>name[y].length()) return false; return true; } queue<int> que; struct Dist{ int w; char x; }; Dist dist[30]; bool inque[30]; int inques[30]; inline bool spfa(){ dist[1].w=30; for (int i=2;i<=26;i++) dist[i].w=dist[i-1].w+30; for (int i=1;i<=26;i++) dist[i].x=(char)('a'+i-1); while(!que.empty()) que.pop(); for (int i=1;i<=26;i++){ que.push(i); inque[i]=true; inques[i]=0; } while(!que.empty()){ int now=que.front();que.pop(); for (int i=head[now];i;i=e[i].nxt){ int v=e[i].v; if (dist[v].w<dist[now].w+1){ dist[v].w=dist[now].w+1; if (inque[v]==false){ inques[v]++; if (inques[v]>n) return false; inque[v]=true; que.push(v); } } } inque[now]=false; } return true; } bool cmp(Dist a,Dist b){ if (a.w<b.w) return true; return false; } int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) cin>>name[i]; for (int i=1;i<n;i++){ for (int j=i+1;j<=n;j++){ if (!compare(i,j)){ printf("Impossiblen"); return 0; } } } if (spfa()){ sort(dist+1,dist+27,cmp); for (int i=1;i<=26;i++) cout<<dist[i].x; cout<<endl; }else{ printf("Impossiblen"); } return 0; } |
[remind]
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
The first line contains an integer n (1 ≤ n ≤ 300), number of cards.
The second line contains n numbers li (1 ≤ li ≤ 109), the jump lengths of cards.
The third line contains n numbers ci (1 ≤ ci ≤ 105), the costs of cards.
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
1 2 3 |
3 100 99 9900 1 1 1 |
1 |
2 |
1 2 3 |
5 10 20 30 40 50 1 1 1 1 1 |
1 |
-1 |
1 2 3 |
7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10 |
1 |
6 |
1 2 3 |
8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026 |
1 |
7237 |
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can’t jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can’t jump to any cell whose index is not a multiple of 10, so you should output -1.
[/remind]
题目大意:有n个数字,每个数字有花费,我们要花最少的钱,是选择的数字的总GCD为1.
Orz一开始被忽悠过去做E、、【他们说E简单、、】然后最后十几分钟赶快写了一个纯暴力DP的D、【最后一秒钟提交Present Pass我也是醉、、、】总之很惊险,可能会T掉、【因为太暴力了】祝好运!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* Author:WNJXYK WebSite:http://wnjxyk.cn Weibo:http://weibo.com/WNJXYK */ #include<cstdio> #include<cstring> #include<algorithm> #include<map> using namespace std; int gcd(int x,int y){ return y==0?x:gcd(y,x%y); } const int Maxn=305; int cst[Maxn]; int len[Maxn]; int n; map<int,int> dp[Maxn]; map<int,int>::iterator it; int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&len[i]); for (int i=1;i<=n;i++) scanf("%d",&cst[i]); int tmpAllGcd=len[1]; for (int i=2;i<=n;i++) tmpAllGcd=gcd(tmpAllGcd,len[i]); if (tmpAllGcd!=1){printf("-1n");return 0;} dp[1][len[1]]=cst[1]; for (int i=2;i<=n;i++){ dp[i][len[i]]=cst[i]; for (it=dp[i-1].begin();it!=dp[i-1].end();it++){ dp[i][it->first]=dp[i-1][it->first]; int lenx=gcd(len[i],it->first); int val=it->second; if (dp[i][lenx]==0 || dp[i][lenx]>val+cst[i]) dp[i][lenx]=val+cst[i]; } } int Ans=2147483647; for (int i=1;i<=n;i++) if (dp[i][1]!=0) Ans=Ans>dp[i][1]?dp[i][1]:Ans; printf("%dn",Ans); return 0; } |
[remind]E题没做(-。-;)、、[/remind]
评测之后
A题Accept
B题Accept
C题Accept
D题FST、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、世界再见、
好吧,昨天一躺下来就知道D哪里错了、、有一个更新的时候忘记判断了、、毕竟是15分钟码完然后最后1秒教的程序、、、、的确坑、、改了一下果然A掉了、QAQ、、SB错毁一生、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* Author:WNJXYK WebSite:http://wnjxyk.cn Weibo:http://weibo.com/WNJXYK */ #include<cstdio> #include<cstring> #include<algorithm> #include<map> using namespace std; int gcd(int x,int y){ return y==0?x:gcd(y,x%y); } const int Maxn=305; int cst[Maxn]; int len[Maxn]; int n; map<int,int> dp[Maxn]; map<int,int>::iterator it; int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&len[i]); for (int i=1;i<=n;i++) scanf("%d",&cst[i]); int tmpAllGcd=len[1]; for (int i=2;i<=n;i++) tmpAllGcd=gcd(tmpAllGcd,len[i]); if (tmpAllGcd!=1){printf("-1n");return 0;} dp[1][len[1]]=cst[1]; for (int i=2;i<=n;i++){ dp[i][len[i]]=cst[i]; for (it=dp[i-1].begin();it!=dp[i-1].end();it++){ if (dp[i][it->first]==0 || dp[i][it->first]>dp[i-1][it->first])dp[i][it->first]=dp[i-1][it->first]; //警察叔叔,就是上一句犯了SB错! int lenx=gcd(len[i],it->first); int val=it->second; if (dp[i][lenx]==0 || dp[i][lenx]>val+cst[i]) dp[i][lenx]=val+cst[i]; } } int Ans=2147483647; for (int i=1;i<=n;i++) if (dp[i][1]!=0) Ans=Ans>dp[i][1]?dp[i][1]:Ans; printf("%dn",Ans); return 0; } |

原文链接:Codeforces Round #290 (Div. 2)
WNJXYKの博客 版权所有,转载请注明出处。
orzorz
orzorz