一本通提高篇
1422
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 struct dat 5 { 6 int s,f; 7 } a[1100]; 8 int res,n,t; 9 bool cmp(dat x,dat y) 10 { 11 return x.f < y.f; 12 } 13 int main() 14 { 15 scanf("%d",&n); 16 for (int i = 1;i <= n;i++) 17 scanf("%d%d",&a[i].s,&a[i].f); 18 sort(a + 1,a + n + 1,cmp); 19 t = a[1].f; 20 res = 1; 21 for (int i = 2;i <= n;i++) 22 if (a[i].s >= t) 23 { 24 res++; 25 t = a[i].f; 26 } 27 printf("%d\n",res); 28 }
1423
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 struct dat 9 { 10 int s,f,v; 11 } a[5100]; 12 int n,m,res; 13 bool tre[31000]; 14 bool cmp(dat x,dat y) 15 { 16 return x.f < y.f; 17 } 18 int main() 19 { 20 scanf("%d%d",&n,&m); 21 for (int i = 1;i <= m;i++) 22 { 23 scanf("%d%d%d",&a[i].s,&a[i].f,&a[i].v); 24 } 25 sort(a + 1,a + m + 1,cmp); 26 for (int i = 1;i <= m;i++) 27 { 28 int sum = 0; 29 for (int o = a[i].s;o <= a[i].f;o++) 30 if (tre[o] == true) 31 sum++; 32 if (sum >= a[i].v) 33 continue; 34 else 35 { 36 37 for (int o = a[i].f;o >= a[i].s;o--) 38 { 39 if (!tre[o]) 40 { 41 sum++; 42 tre[o] = true; 43 res++; 44 if (sum >= a[i].v) 45 break; 46 } 47 } 48 } 49 } 50 printf("%d\n",res); 51 return 0; 52 }
1424
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 struct dat 9 { 10 double x,y; 11 } a[21000]; 12 int T,ans,cnt,l,w,n,t; 13 bool suc; 14 bool cmp(dat x,dat y) 15 { 16 return x.x < y.x; 17 } 18 int main() 19 { 20 scanf("%d",&T); 21 for (int i = 1;i <= T;i++) 22 { 23 cnt = ans = 0; 24 suc = true; 25 scanf("%d%d%d",&n,&l,&w); 26 int tx,tr; 27 for (int i = 1;i <= n;i++) 28 { 29 scanf("%d%d",&tx,&tr); 30 if (tr <= w / 2) 31 continue; 32 cnt++; 33 a[cnt].x = tx - sqrt(tr * tr - w * w / 4.0); 34 a[cnt].y = tx + sqrt(tr * tr - w * w / 4.0); 35 } 36 sort(a + 1,a + cnt + 1,cmp); 37 double t = 0.0; 38 int i = 1; 39 while (t < l) 40 { 41 ans++; 42 double s = t; 43 for (;a[i].x <= s && i <= cnt;i++) 44 if (t < a[i].y) 45 t = a[i].y; 46 if (t == s && s < l) 47 { 48 printf("-1\n"); 49 suc = false; 50 break; 51 } 52 } 53 if (suc == true) 54 printf("%d\n",ans); 55 } 56 57 return 0; 58 }
1425
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 int n,a[11000],b[11000],m[11000],ans[11000],s[11000]; 9 int main() 10 { 11 scanf("%d",&n); 12 for (int i = 1;i <= n;i++) 13 scanf("%d",&a[i]); 14 for (int i = 1;i <= n;i++) 15 scanf("%d",&b[i]); 16 for (int i = 1;i <= n;i++) 17 { 18 m[i] = min(a[i],b[i]); 19 s[i] = i; 20 } 21 for (int i = 1;i <= n - 1;i++) 22 { 23 for (int j = i + 1;j <= n;j++) 24 if (m[i] > m[j]) 25 { 26 swap(m[i],m[j]); 27 swap(s[i],s[j]); 28 } 29 } 30 int k = 0,t = n + 1; 31 for (int i = 1;i <= n;i++) 32 { 33 if (m[i] == a[s[i]]) 34 { 35 k++; 36 ans[k] = s[i]; 37 }else if (m[i] == b[s[i]]) 38 { 39 t--; 40 ans[t] = s[i]; 41 } 42 } 43 k = 0; 44 t = 0; 45 for (int i = 1;i <= n;i++) 46 { 47 k += a[ans[i]]; 48 t = max(k,t); 49 t += b[ans[i]]; 50 } 51 printf("%d\n",t); 52 for (int i = 1;i <= n;i++) 53 printf("%d ",ans[i]); 54 return 0; 55 }
1426
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 struct dat 9 { 10 int t,v; 11 }; 12 bool cmp(dat x,dat y) 13 { 14 return x.v > y.v; 15 } 16 int m,n; 17 dat a[510]; 18 bool use[510]; 19 int main() 20 { 21 scanf("%d%d",&m,&n); 22 for (int i = 1;i <= n;i++) 23 scanf("%d",&a[i].t); 24 for (int i = 1;i <= n;i++) 25 scanf("%d",&a[i].v); 26 sort(a + 1,a + n + 1,cmp); 27 bool suc; 28 for (int i = 1;i <= n;i++) 29 { 30 suc = false; 31 for (int j = a[i].t;j >= 1;j--) 32 { 33 if (use[j] == false) 34 { 35 suc = true; 36 use[j] = true; 37 break; 38 } 39 } 40 if (suc == false) 41 { 42 m -= a[i].v; 43 } 44 } 45 printf("%d\n",m); 46 return 0; 47 }
1433
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 const int MAXN = 110000; 5 int n,c,a[MAXN]; 6 bool check(int d) 7 { 8 int cnt = 1,pos = a[1] + d; 9 for (int i = 2;i <= n;i++) 10 { 11 if (a[i] < pos) 12 continue; 13 cnt++; 14 pos = a[i] + d; 15 } 16 if (cnt >= c) 17 return true; 18 return false; 19 } 20 int main() 21 { 22 scanf("%d%d",&n,&c); 23 for (int i = 1;i <= n;i++) 24 scanf("%d",&a[i]); 25 sort(a + 1,a + n + 1); 26 int l = 0,r = a[n] - a[1]; 27 while (l < r) 28 { 29 int mid = l + r + 1 >> 1; 30 if (check(mid)) 31 l = mid; 32 else 33 r = mid - 1; 34 } 35 printf("%d\n",l); 36 return 0; 37 }
1434
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 const double eps = 0.000001; 9 int n,L; 10 double a[110000],b[110000],sum[110000],ans,minn; 11 int main() 12 { 13 scanf("%d%d",&n,&L); 14 for (int i = 1;i <= n;i++) 15 scanf("%lf",&a[i]); 16 double l = -1000000.0,r = 1000000.0; 17 while (r - l > eps) 18 { 19 double mid = (l + r) / 2.0; 20 21 for (int i = 1;i <= n;i++) 22 b[i] = a[i] - mid; 23 for (int i = 1;i <= n;i++) 24 sum[i] = (sum[i - 1] + b[i]); 25 ans = -10000000000; 26 minn = 10000000000; 27 for (int i = L;i <= n;i++) 28 { 29 minn = min(minn,sum[i - L]); 30 ans = max(ans,sum[i] - minn); 31 } 32 //new version 33 34 for (int i = L;i <= n;i++) 35 { 36 for (int j = 1;j <= i - L;j++) 37 ans = min(ans,(sum[i] - sum[j - 1]) / (i - j + 1)); 38 } 39 if (ans >= 0) 40 l = mid; 41 else 42 r = mid; 43 } 44 printf("%d\n",(int)(r * 1000)); 45 return 0; 46 }
1435
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 const double eps = 0.000000001; 9 int T,n; 10 double a[11000],b[11000],c[11000]; 11 double f(double x) 12 { 13 double res = -10000000000.0; 14 for (int i = 1;i <= n;i++) 15 res = max(res,a[i] * x * x + b[i] * x + c[i]); 16 return res; 17 } 18 int main() 19 { 20 for (scanf("%d",&T);T;T--) 21 { 22 scanf("%d",&n); 23 for (int i = 1;i <= n;i++) 24 scanf("%lf%lf%lf",&a[i],&b[i],&c[i]); 25 double l = 0.0,r = 1000.0,lmid,rmid; 26 while (r - l > eps) 27 { 28 lmid = l + (r - l) / 3.0; 29 rmid = r - (r - l) / 3.0; 30 if (f(lmid) > f(rmid)) 31 l = lmid; 32 else 33 r = rmid; 34 } 35 printf("%.4lf\n",f(l)); 36 } 37 return 0; 38 }
1440
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 int n,m,ans,a[20]; 9 void dfs(int k) 10 { 11 if (n == 0) 12 return; 13 if (k == m) 14 { 15 if (n >= a[k - 1]) 16 { 17 ans++; 18 return; 19 } 20 } 21 for (int i = a[k - 1];i <= n / (m - k + 1);i++) 22 { 23 a[k] = i; 24 n -= i; 25 dfs(k + 1); 26 n += i; 27 } 28 } 29 int main() 30 { 31 scanf("%d%d",&n,&m); 32 a[0] = 1; 33 dfs(1); 34 printf("%d\n",ans); 35 return 0; 36 }
1441
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 int n,m,res = 200000000; 9 void dfs(int st,int v,int s,int r,int h) 10 { 11 if (st == m + 1) 12 { 13 if(v == n) 14 res = min(res,s); 15 return; 16 } 17 if (v + (r - 1) * (r - 1) * (h - 1) * (m - st + 1) < n) 18 return; 19 if (v + 1 * 1 * 1 * (m - st + 1) > n) 20 return; 21 if ((n - v) / r * 2 + s >= res) 22 return; 23 for (int i = r - 1;i >= m - st + 1;i--) 24 for (int j = h - 1;j >= m - st + 1;j--) 25 { 26 int ns = s + 2 * i * j; 27 int nv = v + i * i * j; 28 if (ns < res && nv <= n) 29 dfs(st + 1,nv,ns,i,j); 30 } 31 } 32 int main() 33 { 34 scanf("%d%d",&n,&m); 35 for (int i = m;i * i * m <= n;i++) 36 { 37 for (int j = m;j * i * i <= n;j++) 38 { 39 int ns = 2 * i * j + i * i; 40 int nv = i * i * j; 41 if (ns < res) 42 dfs(2,nv,ns,i,j); 43 } 44 } 45 printf("%d\n",res); 46 return 0; 47 }
1442
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 int n,cnt,a[70],nxt[70],minx,sum,len; 5 bool vis[70],suc; 6 void dfs(int k,int lst,int res) 7 { 8 if (res == 0) 9 { 10 if (k == sum / len) 11 { 12 suc = true; 13 return; 14 } 15 //k != m 16 int i; 17 for (i = 1;i <= cnt;i++) 18 if (vis[i] == false) 19 break; 20 vis[i] = true; 21 dfs(k + 1,i,len - a[i]); 22 vis[i] = false; 23 if (suc) 24 return; 25 } 26 //res != 0 27 int l = lst + 1; 28 while (a[l] > res) 29 l++; 30 for (int i = l;i <= cnt;i++) 31 { 32 if (vis[i] == false) 33 { 34 vis[i] = true; 35 dfs(k,i,res - a[i]); 36 vis[i] = false; 37 if (suc) 38 return; 39 i = nxt[i]; 40 } 41 } 42 return; 43 } 44 bool cmp(int a,int b) 45 { 46 return a > b; 47 } 48 int main() 49 { 50 scanf("%d",&n); 51 int td; 52 for (int i = 1;i <= n;i++) 53 { 54 scanf("%d",&td); 55 if (td <= 50) 56 { 57 a[++cnt] = td; 58 minx = max(minx,a[cnt]); 59 sum += a[cnt]; 60 } 61 } 62 sort(a + 1,a + n + 1,cmp); 63 nxt[cnt] = cnt; 64 for (int i = cnt - 1;i >= 1;i--) 65 { 66 if (a[i] == a[i + 1]) 67 nxt[i] = nxt[i + 1]; 68 else 69 nxt[i] = i; 70 } 71 72 for (len = minx;len <= sum / 2;len++) 73 { 74 if (sum % len == 0) 75 { 76 suc = false; 77 vis[1] = true; 78 dfs(1,1,len - a[1]); 79 vis[1] = false; 80 if (suc) 81 { 82 printf("%d\n",len); 83 return 0; 84 } 85 } 86 } 87 printf("%d\n",sum); 88 return 0; 89 }
1443(poj2248)
1 bool dfs(int x,int lst) 2 {//把a[0]到a[dep-1] 3 if (x == dep) 4 { 5 if (a[x - 1] == n) 6 return true; 7 return false; 8 } 9 for (int i = 0;i <= x - 1;i++) 10 for (int j = i;j <= x - 1;j++) 11 { 12 if (a[i] + a[j] > n) 13 break; 14 if (a[i] + a[j] > lst) 15 { 16 a[x] = a[i] + a[j]; 17 if (dfs(x + 1,a[x])) 18 return true; 19 } 20 } 21 return false; 22 } 23 24 int main() 25 { 26 a[0] = 1; 27 while (scanf("%d",&n) && n) 28 { 29 for (int i = 1;;i++) 30 { 31 dep = i; 32 if (dfs(1,1)) 33 { 34 for (int j = 0;j <= i - 1;j++) 35 printf("%d ",a[j]); 36 break; 37 } 38 } 39 } 40 }
1443
1 #include <cstdio> 2 using namespace std; 3 int minn,n,a[1000],ans[1000]; 4 void dfs(int x) 5 { 6 if (x - 1> minn) 7 return; 8 if (a[x - 1] > n) 9 return; 10 if (a[x - 1] == n) 11 { 12 if (x - 1 >= minn) 13 return; 14 minn = x - 1; 15 for (int i = 1;i < x;i++) 16 ans[i] = a[i]; 17 return; 18 } 19 for (int j = x - 1;j >= 1;j--) 20 { 21 if (a[x - 1] + a[j] <= n) 22 { 23 a[x] = a[x - 1] + a[j]; 24 dfs(x + 1); 25 a[x] = 0; 26 } 27 } 28 29 } 30 int main() 31 { 32 while (scanf("%d",&n) > 0 && n) 33 { 34 a[1] = 1; 35 minn = 10000000; 36 dfs(2); 37 for (int i = 1;i <= minn;i++) 38 printf("%d ",ans[i]); 39 printf("\n"); 40 } 41 }
1444
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 typedef long long ll; 6 const int MAXN = 110000; 7 ll f[MAXN],ans[MAXN],cnt,a,b; 8 bool suc; 9 ll gcd(ll x,ll y) 10 { 11 if (y == 0) 12 return x; 13 return gcd(y,x % y); 14 } 15 void dfs(ll mol,ll den,ll st,ll dep) 16 { 17 if (dep == cnt - 1) 18 { 19 if (mol != 1) 20 { 21 return; 22 } 23 if (den < st) 24 { 25 return; 26 } 27 f[dep + 1] = den; 28 if (suc == false) 29 { 30 for (int i = 1;i <= cnt;i++) 31 ans[i] = f[i]; 32 suc = true; 33 }else 34 { 35 if (f[cnt] < ans[cnt]) 36 { 37 for (int i = 1;i <= cnt;i++) 38 ans[i] = f[i]; 39 } 40 } 41 return; 42 } 43 ll sta = max(st,den / mol); 44 for (ll i = sta;;i++) 45 { 46 if (mol * i - den < 0) 47 continue; 48 if ((cnt - dep + 1) * den <= mol * i) 49 break; 50 ll m = mol * i - den; 51 ll de = den * i; 52 ll d = gcd(m,de); 53 f[dep + 1] = i; 54 dfs(m / d,de / d,i + 1,dep + 1); 55 } 56 } 57 int main() 58 { 59 scanf("%lld%lld",&a,&b); 60 for (cnt = 1;;cnt++) 61 { 62 memset(f,0,sizeof(f)); 63 suc = false; 64 dfs(a,b,b / a + 1,0); 65 if (suc) 66 { 67 for (int i = 1;i <= cnt;i++) 68 printf("%lld ",ans[i]); 69 return 0; 70 } 71 } 72 }
1448
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 int qx[600000],qy[600000],dis[510][510],li,ri,T,r,c; 9 char mp[510][510]; 10 const int mx[] = {-1,-1,1,1}; 11 const int my[] = {-1,1,-1,1}; 12 void ladd(int x,int y) 13 { 14 if (li == ri) 15 { 16 qx[ri] = x; 17 qy[ri] = y; 18 ri++; 19 }else 20 { 21 li--; 22 qx[li] = x; 23 qy[li] = y; 24 } 25 } 26 void radd(int x,int y) 27 { 28 qx[ri] = x; 29 qy[ri] = y; 30 ri++; 31 } 32 int main() 33 { 34 for (scanf("%d",&T);T;T--) 35 { 36 scanf("%d%d",&r,&c); 37 for (int i = 1;i <= r;i++) 38 scanf("%s",mp[i] + 1); 39 memset(dis,0x1f,sizeof(dis)); 40 dis[1][1] = 0; 41 li = ri = 300000; 42 qx[ri] = 1; 43 qy[ri] = 1; 44 ri++; 45 while (li != ri) 46 { 47 int nx = qx[li],ny = qy[li]; 48 ++li; 49 for (int i = 0;i <= 3;i++) 50 { 51 int xx = nx + mx[i]; 52 int yy = ny + my[i]; 53 if (xx <= 0 || xx >= r + 2 || yy <= 0 || yy >= c + 2) 54 continue; 55 if (i == 0) 56 { 57 if (mp[nx - 1][ny - 1] == '\\') 58 { 59 if (dis[xx][yy] <= dis[nx][ny]) 60 continue; 61 ladd(xx,yy); 62 dis[xx][yy] = dis[nx][ny]; 63 } 64 else 65 { 66 if (dis[xx][yy] <= dis[nx][ny] + 1) 67 continue; 68 radd(xx,yy); 69 dis[xx][yy] = dis[nx][ny] + 1; 70 } 71 }else if (i == 1) 72 { 73 if (mp[nx - 1][ny] == '/') 74 { 75 if (dis[xx][yy] <= dis[nx][ny]) 76 continue; 77 ladd(xx,yy); 78 dis[xx][yy] = dis[nx][ny]; 79 } 80 else 81 { 82 if (dis[xx][yy] <= dis[nx][ny] + 1) 83 continue; 84 radd(xx,yy); 85 dis[xx][yy] = dis[nx][ny] + 1; 86 } 87 }else if (i == 2) 88 { 89 if (mp[nx][ny - 1] == '/') 90 { 91 if (dis[xx][yy] <= dis[nx][ny]) 92 continue; 93 ladd(xx,yy); 94 dis[xx][yy] = dis[nx][ny]; 95 } 96 else 97 { 98 if (dis[xx][yy] <= dis[nx][ny] + 1) 99 continue; 100 radd(xx,yy); 101 dis[xx][yy] = dis[nx][ny] + 1; 102 } 103 }else if (i == 3) 104 { 105 if (mp[nx][ny] == '\\') 106 { 107 if (dis[xx][yy] <= dis[nx][ny]) 108 continue; 109 ladd(xx,yy); 110 dis[xx][yy] = dis[nx][ny]; 111 } 112 else 113 { 114 if (dis[xx][yy] <= dis[nx][ny] + 1) 115 continue; 116 radd(xx,yy); 117 dis[xx][yy] = dis[nx][ny] + 1; 118 } 119 } 120 } 121 } 122 if (dis[r + 1][c + 1] != dis[0][0]) 123 printf("%d\n",dis[r + 1][c + 1]); 124 else 125 printf("NO SOLUTION\n"); 126 } 127 return 0; 128 }
1449
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 struct dat 9 { 10 int mtx[3][5]; 11 friend bool operator == (dat x,dat y) 12 { 13 for (int i = 1;i <= 4;i++) 14 if (x.mtx[1][i] != y.mtx[1][i] || x.mtx[2][i] != y.mtx[2][i]) 15 return false; 16 return true; 17 } 18 }; 19 int dis[51000],vis[51000]; 20 dat sta,fnsh,pre[51000]; 21 char wrk[51000]; 22 int calc(int x) 23 { 24 int res = 1; 25 for (int i = 2;i <= x;i++) 26 res *= i; 27 return res; 28 } 29 int get(dat x) 30 { 31 int t[10],res = 0,sum = 0; 32 for (int i = 1;i <= 4;i++) 33 t[i] = x.mtx[1][i]; 34 for (int i = 4;i >= 1;i--) 35 t[8 - i + 1] = x.mtx[2][i]; 36 //康托展开的部分 37 for (int i = 1;i <= 8;i++) 38 { 39 sum = 0; 40 for (int j = i + 1;j <= 8;j++) 41 if (t[j] < t[i]) 42 sum++; 43 res += sum * calc(8 - i); 44 } 45 return res; 46 } 47 dat change(dat x,int opt) 48 { 49 dat res; 50 if (opt == 0) 51 { 52 for (int i = 1;i <= 4;i++) 53 { 54 res.mtx[1][i] = x.mtx[2][i]; 55 res.mtx[2][i] = x.mtx[1][i]; 56 } 57 return res; 58 } 59 if (opt == 1) 60 { 61 res.mtx[1][1] = x.mtx[1][4]; 62 res.mtx[2][1] = x.mtx[2][4]; 63 for (int i = 2;i <= 4;i++) 64 { 65 res.mtx[1][i] = x.mtx[1][i - 1]; 66 res.mtx[2][i] = x.mtx[2][i - 1]; 67 } 68 return res; 69 } 70 res.mtx[1][1] = x.mtx[1][1]; 71 res.mtx[1][2] = x.mtx[2][2]; 72 res.mtx[1][3] = x.mtx[1][2]; 73 res.mtx[1][4] = x.mtx[1][4]; 74 res.mtx[2][1] = x.mtx[2][1]; 75 res.mtx[2][2] = x.mtx[2][3]; 76 res.mtx[2][3] = x.mtx[1][3]; 77 res.mtx[2][4] = x.mtx[2][4]; 78 return res; 79 } 80 void print(dat x) 81 { 82 if (x == sta) 83 return; 84 print(pre[get(x)]); 85 printf("%c",wrk[get(x)]); 86 //因为题目要求每行输出60个 87 } 88 void bfs() 89 { 90 memset(dis,0x1f,sizeof(dis)); 91 queue <dat> que; 92 que.push(sta); 93 dis[get(sta)] = 0; 94 while (!que.empty()) 95 { 96 dat now = que.front(); 97 que.pop(); 98 if (now == fnsh) 99 { 100 printf("%d\n",dis[get(now)]); 101 print(fnsh); 102 printf("\n"); 103 break; 104 } 105 for (int i = 0;i <= 2;i++) 106 { 107 dat tmp = change(now,i); 108 if (vis[get(tmp)]) 109 continue; 110 vis[get(tmp)] = true; 111 que.push(tmp); 112 dis[get(tmp)] = dis[get(now)] + 1; 113 pre[get(tmp)] = now; 114 wrk[get(tmp)] = 'A' + i; 115 } 116 } 117 } 118 int main() 119 { 120 for (int i = 1;i <= 4;i++) 121 sta.mtx[1][i] = i; 122 for (int i = 4;i >= 1;i--) 123 sta.mtx[2][i] = 8 - i + 1; 124 for (int i = 1;i <= 4;i++) 125 scanf("%d",&fnsh.mtx[1][i]); 126 for (int i = 4;i >= 1;i--) 127 scanf("%d",&fnsh.mtx[2][i]); 128 bfs(); 129 return 0; 130 }
1450
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 queue <int> quex[2],quey[2]; 9 const int mx[] = {-2,-2,-1,-1,1,1,2,2}; 10 const int my[] = {-1,1,-2,2,-2,2,-1,1}; 11 int n,ans,sx,sy,fx,fy,T; 12 bool vis[2][310][310]; 13 int dis[2][310][310]; 14 bool solve(int o) 15 { 16 int nx = quex[o].front(); 17 quex[o].pop(); 18 int ny = quey[o].front(); 19 quey[o].pop(); 20 for (int i = 0;i <= 7;i++) 21 { 22 int xx = nx + mx[i]; 23 int yy = ny + my[i]; 24 if (xx >= 0 && yy >= 0 && xx <= n - 1 && yy <= n - 1 && !vis[o][xx][yy]) 25 { 26 vis[o][xx][yy] = true; 27 28 quex[o].push(xx); 29 quey[o].push(yy); 30 dis[o][xx][yy] = dis[o][nx][ny] + 1; 31 if (vis[o ^ 1][xx][yy]) 32 { 33 ans = dis[o][xx][yy] + dis[o ^ 1][xx][yy]; 34 return true; 35 } 36 } 37 } 38 return false; 39 } 40 void bfs() 41 { 42 ans = 0; 43 memset(vis,0,sizeof(vis)); 44 memset(dis,0x1f,sizeof(dis)); 45 dis[0][sx][sy] = 0; 46 dis[1][fx][fy] = 0; 47 vis[0][sx][sy] = true; 48 vis[1][fx][fy] = true; 49 while (!quex[0].empty()) 50 quex[0].pop(); 51 while (!quex[1].empty()) 52 quex[1].pop(); 53 while (!quey[0].empty()) 54 quey[0].pop(); 55 while (!quey[1].empty()) 56 quey[1].pop(); 57 quex[0].push(sx); 58 quey[0].push(sy); 59 quex[1].push(fx); 60 quey[1].push(fy); 61 while (!quex[0].empty() && !quex[1].empty()) 62 { 63 if (quex[0].size() > quex[1].size()) 64 { 65 if (solve(1)) 66 return; 67 }else 68 { 69 if (solve(0)) 70 return; 71 } 72 } 73 } 74 int main() 75 { 76 for (scanf("%d",&T);T;T--) 77 { 78 scanf("%d",&n); 79 n--; 80 scanf("%d%d",&sx,&sy); 81 scanf("%d%d",&fx,&fy); 82 if (sx == fx && sy == fy) 83 { 84 printf("0\n"); 85 continue; 86 } 87 bfs(); 88 printf("%d\n",ans); 89 } 90 return 0; 91 }
1455
取模
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 typedef long long ll; 5 const int MAXN = 1100000,b = 29,mo = 1000000007; 6 char s1[MAXN],s2[MAXN]; 7 int ans,sum,n,m,T,bb[MAXN],h[MAXN]; 8 int main() 9 { 10 bb[0] = 1; 11 for (int i = 1;i <= 1000000;i++) 12 bb[i] = (ll)bb[i - 1] * b % mo; 13 for (scanf("%d",&T);T;T--) 14 { 15 ans = 0; 16 scanf("%s%s",s1 + 1,s2 + 1); 17 n = strlen(s1 + 1); 18 m = strlen(s2 + 1); 19 h[0] = 0; 20 for (int i = 1;i <= m;i++) 21 h[i] = ((ll)h[i - 1] * b + (s2[i] - 'A' + 1)) % mo; 22 sum = 0; 23 for (int i = 1;i <= n;i++) 24 sum = ((ll)sum * b + (s1[i] - 'A' + 1)) % mo; 25 for (int i = 0;i <= m - n;i++) 26 {//从[1,n] [2,n+1] .....和是s1是否相等 27 ll tmp = h[i + n] - (ll)h[i] * bb[n] % mo; 28 while (tmp < 0) 29 tmp += mo; 30 tmp %= mo; 31 if (sum == tmp) 32 ans++; 33 } 34 printf("%d\n",ans); 35 } 36 return 0; 37 }
自然溢出
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 typedef unsigned long long ull; 5 const int MAXN = 1100000,b = 29; 6 char s1[MAXN],s2[MAXN]; 7 ull ans,sum,n,m,T,bb[MAXN],h[MAXN]; 8 int main() 9 { 10 bb[0] = 1; 11 for (int i = 1;i <= 1000000;i++) 12 bb[i] = bb[i - 1] * b; 13 for (scanf("%d",&T);T;T--) 14 { 15 ans = 0; 16 scanf("%s%s",s1 + 1,s2 + 1); 17 n = strlen(s1 + 1); 18 m = strlen(s2 + 1); 19 h[0] = 0; 20 for (int i = 1;i <= m;i++) 21 h[i] = h[i - 1] * b + s2[i] - 'A' + 1; 22 sum = 0; 23 for (int i = 1;i <= n;i++) 24 sum = sum * b + s1[i] - 'A' + 1; 25 for (int i = 0;i <= m - n;i++) 26 {//从[1,n] [2,n+1] .....和是s1是否相等 27 if (sum == h[i + n] - h[i] * bb[n]) 28 ans++; 29 } 30 printf("%d\n",ans); 31 } 32 return 0; 33 }
1456
1 #include <map> 2 #include <iostream> 3 #include <string> 4 #include <cstdio> 5 using namespace std; 6 int m; 7 map <string,bool> mp; 8 int main() 9 { 10 scanf("%d",&m); 11 for(int i = 1;i <= m;i++) 12 { 13 string op,str; 14 char c; 15 cin >> op; 16 c = getchar(); 17 getline(cin,str); 18 if (op == "find") 19 { 20 if (mp[str] == true) 21 printf("yes\n"); 22 else 23 printf("no\n"); 24 }else 25 { 26 mp[str] = true; 27 } 28 } 29 return 0; 30 }
1471
1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 using namespace std; 5 int tot,trie[110000][12],T,n; 6 string str[11000]; 7 bool insert(string s) 8 { 9 int p = 1; 10 bool suc = true; 11 for (int i = 0;i < s.length();i++) 12 { 13 if (trie[p][s[i] - '0']) 14 p = trie[p][s[i] - '0']; 15 else 16 { 17 trie[p][s[i] - '0'] = ++tot; 18 p = tot; 19 suc = false; 20 } 21 } 22 return suc; 23 } 24 bool cmp(string a,string b) 25 { 26 return a.length() > b.length(); 27 } 28 int main() 29 { 30 for (scanf("%d",&T);T;T--) 31 { 32 for (int i = 1;i <= tot;i++) 33 for (int j = 0;j <= 9;j++) 34 trie[i][j] = 0; 35 tot = 1; 36 bool suc = false; 37 scanf("%d",&n); 38 for (int i = 1;i <= n;i++) 39 { 40 cin >> str[i]; 41 } 42 sort(str + 1,str + n + 1,cmp); 43 for (int i = 1;i <= n;i++) 44 if (insert(str[i])) 45 { 46 suc = true; 47 break; 48 } 49 if (suc == false) 50 printf("YES\n"); 51 else 52 printf("NO\n"); 53 } 54 return 0; 55 }
1472
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 const int MAXN = 110000; 5 int tot = 1,n,trie[MAXN * 32][2],a[MAXN]; 6 void insert(int x) 7 { 8 int u = 1; 9 for (int i = 31;i >= 0;i--) 10 { 11 int t = (x >> i) & 1; 12 if (trie[u][t] == 0) 13 { 14 trie[u][t] = ++tot; 15 u = trie[u][t]; 16 }else 17 u = trie[u][t]; 18 } 19 } 20 int find(int x) 21 { 22 int u = 1,res = 0; 23 for (int i = 31;i >= 0;i--) 24 { 25 int t = (x >> i) & 1; 26 if (trie[u][t ^ 1]) 27 { 28 u = trie[u][t ^ 1]; 29 res = (res << 1) | 1; 30 }else 31 { 32 u = trie[u][t]; 33 res = (res << 1); 34 } 35 } 36 return res; 37 } 38 int main() 39 { 40 scanf("%d",&n); 41 for (int i = 1;i <= n;i++) 42 { 43 scanf("%d",&a[i]); 44 insert(a[i]); 45 } 46 int res = 0; 47 for (int i = 1;i <= n;i++) 48 { 49 res = max(res,find(a[i])); 50 } 51 printf("%d\n",res); 52 return 0; 53 }
1473
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 const int MAXN = 410000; 5 int tot = 1,n,trie[MAXN * 32][2],a[MAXN],m1[MAXN],m2[MAXN]; 6 void insert(int x) 7 { 8 int u = 1; 9 for (int i = 31;i >= 0;i--) 10 { 11 int t = (x >> i) & 1; 12 if (trie[u][t] == 0) 13 { 14 trie[u][t] = ++tot; 15 u = trie[u][t]; 16 }else 17 u = trie[u][t]; 18 } 19 } 20 int find(int x) 21 { 22 int u = 1,res = 0; 23 for (int i = 31;i >= 0;i--) 24 { 25 int t = (x >> i) & 1; 26 if (trie[u][t ^ 1]) 27 { 28 u = trie[u][t ^ 1]; 29 res = (res << 1) | 1; 30 }else 31 { 32 u = trie[u][t]; 33 res = (res << 1); 34 } 35 } 36 return res; 37 } 38 int main() 39 { 40 scanf("%d",&n); 41 for (int i = 1;i <= n;i++) 42 scanf("%d",&a[i]); 43 insert(0); 44 int sum = 0; 45 for (int i = 1;i <= n;i++) 46 { 47 sum ^= a[i]; 48 m1[i] = max(m1[i - 1],find(sum)); 49 insert(sum); 50 } 51 52 for (int i = 1;i <= tot;i++) 53 trie[i][0] = trie[i][1] = 0; 54 tot = 1; 55 56 insert(0); 57 sum = 0; 58 for (int i = n;i >= 1;i--) 59 { 60 sum ^= a[i]; 61 m2[i] = max(m2[i + 1],find(sum)); 62 insert(sum); 63 } 64 int res = 0; 65 for (int i = 1;i <= n - 1;i++) 66 res = max(res,m1[i] + m2[i + 1]); 67 printf("%d\n",res); 68 }
1486(loj10064)
1 #include <cstdio> 2 #include <algorithm> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 const int MAXN = 1100,MAXM = 1100000; 7 bool inq[MAXN]; 8 int dis[MAXN],mp[MAXN][MAXN],n,m; 9 int head[MAXN]; 10 int to[MAXM],nxt[MAXM],val[MAXM]; 11 int cnt; 12 void add(int x,int y,int v) 13 { 14 nxt[++cnt] = head[x]; 15 to[cnt] = y; 16 val[cnt] = v; 17 head[x] = cnt; 18 } 19 void spfa(int s) 20 { 21 memset(inq,0,sizeof(inq)); 22 memset(dis,0x1f,sizeof(dis)); 23 queue <int> que; 24 que.push(s); 25 dis[s] = 0; 26 inq[s] = 1; 27 28 while (!que.empty()) 29 { 30 int tx = que.front(); 31 que.pop(); 32 inq[tx] = false; 33 for (int i = head[tx];i;i = nxt[i]) 34 { 35 if (dis[to[i]] > dis[tx] + val[i]) 36 { 37 dis[to[i]] = dis[tx] + val[i]; 38 if (inq[to[i]] == false) 39 { 40 que.push(to[i]); 41 inq[to[i]] = true; 42 } 43 } 44 } 45 } 46 return; 47 } 48 49 int main() 50 { 51 scanf("%d%d",&n,&m); 52 int tx,ty,tv; 53 memset(mp,0x1f,sizeof(mp)); 54 for (int i = 1;i <= m;i++) 55 { 56 scanf("%d%d%d",&tx,&ty,&tv); 57 add(tx,ty,tv); 58 add(ty,tx,tv); 59 mp[tx][ty] = mp[ty][tx] = tv; 60 } 61 spfa(1); 62 long long ans = 1; 63 for (int i = 2;i <= n;i++) 64 { 65 int sum = 0; 66 for (int j = 1;j <= n;j++) 67 { 68 if (dis[j] + mp[j][i] == dis[i]) 69 { 70 sum++; 71 } 72 } 73 if (sum != 0) 74 { 75 ans = ans * sum % ((1 << 31) - 1); 76 } 77 } 78 printf("%d\n",ans); 79 }
1487(loj10065,输入格式有微小差距)
1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 using namespace std; 5 struct edg 6 { 7 int x,y; 8 double dis; 9 } vec[11000]; 10 int fa[120],siz[120]; 11 bool cmp(edg a,edg b) 12 { 13 return a.dis < b.dis; 14 } 15 void pre_dsu(int n) 16 { 17 for (int i = 1;i <= n;i++) 18 fa[i] = i,siz[i] = 1; 19 } 20 int getfa(int u) 21 { 22 if (fa[u] == u) return u; 23 return fa[u] = getfa(fa[u]); 24 } 25 void merge(int u,int v) 26 { 27 int s1 = getfa(u),s2 = getfa(v); 28 if(s1 == s2) return; 29 if (siz[s1] < siz[s2]) 30 { 31 fa[s1] = s2; 32 siz[s2] += siz[s1]; 33 }else 34 { 35 fa[s2] = s1; 36 siz[s1] += siz[s2]; 37 } 38 } 39 int cnt,sum,k,n; 40 double x[120],y[120],mp[120][120],ans; 41 int main() 42 { 43 scanf("%d%d",&n,&k); 44 pre_dsu(n); 45 for (int i = 1;i <= n;i++) 46 { 47 scanf("%lf%lf",&x[i],&y[i]); 48 } 49 for (int i = 1;i <= n;i++) 50 for (int j = 1;j <= n;j++) 51 { 52 ++cnt; 53 vec[cnt].x = i; 54 vec[cnt].y = j; 55 mp[i][j] = vec[cnt].dis = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); 56 } 57 sort(vec + 1,vec + cnt + 1,cmp); 58 for (int i = 1;i <= cnt;i++) 59 { 60 if (getfa(vec[i].x) == getfa(vec[i].y)) 61 continue; 62 merge(vec[i].x,vec[i].y); 63 ans = vec[i].dis; 64 sum++; 65 if (sum == n - k) 66 break; 67 } 68 printf("%.2lf\n",ans); 69 }
1496(loj10074)
1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 const int MAXN = 510000,MAXM = 1010000; 7 struct pot 8 { 9 int x,dis; 10 pot(int _x = 0,int _dis = 0) :x(_x),dis(_dis){} 11 friend bool operator < (pot a,pot b) 12 { 13 return a.dis > b.dis; 14 } 15 }; 16 priority_queue <pot> que; 17 bool vis[MAXN]; 18 int head[MAXN],dis[MAXN]; 19 int to[MAXM],nxt[MAXM],val[MAXM]; 20 int cnt,n,m,k; 21 void add(int x,int y,int v) 22 { 23 nxt[++cnt] = head[x]; 24 to[cnt] = y; 25 val[cnt] = v; 26 head[x] = cnt; 27 } 28 int dij(int s,int mid) 29 { 30 while (!que.empty()) que.pop(); 31 que.push(pot(s,0)); 32 memset(dis,0x1f,sizeof(dis)); 33 memset(vis,0,sizeof(vis)); 34 dis[s] = 0; 35 while (!que.empty()) 36 { 37 int u = que.top().x; 38 que.pop(); 39 if (vis[u]) continue; 40 vis[u] = true; 41 for (int i = head[u];i;i = nxt[i]) 42 { 43 int tv = 0; 44 if(val[i] > mid) tv = 1; 45 else tv = 0; 46 if (dis[to[i]] > dis[u] + tv) 47 { 48 dis[to[i]] = dis[u] + tv; 49 que.push(pot(to[i],dis[to[i]])); 50 } 51 } 52 } 53 return dis[n]; 54 } 55 56 int main() 57 { 58 scanf("%d%d%d",&n,&m,&k); 59 int tx,ty,tv; 60 for (int i = 1;i <= m;i++) 61 { 62 scanf("%d%d%d",&tx,&ty,&tv); 63 add(tx,ty,tv); 64 add(ty,tx,tv); 65 } 66 int l = 0,r = 1000000000; 67 while (l < r) 68 { 69 int mid = l + r >> 1; 70 if (dij(1,mid) > k) 71 l = mid + 1; 72 else 73 r = mid; 74 } 75 if (l == 1000000000) 76 printf("-1\n"); 77 else 78 printf("%d\n",l); 79 }
1505(loj10083)
1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 int n,m,S,T,cnt,ans; 6 bool inq[110][11000]; 7 int dis[110][11000]; 8 int head[110],to[1100],nxt[1100],tim[1100],val[1100]; 9 struct dat 10 { 11 int pos; 12 int val; 13 dat (int _pos = 0,int _val = 0):pos(_pos),val(_val){} 14 }; 15 void add(int x,int y,int c,int t) 16 { 17 nxt[++cnt] = head[x]; 18 to[cnt] = y; 19 val[cnt] = c; 20 tim[cnt] = t; 21 head[x] = cnt; 22 } 23 void spfa() 24 { 25 queue <dat> que; 26 memset(dis,0x1f,sizeof(dis)); 27 que.push(dat(S,0)); 28 inq[S][0] = true; 29 dis[S][0] = 0; 30 while (que.size() != 0) 31 { 32 dat x = que.front(); 33 que.pop(); 34 inq[x.pos][x.val] = false; 35 for (int i = head[x.pos];i;i = nxt[i]) 36 { 37 int v = to[i]; 38 if (x.val + val[i] > n * 100) 39 continue; 40 if (dis[v][x.val + val[i]] > dis[x.pos][x.val] + tim[i]) 41 { 42 dis[v][x.val + val[i]] = dis[x.pos][x.val] + tim[i]; 43 if (!inq[v][x.val + val[i]]) 44 { 45 que.push(dat(v,x.val + val[i])); 46 inq[v][x.val + val[i]] = true; 47 } 48 } 49 } 50 } 51 } 52 int main() 53 { 54 scanf("%d%d%d%d",&n,&m,&S,&T); 55 int ta,tb,tc,tt; 56 for (int i = 1;i <= m;i++) 57 { 58 scanf("%d%d%d%d",&ta,&tb,&tc,&tt); 59 add(ta,tb,tc,tt); 60 add(tb,ta,tc,tt); 61 } 62 spfa(); 63 int minn = dis[0][0]; 64 for (int i = 0;i <= n * 100;i++) 65 { 66 if (dis[T][i] == dis[0][0] || dis[T][i] >= minn) 67 continue; 68 minn = dis[T][i]; 69 ans++; 70 } 71 printf("%d\n",ans); 72 return 0; 73 }
1526(loj10104)
1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 char str[1100]; 5 int T,n,tot,c0,c1,S; 6 int ind[30],outd[30],mp[30][30]; 7 bool vis[30],suc; 8 void dfs(int u) 9 { 10 for (int i = 1;i <= 26;i++) 11 { 12 if (mp[u][i] > 0) 13 { 14 mp[u][i] -= 1; 15 dfs(i); 16 } 17 } 18 tot++; 19 } 20 int main() 21 { 22 for (scanf("%d",&T);T;T--) 23 { 24 25 memset(ind,0,sizeof(ind)); 26 memset(outd,0,sizeof(outd)); 27 memset(mp,0,sizeof(mp)); 28 memset(vis,0,sizeof(vis)); 29 c0 = c1 = S = 0; 30 suc = true; 31 scanf("%d",&n); 32 for (int i = 1;i <= n;i++) 33 { 34 scanf("%s",str + 1); 35 int x = str[1] - 'a' + 1; 36 int y = str[strlen(str + 1)] - 'a' + 1; 37 mp[x][y] += 1; 38 outd[x] += 1; 39 ind[y] += 1; 40 vis[x] = true; 41 vis[y] = true; 42 } 43 for (int i = 1;i <= 26;i++) 44 { 45 if (outd[i] - ind[i] == 1) 46 { 47 S = i; 48 c0 += 1; 49 }else if (ind[i] - outd[i] == 1) 50 { 51 c1 += 1; 52 }else if (ind[i] == outd[i]) 53 { 54 //空着 55 }else 56 { 57 suc = false; 58 } 59 } 60 if (c0 == 0 && c1 == 0) 61 { 62 for (int i = 1;i <= 26;i++) 63 { 64 if (vis[i] == true) 65 { 66 S = i; 67 } 68 } 69 }else if (c0 == 1 && c1 == 1) 70 { 71 // 72 }else 73 { 74 suc = false; 75 } 76 if (suc == true) 77 { 78 tot = 0; 79 dfs(S); 80 if (tot == n + 1) 81 { 82 printf("Ordering is possible.\n"); 83 }else 84 { 85 printf("The door cannot be opened.\n"); 86 } 87 }else 88 { 89 printf("The door cannot be opened.\n"); 90 } 91 } 92 }
1527(loj10105)
1 #include <cstdio> 2 #include <cstring> 3 #include <stack> 4 #include <vector> 5 using namespace std; 6 stack <int> stk; 7 vector <int> vec; 8 const int MAXN = 510000; 9 int t,n,m,cnt; 10 int head[MAXN],to[MAXN],nxt[MAXN],ind[MAXN],outd[MAXN],d[MAXN]; 11 void del(int x,int i) 12 { 13 if (head[x] == i) 14 { 15 head[x] = nxt[i]; 16 return; 17 } 18 //head[x] != i 19 int v = head[x]; 20 for (int o = head[x];o;o = nxt[o]) 21 { 22 if (i == o) 23 { 24 nxt[v] = nxt[o]; 25 return; 26 } 27 v = o; 28 } 29 30 } 31 void dfs1(int x) 32 { 33 while (head[x] != 0) 34 { 35 int i = head[x]; 36 del(x,i); 37 del(to[i],i^1); 38 dfs1(to[i]); 39 stk.push(i); 40 } 41 } 42 void dfs2(int x) 43 { 44 while (head[x] != 0) 45 { 46 int i = head[x]; 47 del(x,i); 48 dfs2(to[i]); 49 stk.push(i); 50 } 51 } 52 void add2(int x,int y) 53 { 54 nxt[++cnt] = head[x]; 55 to[cnt] = y; 56 head[x] = cnt; 57 outd[x]++; 58 ind[y]++; 59 } 60 void add1(int x,int y) 61 { 62 nxt[++cnt] = head[x]; 63 to[cnt] = y; 64 head[x] = cnt; 65 d[x]++; 66 } 67 int main() 68 { 69 scanf("%d%d%d",&t,&n,&m); 70 if (t == 1) 71 { 72 cnt = 1; 73 int ta,tb; 74 for (int i = 1;i <= m;i++) 75 { 76 scanf("%d%d",&ta,&tb); 77 add1(ta,tb);//偶数那条 和原图方向一致 78 add1(tb,ta); //奇数 和原图方向相反 79 } 80 for (int i = 1;i <= n;i++) 81 if (d[i] % 2 == 1) 82 { 83 printf("NO\n"); 84 return 0; 85 } 86 for (int i = 1;i <= n;i++) 87 { 88 if (d[i] == 0) 89 continue; 90 // 91 dfs1(i); 92 if (stk.size() == m) 93 break; 94 else 95 { 96 printf("NO\n"); 97 return 0; 98 } 99 } 100 printf("YES\n"); 101 while (!stk.empty()) 102 { 103 printf("%d ",((stk.top() % 2) ? -((stk.top() - 1)/2) : stk.top() / 2)); 104 stk.pop(); 105 } 106 }else 107 { 108 cnt = 0; 109 int ta,tb; 110 for (int i = 1;i <= m;i++) 111 { 112 scanf("%d%d",&ta,&tb); 113 add2(ta,tb); 114 } 115 for (int i = 1;i <= n;i++) 116 { 117 if (ind[i] != outd[i]) 118 { 119 printf("NO\n"); 120 return 0; 121 } 122 } 123 for (int i = 1;i <= n;i++) 124 { 125 if (ind[i] == 0 && outd[i] == 0) 126 continue; 127 dfs2(i); 128 if (stk.size() == m) 129 break; 130 else 131 { 132 printf("NO\n"); 133 return 0; 134 } 135 } 136 printf("YES\n"); 137 while (!stk.empty()) 138 { 139 vec.push_back(stk.top()); 140 stk.pop(); 141 } 142 for (int i = 0;i < vec.size();i++) 143 printf("%d ",vec[i]); 144 } 145 return 0; 146 }
1535
1 #include <cstdio> 2 using namespace std; 3 const int MAXN = 110000; 4 int n,m,a[MAXN],t[MAXN]; 5 int lowbit(int x) 6 { 7 return x & (-x); 8 } 9 int add(int x,int k) 10 { 11 for (int i = x;i <= n;i += lowbit(i)) 12 t[i] += k; 13 } 14 int sum(int x) 15 { 16 int ans = 0; 17 for (int i = x;i >= 1;i -= lowbit(i)) 18 ans += t[i]; 19 return ans; 20 } 21 int main() 22 { 23 scanf("%d%d",&n,&m); 24 for(int i = 1;i <= n;i++) 25 { 26 scanf("%d",&a[i]); 27 add(i,a[i]); 28 } 29 int tk,tl,tr; 30 for (int i = 1;i <= m;i++) 31 { 32 scanf("%d%d%d",&tk,&tl,&tr); 33 if (tk == 0) 34 printf("%d\n",sum(tr) - sum(tl) + a[tl]); 35 else if (tk == 1) 36 { 37 add(tl,tr); 38 a[tl] += tr; 39 } 40 } 41 return 0; 42 }
1536
1 #include <cstdio> 2 using namespace std; 3 const int MAXN = 110000,maxn = 33000; 4 int n,m,a[MAXN],t[MAXN],tot[MAXN]; 5 int lowbit(int x) 6 { 7 return x & (-x); 8 } 9 int add(int x,int k) 10 { 11 for (int i = x;i <= maxn;i += lowbit(i)) 12 t[i] += k; 13 } 14 int sum(int x) 15 { 16 int ans = 0; 17 for (int i = x;i >= 1;i -= lowbit(i)) 18 ans += t[i]; 19 return ans; 20 } 21 int main() 22 { 23 scanf("%d",&n); 24 int tx,ty,ans; 25 for (int i = 1;i <= n;i++) 26 { 27 scanf("%d%d",&tx,&ty); 28 tx += 1; 29 ans = sum(tx); 30 add(tx,1); 31 tot[ans] += 1; 32 } 33 for (int i = 0;i < n;i++) 34 printf("%d\n",tot[i]); 35 }
1537
1 #include <cstdio> 2 using namespace std; 3 int n,m; 4 struct bit 5 { 6 int t[51000],n; 7 int lowbit(int x) 8 { 9 return x & (-x); 10 } 11 int add(int x,int k) 12 { 13 for (int i = x;i >= 1;i -= lowbit(i)) 14 t[i] += k; 15 } 16 int sum(int x) 17 { 18 int ans = 0; 19 for (int i = x;i <= n;i += lowbit(i)) 20 ans += t[i]; 21 return ans; 22 } 23 } T1,T2; 24 int main() 25 { 26 scanf("%d%d",&n,&m); 27 T1.n = T2.n = n; 28 int opt,tx,ty; 29 for (int i = 1;i <= m;i++) 30 { 31 scanf("%d%d%d",&opt,&tx,&ty); 32 if(opt == 1) 33 { 34 T1.add(ty,1); 35 T2.add(tx,1); 36 }else if (opt == 2) 37 { 38 printf("%d\n",T1.sum(tx) - T2.sum(ty + 1)); 39 } 40 } 41 return 0; 42 }
1538
1 #include <cstdio> 2 using namespace std; 3 int n,k,t[1100000]; 4 int lowbit(int x) 5 { 6 return x & (-x); 7 } 8 int add(int x,int k) 9 { 10 for (int i = x;i <= n;i += lowbit(i)) 11 t[i] += k; 12 } 13 int sum(int x) 14 { 15 int ans = 0; 16 for (int i = x;i >= 1;i -= lowbit(i)) 17 ans += t[i]; 18 return ans; 19 } 20 int main() 21 { 22 scanf("%d%d",&n,&k); 23 char opt[10]; 24 int ta,tb; 25 for (int i = 1;i <= k;i++) 26 { 27 scanf("%s",opt); 28 if (opt[0] == 'A') 29 { 30 scanf("%d",&ta); 31 printf("%d\n",sum(ta)); 32 }else if (opt[0] == 'B') 33 { 34 scanf("%d%d",&ta,&tb); 35 add(ta,tb); 36 }else if (opt[0] == 'C') 37 { 38 scanf("%d%d",&ta,&tb); 39 add(ta,-tb); 40 } 41 } 42 return 0; 43 }
1539
1 #include <cstdio> 2 using namespace std; 3 int n,m,t[1100000]; 4 int lowbit(int x) 5 { 6 return x & (-x); 7 } 8 int add(int x,int k) 9 { 10 for (int i = x;i <= n;i += lowbit(i)) 11 t[i] += k; 12 } 13 int sum(int x) 14 { 15 int ans = 0; 16 for (int i = x;i >= 1;i -= lowbit(i)) 17 ans += t[i]; 18 return ans; 19 } 20 int main() 21 { 22 scanf("%d%d",&n,&m); 23 n += 1; 24 int opt,tl,tr; 25 for (int i = 1;i <= m;i++) 26 { 27 scanf("%d",&opt,&tl,&tr); 28 if (opt == 1) 29 { 30 scanf("%d%d",&tl,&tr); 31 add(tl,1); 32 add(tr + 1,-1); 33 }else if (opt == 2) 34 { 35 scanf("%d",&tl); 36 printf("%d\n",sum(tl) % 2); 37 } 38 } 39 return 0; 40 }
1541
1 #include <cmath> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 int n,m,t,a[110000],p[20][110000]; 6 int main() 7 { 8 scanf("%d%d",&n,&m); 9 for (int i = 1;i <= n;i++) 10 scanf("%d",&a[i]); 11 for (int i = 1;i <= n;i++) 12 p[0][i] = a[i]; 13 t = log2(n); 14 for (int i = 1;i <= t;i++) 15 { 16 for (int j = 1;j + (1 << i) - 1 <= n;j++) 17 { 18 p[i][j] = max(p[i - 1][j],p[i - 1][j + (1 << (i - 1))]); 19 } 20 } 21 int tl,tr,tt; 22 for (int i = 1;i <= m;i++) 23 { 24 scanf("%d%d",&tl,&tr); 25 tt = log2(tr - tl + 1); 26 printf("%d\n",max(p[tt][tl],p[tt][tr - (1 << tt) + 1])); 27 } 28 return 0; 29 }
1547
1 #include <cstdio> 2 using namespace std; 3 long long tre[410000],n,m; 4 void add(int k,int l,int r,int x,long long ad) 5 { 6 if (l == r) 7 { 8 tre[k] += ad; 9 return; 10 } 11 int mid = l + r >> 1; 12 if (x <= mid) 13 add(k << 1,l,mid,x,ad); 14 else 15 add(k << 1 | 1,mid + 1,r,x,ad); 16 tre[k] = tre[k << 1] + tre[k << 1 | 1]; 17 } 18 long long query(int k,int l,int r,int x,int y) 19 { 20 if (x <= l && r <= y) 21 { 22 return tre[k]; 23 } 24 int mid = l + r >> 1; 25 long long sum = 0; 26 if (x <= mid) 27 sum += query(k << 1,l,mid,x,y); 28 if (y >= mid + 1) 29 sum += query(k << 1 | 1,mid + 1,r,x,y); 30 return sum; 31 } 32 int main() 33 { 34 scanf("%lld%lld",&n,&m); 35 long long opt,tl,tr; 36 for (int i = 1;i <= m;i++) 37 { 38 scanf("%lld%lld%lld",&opt,&tl,&tr); 39 if (opt == 1) 40 { 41 printf("%lld\n",query(1,1,n,tl,tr)); 42 }else 43 { 44 add(1,1,n,tl,tr); 45 } 46 } 47 }
1548
1 #include <cstdio> 2 using namespace std; 3 typedef long long ll; 4 ll sum[5000000],lzy[5000000],vec[5000000]; 5 int n,m; 6 void down(int k,int l,int r) 7 { 8 if (l == r) 9 { 10 lzy[k] = 0; 11 return; 12 } 13 int mid = l + r >> 1; 14 lzy[k * 2] += lzy[k]; 15 lzy[k * 2 + 1] += lzy[k]; 16 sum[k * 2] += (mid - l + 1) * lzy[k]; 17 sum[k * 2 + 1] += (r - mid) * lzy[k]; 18 lzy[k] = 0; 19 return; 20 } 21 void build(int k,int l,int r) 22 { 23 if (l == r) 24 { 25 sum[k] = vec[l]; 26 return; 27 } 28 int mid = l + r >> 1; 29 build(k << 1,l,mid); 30 build(k << 1 | 1,mid + 1,r); 31 sum[k] = sum[k << 1] + sum[k << 1 | 1]; 32 return; 33 } 34 35 void vec_add(int k,int l,int r,int x,int y,ll ad) 36 { 37 if (lzy[k] != 0) 38 down(k,l,r); 39 if (x <= l && r <= y) 40 { 41 lzy[k] += ad; 42 sum[k] += (r - l + 1) * ad; 43 return; 44 } 45 int mid = l + r >> 1; 46 if (x <= mid) 47 vec_add(k << 1,l,mid,x,y,ad); 48 if (y >= mid + 1) 49 vec_add(k << 1 | 1,mid + 1,r,x,y,ad); 50 sum[k] = sum[k << 1] + sum[k << 1 | 1]; 51 } 52 ll vec_query(int k,int l,int r,int x,int y) 53 { 54 if (lzy[k] != 0) 55 down(k,l,r); 56 if (x <= l && r <= y) 57 return sum[k]; 58 ll tot = 0; 59 int mid = l + r >> 1; 60 if (x <= mid) 61 tot += vec_query(k << 1,l,mid,x,y); 62 if (y >= mid + 1) 63 tot += vec_query(k << 1 | 1,mid + 1,r,x,y); 64 return tot; 65 } 66 67 int main() 68 { 69 scanf("%d%d",&n,&m); 70 for (int i = 1;i <= n;i++) 71 scanf("%lld",&vec[i]); 72 build(1,1,n); 73 int opt,tx,ty; 74 ll tv; 75 for (int i = 1;i <= m;i++) 76 { 77 scanf("%d",&opt); 78 if (opt == 1) 79 { 80 scanf("%d%d%lld",&tx,&ty,&tv); 81 vec_add(1,1,n,tx,ty,tv); 82 }else 83 { 84 scanf("%d%d",&tx,&ty); 85 printf("%lld\n",vec_query(1,1,n,tx,ty)); 86 } 87 } 88 return 0; 89 }
1549
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 typedef long long ll; 5 ll sum[5000000],vec[5000000]; 6 int n,p,len; 7 8 void vec_add(int k,int l,int r,int x,ll ad) 9 { 10 if (l == r) 11 { 12 sum[k] = ad; 13 return; 14 } 15 int mid = l + r >> 1; 16 if (x <= mid) 17 vec_add(k << 1,l,mid,x,ad); 18 if (x >= mid + 1) 19 vec_add(k << 1 | 1,mid + 1,r,x,ad); 20 sum[k] = max(sum[k << 1],sum[k << 1 | 1]); 21 } 22 ll vec_query(int k,int l,int r,int x,int y) 23 { 24 if (x <= l && r <= y) 25 return sum[k]; 26 ll tot = 0; 27 int mid = l + r >> 1; 28 if (x <= mid) 29 tot = max(tot,vec_query(k << 1,l,mid,x,y)); 30 if (y >= mid + 1) 31 tot = max(tot,vec_query(k << 1 | 1,mid + 1,r,x,y)); 32 return tot; 33 } 34 35 int main() 36 { 37 scanf("%d%d",&n,&p); 38 char str[10]; 39 int tx,last = 0; 40 for (int i = 1;i <= n;i++) 41 { 42 scanf("%s",str); 43 if (str[0] == 'A') 44 { 45 scanf("%d",&tx); 46 tx = (tx + last) % p; 47 len++; 48 vec_add(1,1,200000,len,tx); 49 }else 50 { 51 scanf("%d",&tx); 52 printf("%d\n",last = vec_query(1,1,200000,len - tx + 1,len)); 53 } 54 } 55 return 0; 56 }
1550
1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 using namespace std; 5 long long vec[410000],maxn[410000],tre[410000]; 6 int n,m; 7 void build(int k,int l,int r) 8 { 9 if (l == r) 10 { 11 tre[k] = vec[l]; 12 maxn[k] = vec[l]; 13 return; 14 } 15 int mid = l + r >> 1; 16 build(k << 1,l,mid); 17 build(k << 1 | 1,mid + 1,r); 18 tre[k] = tre[k << 1] + tre[k << 1 | 1]; 19 maxn[k] = max(maxn[k << 1],maxn[k << 1 | 1]); 20 } 21 void change(int k,int l,int r,int x,int y) 22 { 23 if (maxn[k] <= 1) 24 return; 25 if (l == r) 26 { 27 tre[k] = sqrt(tre[k]); 28 maxn[k] = sqrt(maxn[k]); 29 return; 30 } 31 int mid = l + r >> 1; 32 if (x <= mid) 33 change(k << 1,l,mid,x,y); 34 if (y >= mid + 1) 35 change(k << 1 | 1,mid + 1,r,x,y); 36 tre[k] = tre[k << 1] + tre[k << 1 | 1]; 37 maxn[k] = max(maxn[k << 1],maxn[k << 1 | 1]); 38 } 39 long long query(int k,int l,int r,int x,int y) 40 { 41 if (x <= l && r <= y) 42 { 43 return tre[k]; 44 } 45 int mid = l + r >> 1; 46 long long sum = 0; 47 if (x <= mid) 48 sum += query(k << 1,l,mid,x,y); 49 if (y >= mid + 1) 50 sum += query(k << 1 | 1,mid + 1,r,x,y); 51 return sum; 52 } 53 int main() 54 { 55 scanf("%d",&n); 56 for (int i = 1;i <= n;i++) 57 scanf("%lld",&vec[i]); 58 build(1,1,n); 59 scanf("%d",&m); 60 int tl,tr,opt; 61 for (int i = 1;i <= m;i++) 62 { 63 scanf("%d%d%d",&opt,&tl,&tr); 64 if (opt == 1) 65 { 66 printf("%lld\n",query(1,1,n,tl,tr)); 67 }else 68 { 69 change(1,1,n,tl,tr); 70 } 71 } 72 }
1551
1 #include <cstdio> 2 using namespace std; 3 typedef long long ll; 4 int n,m; 5 ll p; 6 ll sum[1000000],lzy[1000000],mul[1000000],vec[1000000]; 7 void build(int k,int l,int r) 8 { 9 mul[k] = 1; 10 if (l == r) 11 { 12 sum[k] = vec[l]; 13 return; 14 } 15 int mid = (l + r) / 2; 16 build(k * 2,l,mid); 17 build(k * 2 + 1,mid + 1,r); 18 sum[k] = sum[k * 2] + sum[k * 2 + 1]; 19 return; 20 } 21 void down(int k,int l,int r) 22 { 23 if (l == r) 24 { 25 lzy[k] = 0; 26 mul[k] = 1; 27 return; 28 } 29 int mid = (l + r) / 2; 30 if (mul[k] != 1) 31 { 32 sum[k * 2] = sum[k * 2] * mul[k] % p; 33 sum[k * 2 + 1] = sum[k * 2 + 1] * mul[k] % p; 34 mul[k * 2] = mul[k * 2] * mul[k] % p; 35 mul[k * 2 + 1] = mul[k * 2 + 1] * mul[k] % p; 36 lzy[k * 2] = lzy[k * 2] * mul[k] % p; 37 lzy[k * 2 + 1] = lzy[k * 2 + 1] * mul[k] % p; 38 mul[k] = 1; 39 } 40 if (lzy[k] != 0) 41 { 42 sum[k * 2] = (sum[k * 2] + lzy[k] * (mid - l + 1)) % p; 43 sum[k * 2 + 1] = (sum[k * 2 + 1] + lzy[k] * (r - mid)) % p; 44 lzy[k * 2] = (lzy[k * 2] + lzy[k]) % p; 45 lzy[k * 2 + 1] = (lzy[k * 2 + 1] + lzy[k]) % p; 46 lzy[k] = 0; 47 } 48 return; 49 } 50 void vec_add(int k,int l,int r,int x,int y,ll ad) 51 { 52 if (lzy[k] != 0 || mul[k] != 1) 53 down(k,l,r); 54 if (x <= l && r <= y) 55 { 56 lzy[k] = (lzy[k] + ad) % p; 57 sum[k] = (sum[k] + ad * (r - l + 1)) % p; 58 return; 59 } 60 int mid = (l + r) / 2; 61 if (x <= mid) 62 vec_add(k * 2,l,mid,x,y,ad); 63 if (y >= mid + 1) 64 vec_add(k * 2 + 1,mid + 1,r,x,y,ad); 65 sum[k] = (sum[k * 2] + sum[k * 2 + 1]) % p; 66 return; 67 } 68 void vec_mul(int k,int l,int r,int x,int y,ll mu) 69 { 70 if (lzy[k] != 0 || mul[k] != 1) 71 down(k,l,r); 72 if (x <= l && r <= y) 73 { 74 mul[k] = mul[k] * mu % p; 75 lzy[k] = lzy[k] * mu % p; 76 sum[k] = sum[k] * mu % p; 77 return; 78 } 79 int mid = (l + r) / 2; 80 if (x <= mid) 81 vec_mul(k * 2,l,mid,x,y,mu); 82 if (y >= mid + 1) 83 vec_mul(k * 2 + 1,mid + 1,r,x,y,mu); 84 sum[k] = (sum[k * 2] + sum[k * 2 + 1]) % p; 85 } 86 ll vec_query(int k,int l,int r,int x,int y) 87 { 88 if (lzy[k] != 0 || mul[k] != 1) 89 down(k,l,r); 90 if (x <= l && r <= y) 91 return sum[k]; 92 int mid = (l + r) / 2; 93 ll tot = 0; 94 if (x <= mid) 95 tot += vec_query(k * 2,l,mid,x,y); 96 if (y >= mid + 1) 97 tot += vec_query(k * 2 + 1,mid + 1,r,x,y); 98 return tot % p; 99 } 100 int main() 101 { 102 scanf("%d%lld",&n,&p); 103 for (int i = 1;i <= n;i++) 104 scanf("%lld",&vec[i]); 105 build(1,1,n); 106 int opt,tx,ty; 107 ll tk; 108 scanf("%d",&m); 109 for (int i = 1;i <= m;i++) 110 { 111 scanf("%d",&opt); 112 if (opt == 1) 113 { 114 scanf("%d%d%lld",&tx,&ty,&tk); 115 vec_mul(1,1,n,tx,ty,tk); 116 }else if (opt == 2) 117 { 118 scanf("%d%d%lld",&tx,&ty,&tk); 119 vec_add(1,1,n,tx,ty,tk); 120 }else if (opt == 3) 121 { 122 scanf("%d%d",&tx,&ty); 123 printf("%lld\n",vec_query(1,1,n,tx,ty)); 124 } 125 } 126 return 0; 127 }
1552
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 using namespace std; 5 const int MAXN = 1100000; 6 int t,n,m,cnt; 7 int head[MAXN],to[2 * MAXN],nxt[2 * MAXN],p[MAXN][20],dep[MAXN]; 8 void add(int x,int y) 9 { 10 nxt[++cnt] = head[x]; 11 to[cnt] = y; 12 head[x] = cnt; 13 } 14 void dfs(int x) 15 { 16 for (int i = head[x];i;i = nxt[i]) 17 { 18 if (to[i] == p[x][0]) 19 continue; 20 dep[to[i]] = dep[x] + 1; 21 p[to[i]][0] = x; 22 dfs(to[i]); 23 } 24 } 25 int lca(int x,int y) 26 { 27 if (dep[x] < dep[y]) 28 swap(x,y); 29 for (int i = t;i >= 0;i--) 30 if (dep[p[x][i]] >= dep[y]) 31 x = p[x][i]; 32 if (x == y) 33 return x; 34 for (int i = t;i >= 0;i--) 35 if (p[x][i] != p[y][i]) 36 { 37 x = p[x][i]; 38 y = p[y][i]; 39 } 40 return p[x][0]; 41 } 42 int main() 43 { 44 scanf("%d",&n); 45 t = log2(n); 46 int tx,ty; 47 for (int i = 1;i <= n - 1;i++) 48 { 49 scanf("%d%d",&tx,&ty); 50 add(tx,ty); 51 add(ty,tx); 52 } 53 dep[1] = 1; 54 dfs(1); 55 for (int i = 1;i <= t;i++) 56 for (int j = 1;j <= n;j++) 57 p[j][i] = p[p[j][i - 1]][i - 1]; 58 scanf("%d",&m); 59 for (int i = 1;i <= m;i++) 60 { 61 scanf("%d%d",&tx,&ty); 62 printf("%d\n",dep[tx] + dep[ty] - 2 * dep[lca(tx,ty)]); 63 } 64 }
1569
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 typedef long long ll; 5 int n; 6 ll vec[220],sum[220],res0,res1,dp[220][220][2]; 7 ll dfs(int l,int r,int opt) 8 { 9 if (dp[l][r][opt] != 0 || l == r) 10 return dp[l][r][opt]; 11 if (opt == 0) 12 dp[l][r][opt] = 10000000000000; 13 for (int i = l;i <= r - 1;i++) 14 if (opt == 1) 15 dp[l][r][opt] = max(dfs(l,i,opt) + dfs(i + 1,r,opt),dp[l][r][opt]); 16 else 17 dp[l][r][opt] = min(dfs(l,i,opt) + dfs(i + 1,r,opt),dp[l][r][opt]); 18 dp[l][r][opt] += sum[r] - sum[l - 1]; 19 return dp[l][r][opt]; 20 } 21 int main() 22 { 23 scanf("%d",&n); 24 for (int i = 1;i <= n;i++) 25 scanf("%lld",&vec[i]); 26 for (int i = n + 1;i <= 2 * n;i++) 27 vec[i] = vec[i - n]; 28 for (int i = 1;i <= 2 * n;i++) 29 sum[i] = vec[i] + sum[i - 1]; 30 res0 = 10000000000000; 31 res1 = 0; 32 for (int i = 1;i <= n + 1;i++) 33 { 34 res0 = min(res0,dfs(i,i + n - 1,0)); 35 res1 = max(res1,dfs(i,i + n - 1,1)); 36 } 37 printf("%lld\n%lld\n",res0,res1); 38 return 0; 39 }
1575
#include <cstdio> #include <algorithm> using namespace std; int lc[1100],rc[1100],fa[1100],val[1100][1100],f[1100][1100],n,q; int dp(int i,int j) { if (f[i][j]) return f[i][j]; if (j <= 0) return 0; if (lc[i] == 0 && rc[i] == 0) return 0; int ans; for (int k = 0;k <= j;k++) { int tl = dp(lc[i],k - 1); int tr = dp(rc[i],j - k - 1); if (k == 0) ans = tr + val[i][rc[i]]; else if (k == j) ans = tl + val[i][lc[i]]; else ans = tl + tr + val[i][rc[i]] + val[i][lc[i]]; f[i][j] = max(f[i][j],ans); } return f[i][j]; } int main() { scanf("%d%d",&n,&q); int ta,tb,tc; for (int i = 1;i <= n - 1;i++) { scanf("%d%d%d",&ta,&tb,&tc); if (fa[tb]) { fa[ta] = tb; if (lc[tb] == 0) lc[tb] = ta; else rc[tb] = ta; }else { fa[tb] = ta; if (lc[ta] == 0) lc[ta] = tb; else rc[ta] = tb; } val[ta][tb] = tc; val[tb][ta] = tc; } printf("%d\n",dp(1,q)); }
1577
#include <cstdio> #include <algorithm> using namespace std; const int MAXN = 510000; int d[MAXN],ans,cnt,n,sum[MAXN]; int head[MAXN],to[MAXN * 2],nxt[MAXN * 2],val[MAXN * 2]; bool vis[MAXN]; void add(int x,int y,int v) { nxt[++cnt] = head[x]; to[cnt] = y; head[x] = cnt; val[cnt] = v; } void dp(int x) { vis[x] = true; for (int i = head[x];i;i = nxt[i]) { if (vis[to[i]]) continue; int y = to[i],z = val[i]; dp(y); ans = max(ans,d[x] + d[y] + z); d[x] = max(d[x],d[y] + z); } } int main() { scanf("%d",&n); for (int i = 1;i <= n;i++) for (int j = 2;j <= n / i;j++) sum[i * j] += i; for (int i = 2;i <= n;i++) { if (sum[i] >= i) continue; add(i,sum[i],1); add(sum[i],i,1); } dp(1); printf("%d\n",ans); }
1578
#include <cstdio> #include <vector> using namespace std; const int MAXN = 2100; int n,f[MAXN][2]; vector <int> edg[MAXN]; void dfs(int x,int fa) { f[x][0] = 0; f[x][1] = 1; for (int i = 0;i < edg[x].size();i++) { if (edg[x][i] == fa) continue; dfs(edg[x][i],x); f[x][1] += min(f[edg[x][i]][0],f[edg[x][i]][1]); f[x][0] += f[edg[x][i]][1]; } return; } int main() { scanf("%d",&n); for (int i = 0;i < n;i++) { int k,tu,tv; scanf("%d%d",&k,&tu); for (int j = 0;j < tu;j++) { scanf("%d",&tv); edg[k].push_back(tv); edg[tv].push_back(k); } } dfs(0,-1); printf("%d\n",min(f[0][0],f[0][1])); return 0; }
1579
#include <cstdio> #include <algorithm> using namespace std; const int MAXN = 2100; int n,w[MAXN],head[MAXN],cnt,f[MAXN][3],to[MAXN * 2],nxt[MAXN * 2]; void add(int x,int y) { nxt[++cnt] = head[x]; to[cnt] = y; head[x] = cnt; } void dfs(int x,int fa) { f[x][0] = w[x]; int minn = 100000000; for (int i = head[x];i;i = nxt[i]) { if (to[i] == fa) continue; dfs(to[i],x); f[x][0] += min(f[to[i]][0],min(f[to[i]][1],f[to[i]][2])); f[x][1] += min(f[to[i]][0],f[to[i]][1]); minn = min(minn,f[to[i]][0] - min(f[to[i]][0],f[to[i]][1])); f[x][2] += f[to[i]][1]; } f[x][1] += minn; } int main() { scanf("%d",&n); for (int i = 1;i <= n;i++) { int tx,tk,ty; scanf("%d",&tx); scanf("%d",&w[tx]); scanf("%d",&tk); for (int j = 1;j <= tk;j++) { scanf("%d",&ty); add(tx,ty); add(ty,tx); } } dfs(1,-1); printf("%d\n",min(f[1][0],f[1][1])); }
1592
1 #include <cstdio> 2 using namespace std; 3 typedef long long ll; 4 ll n,K,sum,ans,num[310],sta[310],f[30][310][110]; 5 void pre() 6 { 7 for (int i = 0;i <= (1 << n) - 1;i++) 8 { 9 if (i & (i << 1)) 10 continue; 11 sta[++sum] = i; 12 int cnt = 0; 13 for (int j = 0;j < n;j++) 14 if ((1 << j) & i) 15 cnt++; 16 num[sum] = cnt; 17 } 18 } 19 bool war(int x,int y) 20 { 21 if (sta[x] & sta[y]) 22 return true; 23 if (sta[x] & (sta[y] << 1)) 24 return true; 25 if (sta[x] & (sta[y] >> 1)) 26 return true; 27 return false; 28 } 29 int main() 30 { 31 scanf("%d%d",&n,&K); 32 pre(); 33 for (int i = 1;i <= sum;i++) 34 f[1][i][num[i]] = 1; 35 for (int i = 2;i <= n;i++) 36 for (int j = 1;j <= sum;j++) 37 for (int k = 0;k <= K;k++) 38 { 39 if (k < num[j]) 40 continue; 41 for (int l = 1;l <= sum;l++) 42 { 43 if (war(j,l) == false) 44 f[i][j][k] += f[i - 1][l][k-num[j]]; 45 } 46 } 47 for (int i = 1;i <= sum;i++) 48 ans += f[n][i][K]; 49 printf("%lld\n",ans); 50 return 0; 51 }
1593
1 #include <cstdio> 2 using namespace std; 3 const int mo = 100000000; 4 int m,n,cnt,vec[20][5100],dp[20][5100],ans; 5 void count(int a,int t) 6 { 7 cnt = 0; 8 for (int i = 0;i <= (1 << n) - 1;i++) 9 { 10 if ((i & (i << 1)) || (i & (i >> 1)) || (i & t)) 11 continue; 12 vec[a][++cnt] = i; 13 } 14 vec[a][0] = cnt; 15 } 16 int main() 17 { 18 scanf("%d%d",&m,&n); 19 int t =0 ,tx; 20 for (int i = 1;i <= m;i++) 21 { 22 t = 0; 23 for (int j = 1;j <= n;j++) 24 { 25 scanf("%d",&tx); 26 t = (t << 1) + 1 - tx; 27 } 28 count(i,t); 29 } 30 for (int i = 1;i <= vec[1][0];i++) 31 dp[1][i] = 1; 32 for (int i = 2;i <= m;i++) 33 { 34 for (int j = 1;j <= vec[i][0];j++) 35 { 36 for (int k = 1;k <= vec[i - 1][0];k++) 37 { 38 if (vec[i][j] & vec[i - 1][k]) 39 continue; 40 dp[i][j] += dp[i - 1][k]; 41 } 42 } 43 } 44 for (int i = 1;i <= vec[m][0];i++) 45 { 46 ans += dp[m][i]; 47 ans %= mo; 48 } 49 printf("%d\n",ans); 50 return 0; 51 }
NOIO #2
普及组
P6473
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 int n,q; 5 double L,v,a[210000],s[210000],t[210000]; 6 bool cmp(double x,double y) 7 { 8 return x > y; 9 } 10 int main() 11 { 12 scanf("%d%lf%lf",&n,&L,&v); 13 14 for (int i = 1;i <= n;i++) 15 { 16 scanf("%lf",&a[i]); 17 a[i] /= v; 18 } 19 sort(a + 1,a + n + 1,cmp); 20 for (int i = 1;i <= n;i++) 21 { 22 s[i] = s[i - 1] + a[i]; 23 } 24 25 scanf("%d",&q); 26 for (int i = 1;i <= q;i++) 27 { 28 scanf("%lf",&t[i]); 29 } 30 for(int i = 1;i <= q;i++) 31 { 32 if (L / v > t[i]) 33 { 34 printf("0\n"); 35 continue; 36 } 37 if (L / v + s[n] <= t[i] + 0.0000000001) 38 { 39 printf("-1\n"); 40 continue; 41 } 42 int l = 1,r = n; 43 while (l < r) 44 { 45 int mid = l + r >> 1; 46 if (L / v + s[mid] > t[i]) 47 { 48 r = mid; 49 }else 50 { 51 l = mid + 1; 52 } 53 } 54 printf("%d\n",l); 55 } 56 }
P6475
1 #include <cstdio> 2 using namespace std; 3 typedef long long ll; 4 const int mo = 998244353; 5 ll m,n,x,y,ans,jc[210000],ny[210000]; 6 ll fpow(ll x,ll k) 7 { 8 if (k == 0) 9 return 1; 10 ll t = fpow(x,k >> 1); 11 if (k & 1) 12 return t * t % mo * x % mo; 13 return t * t % mo; 14 } 15 ll f(ll a,ll b) 16 { 17 return jc[a+b-1] * ny[a] % mo * ny[b - 1] % mo; 18 } 19 int main() 20 { 21 scanf("%lld%lld%lld%lld",&m,&n,&x,&y); 22 jc[0] = 1; 23 ny[0] = 1; 24 for (ll i = 1;i <= n + m;i++) 25 jc[i] = jc[i - 1] * i % mo; 26 for (ll i = 1;i <= n + m;i++) 27 ny[i] = fpow(jc[i],mo - 2); 28 if (x <= n && y >= n + 1) 29 { 30 for (ll i = 1;i <= m;i++) 31 ans = (ans + f(x-1,i) * f(n-x,m-i+1)% mo * f(y-n-1,m-i+1)%mo*f(n*2-y,i)%mo) % mo; 32 }else 33 { 34 ans = f(n,m) * f(n + x -y,m) % mo; 35 } 36 printf("%lld\n",ans); 37 return 0; 38 }
提高组
P6476
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 typedef long long ll; 5 int T; 6 ll p1,p2,k,G; 7 ll gcd(ll a,ll b) 8 { 9 if (b == 0) 10 return a; 11 return gcd(b,a % b); 12 } 13 int main() 14 { 15 for (scanf("%d",&T);T;T--) 16 { 17 scanf("%lld%lld%lld",&p1,&p2,&k); 18 if (k == 1) 19 { 20 printf("NO\n"); 21 continue; 22 } 23 if (p1 > p2) 24 swap(p1,p2); 25 ll G = gcd(p1,p2); 26 p1 /= G; 27 p2 /= G; 28 if (p2 > 2 && (p2 - 2) / p1 + 1 >= k) 29 printf("NO\n"); 30 else 31 printf("YES\n"); 32 } 33 return 0; 34 }
P6477
1 #include <cstdio> 2 #include <set> 3 using namespace std; 4 int n,A[1100]; 5 set <int> st; 6 long long ans; 7 const int mo = 1000000007; 8 int main() 9 { 10 scanf("%d",&n); 11 for (int i = 1;i <= n;i++) 12 scanf("%d",&A[i]); 13 for (int i = 1;i <= n;i++)//枚举左端点 14 { 15 st.clear(); 16 for (int j = i;j <= n;j++)//枚举右端点 17 { 18 if (st.find(A[j]) == st.end()) 19 st.insert(A[j]); 20 //[i,j-1] 21 //[i,j] 22 23 ans = (ans + st.size() * st.size()) % mo; 24 } 25 } 26 printf("%lld\n",ans); 27 return 0; 28 }
心之所动 且就随缘去吧