第一题A,比赛的时候看过题率百分之3没敢动,然后花了一早上一下午的时间竟然搞出来了,真是奇迹。
首先()是这种摆放的可以消除,所以剩下的情况如果有右括号的就一定在左括号左边,然后我们就可以分两种情况,一种是左括号多的,一种是右括号多的,左括号多的肯定要先输出,这样才能抵消后面的右括号,对左括号多的排序,右括号少的优先,因为后面右括号大的前面多余的左括号可以和他抵消,对右括号多的排序,左括号大的优先,最前面的右括号可以和之前剩下的左括号抵消,然后接下来的左括号越多,就不怕右括号过剩,左括号剩的再和后面的抵消。
Policeman Anatoliy again monitors a lair of unorganized criminal group spreading prohibited Asian drawings. Presently the criminals are also sharing wireless Internet which can be used anonymously by whoever wants to. The lair still has only one entrance, which is also an exit. When someone enters into the lair, Anatoliy writes an opening round bracket in his notepad, and when someone comes out, he writes a closing round bracket.
Anatoliy decided to refrain from eating donuts in order not to spoil the records just like the previous time, but nevertheless, when he tore a sheet out of a notepad to file it to a criminal case, accidentally tore it into pieces. He doesn't want his boss to shout on him, so he must restore his records by connecting pieces in the right order. It's good for him that the layout of notepad sheets allows to determine where top and bottom sides of these pieces are. Anatoliy ensured that the lair of criminals was empty before he started the surveillance and after he ended it.
Input
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of pieces of paper with opening and closing round brackets written on them.
The next n lines contain only characters «(» and «)» — these are the strings written on pieces. The total number of brackets does not exceed 2·105.
Output
If the given pieces can be connected so that the resulting string does not contradict the statement, output in the first line «YES» without quotes. In the second line output n integers separated by spaces — the numbers of pieces in the order they must be connected. The pieces are numbered from one in the order they are mentioned in the input.
If Anatoliy messed up something and the given pieces cannot form the correct record, output in the only line «NO» without quotes.
Examples
3
(
()
)
YES
1 2 3
3
)))
((
(
YES
2 3 1
3
)))
((
)
NO
#include <iostream> #include <cstdio> #include <string.h> #include <algorithm> using namespace std; char s[200005]; int len[200005]; struct node { int l; int r; int id; }a[200005],b[200005],c[200005]; bool cmp1(node x,node y){ return x.l>y.l; } bool cmp2(node x,node y) { return x.r<y.r; } int main() { int n,i,j,z=1,x=1; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%s",s+1); len[i]=strlen(s+1); a[i].l=0; a[i].r=0; a[i].id=i; for(j=1;j<=len[i];j++) { if(s[j]=='(') a[i].l++; else { if(a[i].l) { a[i].l--; } else a[i].r++; } } if(a[i].l>a[i].r) b[z++]=a[i]; else c[x++]=a[i]; } sort(b+1,b+z,cmp2); sort(c+1,c+x,cmp1); int ans=0,flag=0; for(i=1;i<z;i++) { ans-=b[i].r; if(ans<0) { flag=1; break; } ans+=b[i].l; } for(i=1;i<x;i++) { ans-=c[i].r; if(ans<0) { flag=1; break; } ans+=c[i].l; } if(ans!=0) flag=1; if(flag) printf("NO "); else { printf("YES "); for(i=1;i<z;i++) { if(i!=1) cout<<" "; cout<<b[i].id; } for(i=1;i<x;i++) { cout<<" "<<c[i].id; } } return 0; }
下一道是B,当时我做的时候一直想用substr,但是后面又突然想到substr只能返回第一个指向它子串的位置,后面的执行起来比较困难,然后队友过了,他们觉得还觉得这道题特简单,呜呜。
然后就是如果没有happiness的时候要考虑ahappiness的情况,后来补的时候没考虑就一直卡在第29个样例
#include <iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn=2e5+10; char s[maxn]; char t[maxn]= {0,'h','a','p','p','i','n','e','s','s'}; int len; int add(int k) { for(int i=1; i<=9; i++) { if(s[k+i-1]!=t[i]) return 0; } return 1; } int find() { for(int i=1; i<=len; i++) { if(add(i)) return i; } return 0; } int main() { scanf("%s",s+1); len=strlen(s+1); int t1=find(); if(!t1) { swap(s[1],s[2]); int kepa=find(); if(!kepa) printf("YES 1 2 "); else printf("YES 1 3 "); } else { s[t1]=1; int t2=find(); if(!t2) printf("YES %d %d ",t1,t1+1); else { s[t2]=1; int t3=find(); if(!t3) printf("YES %d %d ",t1,t2+1); else printf("NO "); } } return 0; }
这道题是codeforces上的,好不容易做出来了,第二天起来被hack了,还卡他第46个数据,我的做法绕了半天把我自己都绕昏了,在网上看到用差分数组的,感觉比较好,跟大家分享一下