清北考前刷题day6早安
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 1000010 using namespace std; int n,a[N],cnt1,cnt2; int read() { int x=0,f=1;char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return 0; } int main() { scanf("%d",&n); for(int i=1; i<=n; i++)a[i]=read(); for(int i=1; i<=n; i++) { if(a[i]==5)cnt1++; if(a[i]==10) { if(cnt1==0) { puts("NO");return 0; } else cnt2++,cnt1--; } if(a[i]==20) { if(cnt1==0||(cnt2==0&&cnt1<3)) { puts("NO"); return 0; } if(cnt2) cnt2--,cnt1--; else cnt1-=3; } } puts("YES"); return 0; }
/* 贪心 反过来括号匹配。 */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> '' using namespace std; const int maxn = 1000007; int p = 0; int z[maxn],a[maxn],n,m; int main() { scanf("%d",&n); for (int i = 1; i<=n; i++) scanf("%d",&a[i]); scanf("%d",&m); int x; for (int i = 1; i<=m; i++) { scanf("%d",&x); if (a[x]>0) a[x] = -a[x]; } for (int x = n; x>=1; x--) { if (a[x]>0) { if (z[p] == -a[x]) p--; else { a[x] = -a[x]; p++; z[p]= a[x]; } } else { p++;z[p]=a[x]; } } if (p==0) { for (int i = 1; i<=n; i++) { if (a[i]>0) printf("+%d ",a[i]); else printf("%d ",a[i]); } } else printf("NO\n"); return 0; }
/* Floyed F[i][j][k]表示从i到j能否以k的方式到达。 K = 括号完全匹配 或者 缺少一个右括号 */ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> using namespace std; int n,m; int g[105][105][40],q[105*105*40][3],t,x,y,z; int main() { freopen("maze.in","r",stdin); freopen("maze.out","w",stdout); scanf("%d%d",&n,&m); memset(g,0,sizeof(g)); t = 0; for (int i = 1; i<=m; i++) { scanf("%d%d%d",&x,&y,&z); if (z!=0) { if (z<0) z = abs(z); else z+=10; g[x][y][z] = 1; g[y][x][z] = 1; } else { g[x][y][z] = 1; g[y][x][z] = 1; t++; q[t][0] = x; q[t][1] = y; q[t][2] = 0; t++; q[t][0] = y; q[t][1] = x; q[t][2] = 0; } } for (int i =1; i<=n; i++) { g[i][i][0] = 1; t++; q[t][0] = i; q[t][1] = i; q[t][2] = 0; } for (int s = 1; s<=t; s++) { int x = q[s][0]; int y = q[s][1]; int status = q[s][2]; if (status!=0) { for (int i = 1; i<=n; i++) if (g[i][x][status-10] == 1) { if (g[i][y][0] ==0) { g[i][y][0] = 1; t++; q[t][0] = i; q[t][1] = y; q[t][2] = 0; } } } else { for (int i =1; i<=n; i++) { if (g[i][x][0] ==1 && g[i][y][0] == 0) { g[i][y][0] =1; t++; q[t][0]= i; q[t][1] = y; q[t][2]= 0; } if (g[y][i][0] == 1 && g[x][i][0] ==0) { g[x][i][0] = 1; t++; q[t][0] = x; q[t][1] = i; q[t][2]= 0; } for (int j = 1; j<=10; j ++) { if (g[y][i][j] == 1 && g[x][i][j+20] == 0) { g[x][i][j+20] = 1; t++; q[t][0] = x; q[t][1] = i; q[t][2] = j+20; } } } } } int tt = 0; scanf("%d",&tt); for (; tt; tt--) { scanf("%d%d",&x,&y); if (g[x][y][0] ==1) printf("YES\n"); else printf("NO\n"); } }
折花枝,恨花枝,准拟花开人共卮,开时人去时。
怕相思,已相思,轮到相思没处辞,眉间露一丝。