#include<iostream>#include<cstring>#include<algorithm>usingnamespace std;#definexfirst#defineysecondtypedef pair<int,int> PII;constint N =100010;int n, m;
PII stk[N];int a[N];intmain(){scanf("%d%d",&n,&m);int top =0;while(m --){int p, q;scanf("%d%d",&p,&q);if(!p){while(top && stk[top].x ==0) q =max(q, stk[top --].y);while(top >=2&& stk[top -1].y <= q) top -=2;
stk[++ top ]={p, q};}elseif(top){while(top && stk[top].x ==1) q =min(q, stk[top --].y);while(top >=2&& stk[top -1].y >= q) top -=2;
stk[++ top ]={p, q};}}int k = n, l =1, r = n;for(int i =1; i <= top; i ++){if(!stk[i].x){while(r > stk[i].y && l <= r)
a[r --]= k --;}else{while(l < stk[i].y && l <= r)
a[l ++]= k --;}if(l > r)break;}if(top %2)while(l <= r) a[l ++]= k --;elsewhile(l <= r) a[r --]= k --;for(int i =1; i <= n; i ++)
cout << a[i]<<' ';return0;}
括号序列
#include<iostream>#include<cstring>#include<algorithm>usingnamespace std;constint N =5010, MOD =1e9+7;typedeflonglong LL;int n;char s[N];
LL f[N][N];
LL work(){memset(f,0,sizeof f);
f[0][0]=1;// 不选择字符时,左括号比右括号多0, // 这个时候不添加左括号也是一种方案方案数为1for(int i =1; i <= n; i ++){if(s[i]=='('){for(int j =1; j <= n; j ++)
f[i][j]= f[i -1][j -1];}else{
f[i][0]=(f[i -1][1]+ f[i -1][0])% MOD;for(int j =1; j <= n; j ++){
f[i][j]=(f[i -1][j +1]+ f[i][j -1])% MOD;}}}for(int i =0; i <= n; i ++)if(f[n][i])return f[n][i];return-1;}intmain(){scanf("%s", s +1);
n =strlen(s +1);
LL l =work();reverse(s +1, s + n +1);for(int i =1; i <= n; i ++){if(s[i]=='(') s[i]=')';else s[i]='(';}
LL r =work();printf("%lld",(l * r)% MOD);return0;}
C组
小朋友崇拜圈
#include<cstring>#include<iostream>#include<algorithm>usingnamespace std;constint N =1e5+10;int n;int q[N];bool st[N];int res;booldfs(int start,int u,int step){
st[u]=true;if(q[u]== start){
res =max(res, step +1);returntrue;}if(!st[q[u]]){
st[q[u]]=dfs(start, q[u], step +1);if(st[q[u]])returntrue;}returnfalse;}intmain(){
cin >> n;for(int i =1; i <= n; i ++) cin >> q[i];for(int i =1; i <= n; i ++)if(!st[i])dfs(i, i,0);
cout << res << endl;return0;}
耐摔指数
#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>usingnamespace std;constint N =10010;int n;int f[4][N];intmain(){scanf("%d",&n);memset(f,0x3f,sizeof(f));for(int i =0; i <= n; i++)
f[1][i]= i;for(int i =2; i <=3; i++){
f[i][0]=0;for(int j =1; j <= n; j++)for(int k =1; k <= j; k++)
f[i][j]=min(f[i][j],max(f[i -1][k -1], f[i][j - k])+1);}printf("%d\n", f[3][n]);return0;}