CF1291
A
1 #include <cstdio> 2 using namespace std; 3 int T,n,sum,num[3100]; 4 char str[3100]; 5 bool suc; 6 int main() 7 { 8 for (scanf("%d",&T);T;T--) 9 { 10 suc = false; 11 sum = 0; 12 scanf("%d%s",&n,str + 1); 13 for (int i = 1;i <= n;i++) 14 { 15 num[i] = str[i] - '0'; 16 sum += num[i]; 17 } 18 int i; 19 for (i = n;i >= 1;i--) 20 if ((num[i] & 1) == 0) 21 { 22 sum -= num[i]; 23 num[i] = -1; 24 }else 25 { 26 suc = true; 27 break; 28 } 29 if (suc == false) 30 { 31 printf("-1\n"); 32 continue; 33 } 34 if ((sum & 1) != 0) 35 for (i--;i >= 1;i--) 36 if (num[i] != -1 && (num[i] & 1)) 37 { 38 sum -= num[i]; 39 num[i] = -1; 40 break; 41 } 42 if ((sum & 1) != 0 || sum == 0) 43 { 44 printf("-1\n"); 45 continue; 46 } 47 for (int i = 1;i <= n;i++) 48 if (num[i] == 0) 49 num[i] = -1; 50 else 51 break; 52 for (int i = 1;i <= n;i++) 53 if (num[i] != -1) 54 printf("%d",num[i]); 55 printf("\n"); 56 } 57 return 0; 58 }
B
1 #include <cstdio> 2 using namespace std; 3 int T,n,left,right; 4 int a[310000]; 5 int main() 6 { 7 for (scanf("%d",&T);T;T--) 8 { 9 scanf("%d",&n); 10 left = 0; 11 right = n - 1; 12 for (int i = 1;i <= n;i++) 13 scanf("%d",&a[i]); 14 for (int i = 1;i <= n;i++) 15 if (a[i] < i - 1) 16 break; 17 else 18 left = i; 19 for (int i = n;i >= 1;i--) 20 if (a[i] < n - i) 21 break; 22 else 23 right = i; 24 if (left >= right) 25 printf("Yes\n"); 26 else 27 printf("No\n"); 28 } 29 30 return 0; 31 }
C
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 int T,n,m,k,a[4100],ans,tres; 5 int main() 6 { 7 for (scanf("%d",&T);T;T--) 8 { 9 ans = 0; 10 scanf("%d%d%d",&n,&m,&k); 11 for (int i = 1;i <= n;i++) 12 scanf("%d",&a[i]); 13 k = min(k,m - 1); 14 for (int i = 0;i <= k;i++) 15 { 16 //l:1 + i r:n - (k - i) 17 tres = 2000000000; 18 for (int j = 0;j <= m - k - 1;j++) 19 //l:1 + i + j r: 20 tres = min(tres,max(a[1 + i + j],a[n + i - m + j + 1])); 21 ans = max(ans,tres); 22 } 23 printf("%d\n",ans); 24 } 25 }
D
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 struct dat 7 { 8 int l,r,id; 9 bool res; 10 } qry[110000]; 11 char s[210000]; 12 int q,B,sum,cnt[200]; 13 bool cmp1(dat a,dat b) 14 { 15 if (a.l / B != b.l / B) 16 return a.l < b.l; 17 return a.r < b.r; 18 } 19 bool cmp2(dat a,dat b) 20 { 21 return a.id < b.id; 22 } 23 void add(int x) 24 { 25 if (cnt[s[x]] == 0) 26 sum++; 27 cnt[s[x]]++; 28 } 29 void des(int x) 30 { 31 if (cnt[s[x]] == 1) 32 sum--; 33 cnt[s[x]]--; 34 } 35 int main() 36 { 37 scanf("%s%d",s + 1,&q); 38 B = sqrt(strlen(s + 1)); 39 for (int i = 1;i <= q;i++) 40 { 41 scanf("%d%d",&qry[i].l,&qry[i].r); 42 qry[i].id = i; 43 } 44 sort(qry + 1,qry + q + 1,cmp1); 45 int l = qry[1].l,r = qry[1].r; 46 for (int i = l;i <= r;i++) 47 add(i); 48 if (qry[1].l == qry[1].r || s[qry[1].l] != s[qry[1].r] || sum >= 3) 49 qry[1].res = true; 50 for (int i = 2;i <= q;i++) 51 { 52 53 if (qry[i].l == qry[i].r || s[qry[i].l] != s[qry[i].r]) 54 { 55 qry[i].res = true; 56 continue; 57 } 58 while (l < qry[i].l) 59 des(l++); 60 while (l > qry[i].l) 61 add(--l); 62 while (r < qry[i].r) 63 add(++r); 64 while (r > qry[i].r) 65 des(r--); 66 if (sum >= 3) 67 qry[i].res = true; 68 } 69 sort(qry + 1,qry + q + 1,cmp2); 70 for (int i = 1;i <= q;i++) 71 printf("%s\n",qry[i].res ? "Yes" : "No"); 72 return 0; 73 }
E
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 const int MAXN = 810000; 5 int fa[MAXN],val[MAXN],vec[MAXN][3]; 6 int n,k,ans; 7 char str[MAXN]; 8 int getfa(int x) 9 { 10 if (fa[x] == x) 11 return x; 12 return fa[x] = getfa(fa[x]); 13 } 14 void merge(int x,int y) 15 { 16 int p = getfa(x),q = getfa(y); 17 if (p == q) 18 return; 19 fa[p] = q; 20 val[q] += val[p]; 21 } 22 int getval(int x) 23 { 24 return min(val[getfa(x)],val[getfa(x + k)]); 25 } 26 int main() 27 { 28 scanf("%d%d%s",&n,&k,str + 1); 29 int tc; 30 for (int i = 1;i <= k;i++) 31 { 32 scanf("%d",&tc); 33 int tx; 34 for (int j = 1;j <= tc;j++) 35 { 36 scanf("%d",&tx); 37 vec[tx][++vec[tx][0]] = i; 38 } 39 } 40 for (int i = 1;i <= k;i++) 41 fa[i] = i,val[i] = 0; 42 for (int i = k + 1;i <= 2 * k;i++) 43 fa[i] = i,val[i] = 1; 44 fa[k << 1 | 1] = k << 1 | 1; 45 val[k << 1 | 1] = 1000000; 46 for (int i = 1;i <= n;i++) 47 { 48 if (vec[i][0] == 2) 49 { 50 if (str[i] == '0') 51 { 52 if (getfa(vec[i][1]) != getfa(vec[i][2] + k)) 53 { 54 ans -= getval(vec[i][1]) + getval(vec[i][2]); 55 merge(vec[i][1],vec[i][2] + k); 56 merge(vec[i][1] + k,vec[i][2]); 57 ans += getval(vec[i][1]); 58 } 59 }else 60 { 61 if (getfa(vec[i][1]) != getfa(vec[i][2])) 62 { 63 ans -= getval(vec[i][1]) + getval(vec[i][2]); 64 merge(vec[i][1] + k,vec[i][2] + k); 65 merge(vec[i][1],vec[i][2]); 66 ans += getval(vec[i][1]); 67 } 68 } 69 }else if (vec[i][0] == 1) 70 { 71 if (str[i] == '0') 72 { 73 ans -= getval(vec[i][1]); 74 merge(vec[i][1],k << 1 | 1); 75 ans += getval(vec[i][1]); 76 }else 77 { 78 ans -= getval(vec[i][1]); 79 merge(vec[i][1] + k,k << 1 | 1); 80 ans += getval(vec[i][1]); 81 } 82 } 83 printf("%d\n",ans); 84 } 85 return 0; 86 }
心之所动 且就随缘去吧