A. Alice in the Wonderland

按题意模拟。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<math.h>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. #include<bitset>
  12. #include<algorithm>
  13. #include<time.h>
  14. using namespace std;
  15. void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
  16. #define MS(x, y) memset(x, y, sizeof(x))
  17. #define ls o<<1
  18. #define rs o<<1|1
  19. typedef long long LL;
  20. typedef unsigned long long UL;
  21. typedef unsigned int UI;
  22. template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
  23. template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
  24. const int N = 0, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
  25. template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
  26. int casenum, casei;
  27.  
  28. struct A
  29. {
  30. int x, y, z;
  31. }t, tt, st, ed;
  32. queue<A> q;
  33. int n, m, h;
  34. char s[60][60][60];
  35. bool e[60][60][60];
  36. const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
  37.  
  38. int main()
  39. {
  40. scanf("%d%d%d", &n, &m, &h);
  41. for(int i = 1; i <= h; i ++){
  42. for(int j = 1; j <= n; j ++){
  43. scanf("%s", s[i][j] + 1);
  44. }
  45. }
  46. for(int i = 1; i <= h; i ++){
  47. for(int j = 1; j <= n; j ++){
  48. for(int k = 1; k <= m; k ++){
  49. if(s[i][j][k] == 'A'){
  50. st.x = j; st.y = k; st.z = i;
  51. }
  52. else if(s[i][j][k] == 'E'){
  53. ed.x = j; ed.y = k; ed.z = i;
  54. }
  55. }
  56. }
  57. }
  58. int flag = 0;
  59. q.push(st);
  60. e[st.z][st.x][st.y] = 1;
  61. while(! q.empty()){
  62. t = q.front(); q.pop();
  63. if(s[t.z][t.x][t.y] == 'E'){
  64. flag = 1;
  65. break;
  66. }
  67. if(s[t.z][t.x][t.y] == 'w'){
  68. int i;
  69. for(i = t.z; i <= h; i ++){ // 这里的方向要确认一下
  70. if(s[i][t.x][t.y] != 'w'){
  71. break;
  72. }
  73. } i --;
  74. if(e[i][t.x][t.y] == 0){
  75. tt.z = i; tt.x = t.x; tt.y = t.y;
  76. q.push(tt);
  77. e[i][t.x][t.y] = 1;
  78. }
  79. if(i != t.z) continue;
  80. }
  81. if(s[t.z][t.x][t.y] == 's'){
  82. for(int i = t.z; i <= h; i ++){
  83. if(s[i][t.x][t.y] == 's' && e[i][t.x][t.y] == 0){
  84. e[i][t.x][t.y] = 1;
  85. tt.z = i; tt.x = t.x; tt.y = t.y;
  86. q.push(tt);
  87. }
  88. else if(s[i][t.x][t.y] != 's') break;
  89. }
  90. for(int i = t.z; i >= 1; i --){
  91. if(s[i][t.x][t.y] == 's' && e[i][t.x][t.y] == 0){
  92. e[i][t.x][t.y] = 1;
  93. tt.z = i; tt.x = t.x; tt.y = t.y;
  94. q.push(tt);
  95. }
  96. else if(s[i][t.x][t.y] != 's') break;
  97. }
  98.  
  99. }
  100. for(int i = 0; i < 4; i ++){
  101. tt.x = t.x + dx[i];
  102. tt.y = t.y + dy[i];
  103. tt.z = t.z;
  104. if(tt.x >= 1 && tt.x <= n && tt.y >= 1 && tt.y <= m && e[tt.z][tt.x][tt.y] == 0){
  105. q.push(tt);
  106. e[tt.z][tt.x][tt.y] = 1;
  107. }
  108. }
  109. }
  110. if(flag) puts("Yes"); else puts("No");
  111.  
  112. return 0;
  113. }
  114. /*
  115. 【trick&&吐槽】
  116.  
  117. 【题意】
  118.  
  119. 【分析】
  120.  
  121. 3 3 3
  122. A..
  123. .w.
  124. ...
  125. ...
  126. .wE
  127. ...
  128. ...
  129. .w.
  130. ...
  131.  
  132. 【时间复杂度&&优化】
  133. 3 3 3
  134. ...
  135. s.E
  136. ...
  137. ...
  138. s..
  139. ...
  140. ...
  141. s.A
  142. ...
  143.  
  144. */

  

B. Batrachomyomachia

贪心,每次选承受能力最小的可行的。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<math.h>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. #include<bitset>
  12. #include<algorithm>
  13. #include<time.h>
  14. using namespace std;
  15. void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
  16. #define MS(x, y) memset(x, y, sizeof(x))
  17. #define ls o<<1
  18. #define rs o<<1|1
  19. typedef long long LL;
  20. typedef unsigned long long UL;
  21. typedef unsigned int UI;
  22. template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
  23. template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
  24. const int N = 210, M = 1e4 + 10, Z = 1e9 + 7, inf = 0x3f3f3f3f;
  25. template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
  26. int casenum, casei;
  27. int n, w;
  28. const double eps = 1e-12;
  29. int a[N][N], b[M];
  30. double f[N][N];
  31. bool del[111111];
  32. multiset<int> sot;
  33. multiset<int> :: iterator it, ir;
  34. int sgn(double x){
  35. if(fabs(x) < eps) return 0;
  36. return x > 0 ? 1 : -1;
  37. }
  38. int main()
  39. {
  40. scanf("%d%d", &n, &w);
  41. for(int i = 1; i < n; i ++) scanf("%d", &b[i]);
  42. sort(b+1,b+n);
  43. for(int i = 1; i <= 200; i ++){
  44. for(int j = 1; j <= i; j ++){
  45. a[i][j] = w;
  46. }
  47. }
  48. for(int i = 1; i <= 200; i ++){
  49. for(int j = 1; j <= i; j ++){
  50. f[i][j] = (f[i - 1][j] + f[i - 1][j - 1]) / 2 + (a[i - 1][j] + a[i - 1][j - 1]) / 2;
  51. }
  52. }
  53. for(int i=1;i<=200;i++){
  54. sort(f[i] + 1, f[i] + i + 1);
  55. reverse(f[i] + 1, f[i] + i + 1);
  56. }
  57. /*
  58. for(int i = 1; i <= 10; i ++){
  59. for(int j = 1; j <= i; j ++){
  60. printf("%.0f ", f[i][j]);
  61. }puts("");
  62. }
  63. */
  64. int ans = 0;
  65. for(int i = 2; i <= 200; i ++){
  66. for(int j = 1; j <= i; j ++){
  67. int flag=0;
  68. for(int k=1;k<n;k++)if(!del[k]&&sgn(b[k] - f[i][j])>=0){
  69. flag=k;
  70. break;
  71. }
  72.  
  73. if(!flag){
  74. ans = i - 1;
  75. break;
  76. }
  77. del[flag]=1;
  78. }
  79. if(ans) break;
  80. }
  81. printf("%d\n", ans);
  82. return 0;
  83. }
  84. /*
  85. 【trick&&吐槽】
  86.  
  87. 【题意】
  88.  
  89. 【分析】
  90.  
  91. 【时间复杂度&&优化】
  92.  
  93. */

  

C. Cherries

将所有数字排序,那么一定是选取连续$B-A+1$个数进行配对,枚举所有方案即可。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<math.h>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. #include<bitset>
  12. #include<algorithm>
  13. #include<time.h>
  14. using namespace std;
  15. void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
  16. #define MS(x, y) memset(x, y, sizeof(x))
  17. #define ls o<<1
  18. #define rs o<<1|1
  19. typedef long long LL;
  20. typedef unsigned long long UL;
  21. typedef unsigned int UI;
  22. template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
  23. template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
  24. const int N = 5050, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
  25. template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
  26. int casenum, casei;
  27. int n;
  28. int a[N];
  29. int main()
  30. {
  31. while(~scanf("%d", &n))
  32. {
  33. int A, B;
  34. scanf("%d%d", &A, &B);
  35. for(int i = 1; i <= n; ++i)
  36. {
  37. scanf("%d", &a[i]);
  38. }
  39. sort(a + 1, a + n + 1);
  40. int len = B - A;
  41. LL ans = 1e18;
  42. for(int i = 1; i + len <= n; ++i)
  43. {
  44. int j = i + len;
  45. LL tmp = 0;
  46. for(int k = i, x = A; k <= j; ++k, ++x)
  47. {
  48. tmp += abs(a[k] - x);
  49. }
  50. gmin(ans, tmp);
  51. }
  52. printf("%lld\n", ans);
  53. }
  54.  
  55. return 0;
  56. }
  57. /*
  58. 【trick&&吐槽】
  59.  
  60. 【题意】
  61.  
  62. 【分析】
  63.  
  64. 【时间复杂度&&优化】
  65.  
  66. */

  

D. Divisibility Game

预处理出约数集合后爆搜+卡时即可通过。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<math.h>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. #include<bitset>
  12. #include<algorithm>
  13. #include<time.h>
  14. using namespace std;
  15. const int N=401;
  16. const int lim=7;
  17. const int lim2=3;
  18. int i,j,x,now,ans,sum,ave,n,a[N],q[N];
  19. vector<int>d[N];
  20. int ED1 = CLOCKS_PER_SEC * 0.65;
  21. int ED2 = CLOCKS_PER_SEC * 0.95;
  22. void dfs(int s,int x){
  23.  
  24. if(x>n){
  25. for(int i=1;i<=n;i++)printf("%d ",q[i]);
  26. exit(0);
  27. }
  28. if(clock() > ED1)
  29. {
  30. return;
  31. //puts("-1");
  32. //exit(0);
  33. }
  34. for(vector<int>::iterator w=d[s].begin();w!=d[s].end();w++)
  35. for(int j=min(a[*w],1);j;j--){
  36. a[*w]-=j;
  37. for(int o=0;o<j;o++)q[x+o]=*w;
  38. dfs(s+(*w)*j,x+j);
  39. a[*w]+=j;
  40. }
  41. }
  42. void dfs2(int s,int x){
  43.  
  44. if(x>n){
  45. for(int i=1;i<=n;i++)printf("%d ",q[i]);
  46. exit(0);
  47. }
  48. if(clock() > ED2)
  49. {
  50. puts("-1");
  51. exit(0);
  52. }
  53. for(vector<int>::iterator w=d[s].begin();w!=d[s].end();w++)
  54. for(int j=min(a[*w],1);j;j--){
  55. a[*w]-=j;
  56. for(int o=0;o<j;o++)q[x+o]=*w;
  57. dfs2(s+(*w)*j,x+j);
  58. a[*w]+=j;
  59. }
  60. }
  61. int main(){
  62. scanf("%d",&n);
  63. for(i=0;i<n;i++)scanf("%d",&x),a[x]++,sum+=x;
  64. for(i=0;i<=sum;i++){
  65. for(j=lim+1;j<=13;j++)
  66. // for(j=13;j;j--)
  67. if(i%j==0&&a[j])d[i].push_back(j);
  68. for(j=lim2+1;j<=lim;j++)
  69. // for(j=13;j;j--)
  70. if(i%j==0&&a[j])d[i].push_back(j);
  71. for(j=lim2;j;j--)
  72. if(i%j==0&&a[j])d[i].push_back(j);
  73. }
  74. dfs(0,1);
  75. for(i=0;i<=sum;i++)reverse(d[i].begin(),d[i].end());
  76. dfs2(0,1);
  77. puts("-1");
  78. }

  

E. Enter the Word

设$dp[i]$表示打出前$i$个字符的最小代价,那么有$dp[i-1]\leq dp[i]\leq dp[i-1]+1$。

为了检查是否可以不$+1$,找到$dp$值的分界线,那么只要后面部分是前面部分的子串即可。

设$f[i]$表示子串匹配结束位置是$i$是否可行,可以通过bitset加速。

时间复杂度$O(\frac{n^2}{64})$。

  1. #include<cstdio>
  2. #include<bitset>
  3. #include<cstring>
  4. using namespace std;
  5. const int N=200010;
  6. int n,i,r,ans;char a[N];bitset<N>f,v[26];
  7. int main(){
  8. scanf("%s",a);
  9. n=strlen(a);
  10. for(i=0;i<n;i++){
  11. a[i]-='a';
  12. f=f<<1&v[a[i]];
  13. if(!f.any()){
  14. for(int k=r;k<i;k++)v[a[k]][k]=1;
  15. r=i;
  16. ans++;
  17. f=v[a[i]];
  18. }
  19. }
  20. printf("%d",ans);
  21. }

  

F. Formula 1

按题意模拟即可,记录每个人的排名以及领先的圈数。

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. const int N=100010;
  5. int n,m,i,x,f[N],pos[N],g[N];
  6. bool cmp(int x,int y){return g[x]==g[y]?pos[x]<pos[y]:g[x]>g[y];}
  7. int main(){
  8. scanf("%d%d",&n,&m);
  9. for(i=1;i<=n;i++){
  10. f[i]=i;
  11. pos[i]=i;
  12. }
  13. while(m--){
  14. scanf("%d",&x);
  15. for(i=1;i<=n;i++)if(f[i]==x)break;
  16. int o=i;
  17. if(o==1){
  18. g[x]++;
  19. for(i=1;i<n;i++)f[i]=f[i+1];
  20. f[n]=x;
  21. swap(f[n],f[n-1]);
  22. }else swap(f[o],f[o-1]);
  23. //for(i=1;i<=n;i++)printf("%d ",f[i]);puts("");
  24. }
  25. for(i=1;i<=n;i++)pos[f[i]]=i;
  26. sort(f+1,f+n+1,cmp);
  27. for(i=1;i<=n&&i<=6;i++)printf("%d ",f[i]);
  28. }

  

G. Game with Coins

将过程倒过来,则变成对于最后一个数,找到倒数第二个数,然后中间的数都要被最后一个数覆盖掉,区间DP即可。

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. const int N=110;
  5. int n,i,j,a[N],f[N][N],ans;
  6. int dfs(int l,int r){
  7. if(~f[l][r])return f[l][r];
  8. int&t=f[l][r];
  9. t=0;
  10. int left=l+1,right=r;
  11. if(left>right)right+=n;
  12. for(int i=left;i<=right;i++)t=max(t,dfs(l,(i-1+n)%n)+dfs(i%n,r)+abs(a[l]-a[i%n]));
  13. return t;
  14. }
  15. int main(){
  16. scanf("%d",&n);
  17. for(i=0;i<n;i++)scanf("%d",&a[i]);
  18. for(i=0;i<n;i++)for(j=0;j<n;j++)if(i!=j)f[i][j]=-1;
  19. for(i=0;i<n;i++)for(j=0;j<n;j++)ans=max(ans,dfs(i,j));
  20. printf("%d",ans);
  21. }

  

H. Hamnattan

首先特判起点终点都在同一条街道上的情况。其余情况Dijkstra求最短路即可,需要现算代价。

  1. #include<cstdio>
  2. #include<queue>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #include<vector>
  6. using namespace std;
  7. typedef long long ll;
  8. typedef pair<int,int>P;
  9. typedef pair<ll,P>PI;
  10. typedef pair<ll,PI>PII;
  11. const ll inf=1LL<<60;
  12. const int N=110,M=500000;
  13. int sa[N],sb[N];
  14. int n,m,i,j,x,y,a[N],b[N],ns[N][N],ew[N][N],s[N][N];
  15. int sx,sy,ex,ey;
  16. ll d[N][N];
  17. int g[N][N],v[M][2],w[M][2],nxt[M],ed;
  18. priority_queue<PI,vector<PI>,greater<PI> >q;
  19. ll ans=inf;
  20. inline void add(int x,int y,int xx,int yy,int z,int zz){
  21. v[++ed][0]=xx;
  22. v[ed][1]=yy;
  23. w[ed][0]=z;
  24. w[ed][1]=zz;
  25. nxt[ed]=g[x][y];
  26. g[x][y]=ed;
  27. }
  28. inline void add2(int x,int y,int xx,int yy,int w,int ww){
  29. add(x,y,xx,yy,w,ww);
  30. add(xx,yy,x,y,w,ww);
  31. }
  32. inline int col(int x,int y,ll z){
  33. int NS=ns[x][y],EW=ew[x][y],S=s[x][y];
  34. z%=NS+EW;
  35. if(S)return z<EW;
  36. return z>=NS;
  37. }
  38. inline ll cal(int x,int y,int dir,ll z){
  39. while(col(x,y,z)!=dir)z++;
  40. return z;
  41. }
  42. inline void ext(int x,int y,ll z){
  43. if(d[x][y]>z)q.push(PI(d[x][y]=z,P(x,y)));
  44. }
  45. void CHECK(){
  46. int i,j;
  47. for(i=1;i<=n;i++)for(j=1;j<=m;j++){
  48. if(sy==ey)if(i<n)if(sb[j-1]==sy)if(sa[i-1]<=sx&&sx<=sa[i])
  49. if(sa[i-1]<=ex&&ex<=sa[i]){
  50. printf("%d",abs(sx-ex)+abs(sy-ey));
  51. exit(0);
  52. }
  53. if(sx==ex)if(j<m)if(sa[i-1]==sx)if(sb[j-1]<=sy&&sy<=sb[j])
  54. if(sb[j-1]<=ey&&ey<=sb[j]){
  55. printf("%d",abs(sx-ex)+abs(sy-ey));
  56. exit(0);
  57. }
  58. }
  59. }
  60. void EXT(int x,int y){
  61. int i,j;
  62. for(i=1;i<=n;i++)for(j=1;j<=m;j++){
  63. if(i<n)if(sb[j-1]==y)if(sa[i-1]<=x&&x<=sa[i]){
  64. ext(i,j,cal(i,j,1,x-sa[i-1]));
  65. ext(i+1,j,cal(i+1,j,1,sa[i]-x));
  66. }
  67. if(j<m)if(sa[i-1]==x)if(sb[j-1]<=y&&y<=sb[j]){
  68. ext(i,j,cal(i,j,0,y-sb[j-1]));
  69. ext(i,j+1,cal(i,j+1,0,sb[j]-y));
  70. }
  71. }
  72. }
  73. void FIN(int x,int y){
  74. int i,j;
  75. for(i=1;i<=n;i++)for(j=1;j<=m;j++){
  76. if(i<n)if(sb[j-1]==y)if(sa[i-1]<=x&&x<=sa[i]){
  77. ans=min(ans,d[i][j]+x-sa[i-1]);
  78. ans=min(ans,d[i+1][j]+sa[i]-x);
  79. }
  80. if(j<m)if(sa[i-1]==x)if(sb[j-1]<=y&&y<=sb[j]){
  81. ans=min(ans,d[i][j]+y-sb[j-1]);
  82. ans=min(ans,d[i][j+1]+sb[j]-y);
  83. }
  84. }
  85. }
  86. int main(){
  87. scanf("%d%d",&n,&m);
  88. for(i=1;i<n;i++){
  89. scanf("%d",&a[i]);
  90. sa[i]=sa[i-1]+a[i];
  91. }
  92. for(i=1;i<m;i++){
  93. scanf("%d",&b[i]);
  94. sb[i]=sb[i-1]+b[i];
  95. }
  96. for(i=1;i<=n;i++)for(j=1;j<=m;j++){
  97. if(i<n)add2(i,j,i+1,j,a[i],1);
  98. if(j<m)add2(i,j,i,j+1,b[j],0);
  99. }
  100. for(j=1;j<=m;j++)for(i=1;i<=n;i++)scanf("%d%d%d",&ns[i][j],&ew[i][j],&s[i][j]);
  101. scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
  102. CHECK();
  103. for(i=1;i<=n;i++)for(j=1;j<=m;j++)d[i][j]=inf;
  104. EXT(sx,sy);
  105. while(!q.empty()){
  106. PI t=q.top();q.pop();
  107. int x=t.second.first,y=t.second.second;
  108. if(d[x][y]<t.first)continue;
  109. //printf("%d %d %lld\n",x,y,t.first);
  110. for(i=g[x][y];i;i=nxt[i]){
  111. ext(v[i][0],v[i][1],cal(v[i][0],v[i][1],w[i][1],t.first+w[i][0]));
  112. }
  113. }
  114. FIN(ex,ey);
  115. printf("%lld",ans);
  116. }
  117. /*
  118. 4 3
  119. 10 10 10
  120. 10 10
  121. 1 99 0
  122. 99 1 0
  123. 50 99 0
  124. 1 99 1
  125. 1 99 0
  126. 99 1 0
  127. 20 41 1
  128. 1 99 0
  129. 99 1 0
  130. 1 99 1
  131. 99 1 0
  132. 99 1 0
  133. 1 10
  134. 30 19
  135. */

  

I. Integer Pairs

只要$a[j]<0$即合法。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<math.h>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. #include<bitset>
  12. #include<algorithm>
  13. #include<time.h>
  14. using namespace std;
  15. void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
  16. #define MS(x, y) memset(x, y, sizeof(x))
  17. #define ls o<<1
  18. #define rs o<<1|1
  19. typedef long long LL;
  20. typedef unsigned long long UL;
  21. typedef unsigned int UI;
  22. template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
  23. template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
  24. const int N = 0, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
  25. template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
  26. int casenum, casei;
  27. int n;
  28. int main()
  29. {
  30. while(~scanf("%d", &n))
  31. {
  32. int neg = 0;
  33. for(int i = 1; i <= n; ++i)
  34. {
  35. int x; scanf("%d", &x);
  36. neg += x < 0;
  37. }
  38. LL ans = neg * (n - 1ll);
  39. printf("%lld\n", ans);
  40. }
  41.  
  42. return 0;
  43. }
  44. /*
  45. 【trick&&吐槽】
  46.  
  47. 【题意】
  48.  
  49. 【分析】
  50.  
  51. 【时间复杂度&&优化】
  52. 3
  53. -1 -2 -3
  54.  
  55. */

  

J. Jedi Training

线段树维护$f[l][r]$表示对应区间内选择子序列的左端点奇偶性为$l$,右端点奇偶性为$r$时的最大和。

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. #define rep(i) for(int i=0;i<2;i++)
  5. typedef long long ll;
  6. const int N=100010,M=262150;
  7. const ll inf=1LL<<60;
  8. int n,m,i,a[N],op,x,y;
  9. inline void up(ll&a,ll b){a<b?(a=b):0;}
  10. struct E{
  11. ll f[2][2];
  12. E(){rep(i)rep(j)f[i][j]=-inf;}
  13. void clr(){rep(i)rep(j)f[i][j]=-inf;}
  14. void set(int x,ll p){
  15. clr();
  16. f[x&1][x&1]=p;
  17. }
  18. E operator+(const E&b){
  19. E c;
  20. rep(i)rep(j)c.f[i][j]=max(f[i][j],b.f[i][j]);
  21. rep(i)rep(j)rep(k)up(c.f[i][k],f[i][j]+b.f[j^1][k]);
  22. return c;
  23. }
  24. void write(){
  25. ll t=-inf;
  26. rep(i)rep(j)up(t,f[i][j]);
  27. printf("%lld\n",t);
  28. }
  29. }v[M];
  30. void build(int x,int a,int b){
  31. if(a==b){
  32. v[x].set(a,::a[a]);
  33. return;
  34. }
  35. int mid=(a+b)>>1;
  36. build(x<<1,a,mid),build(x<<1|1,mid+1,b);
  37. v[x]=v[x<<1]+v[x<<1|1];
  38. }
  39. void change(int x,int a,int b,int c,int d){
  40. if(a==b){
  41. v[x].set(a,d);
  42. return;
  43. }
  44. int mid=(a+b)>>1;
  45. if(c<=mid)change(x<<1,a,mid,c,d);else change(x<<1|1,mid+1,b,c,d);
  46. v[x]=v[x<<1]+v[x<<1|1];
  47. }
  48. E ask(int x,int a,int b,int c,int d){
  49. if(c<=a&&b<=d)return v[x];
  50. int mid=(a+b)>>1;
  51. E t;
  52. t.clr();
  53. if(c<=mid)t=ask(x<<1,a,mid,c,d);
  54. if(d>mid)t=t+ask(x<<1|1,mid+1,b,c,d);
  55. return t;
  56. }
  57. int main(){
  58. scanf("%d%d",&n,&m);
  59. for(i=1;i<=n;i++)scanf("%d",&a[i]);
  60. build(1,1,n);
  61. while(m--){
  62. scanf("%d%d%d",&op,&x,&y);
  63. if(op==1)change(1,1,n,x,y);
  64. else{
  65. ask(1,1,n,x,y).write();
  66. }
  67. }
  68. }

  

K. Kings of a Round Table

假设$1$号国王一定位于$1$号位置,并不区分剩下$7$个国王,则这种情况下方案数还要乘以$n\times 7!$。

那么剩下的方案数大约只有$3\times 10^8$个,爆搜打表即可。

  1. #include<cstdio>
  2. long long f[111];
  3. int n;
  4. int main(){
  5. f[9]=0;
  6. f[10]=0;
  7. f[11]=0;
  8. f[12]=0;
  9. f[13]=0;
  10. f[14]=0;
  11. f[15]=0;
  12. f[16]=0;
  13. f[17]=685440;
  14. f[18]=725760;
  15. f[19]=11491200;
  16. f[20]=6451200;
  17. f[21]=83825280;
  18. f[22]=38142720;
  19. f[23]=397837440;
  20. f[24]=170311680;
  21. f[25]=1441440000;
  22. f[26]=617460480;
  23. f[27]=4330609920;
  24. f[28]=1905684480;
  25. f[29]=11330323200;
  26. f[30]=5175878400;
  27. f[31]=26645794560;
  28. f[32]=12675317760;
  29. f[33]=57564017280;
  30. f[34]=28504707840;
  31. f[35]=116035920000;
  32. f[36]=59698114560;
  33. f[37]=220799779200;
  34. f[38]=117723513600;
  35. f[39]=400156848000;
  36. f[40]=220502016000;
  37. f[41]=695520483840;
  38. f[42]=395054150400;
  39. f[43]=1165870379520;
  40. f[44]=680891904000;
  41. f[45]=1893253824000;
  42. f[46]=1134285546240;
  43. f[47]=2989486241280;
  44. f[48]=1833544581120;
  45. f[49]=4604213577600;
  46. f[50]=2885462496000;
  47. f[51]=6934509429120;
  48. f[52]=4433085296640;
  49. f[53]=10236190124160;
  50. f[54]=6664974140160;
  51. f[55]=14837041296000;
  52. f[56]=9826142699520;
  53. f[57]=21152159804160;
  54. f[58]=14230860215040;
  55. f[59]=29701625184000;
  56. f[60]=20277521510400;
  57. f[61]=41130725126400;
  58. f[62]=28465795572480;
  59. f[63]=56232969811200;
  60. f[64]=39416274616320;
  61. f[65]=75976140240000;
  62. f[66]=53892855878400;
  63. f[67]=101531626035840;
  64. f[68]=72828098703360;
  65. f[69]=134307318499200;
  66. f[70]=97351809811200;
  67. scanf("%d",&n);
  68. printf("%lld",f[n]);
  69. }

  

L. Lines and Polygon

求出直线与凸包的交点,然后在附近枚举即可得到最近点。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<math.h>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. #include<bitset>
  12. #include<algorithm>
  13. #include<time.h>
  14. using namespace std;
  15. void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
  16. #define MS(x, y) memset(x, y, sizeof(x))
  17. #define ls o<<1
  18. #define rs o<<1|1
  19. typedef unsigned long long UL;
  20. typedef unsigned int UI;
  21. template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
  22. template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
  23. const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
  24. template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
  25. int casenum, casei;
  26.  
  27. const long double eps = 1e-8;
  28. int sgn(double x)
  29. {
  30. if(fabs(x) < eps) return 0;
  31. return x > 0 ? 1 : -1;
  32. }
  33. struct point
  34. {
  35. long double x, y;
  36. point(){}
  37. point(long double a, long double b){x = a; y = b;}
  38. long double det (const point &b)const{
  39. return x * b.y - y * b.x;
  40. }
  41. friend bool operator < (const point &a, const point &b){
  42. if(sgn(a.x - b.x) == 0) return sgn(a.y - b.y) < 0;
  43. return sgn(a.x - b.x) < 0;
  44. }
  45. friend point operator - (const point &a, const point &b){
  46. return point(a.x - b.x, a.y - b.y);
  47. }
  48. };
  49. struct Convex
  50. {
  51. int n;
  52. vector<point> a, upper, lower;
  53. Convex(){}
  54. Convex (vector<point> _a) : a(_a){
  55. n = a.size();
  56. int ptr = 0;
  57. for(int i = 1; i < n; i ++) if(a[ptr] < a[i]) ptr = i;
  58. for(int i = 0; i <= ptr; i ++) lower.push_back(a[i]);
  59. for(int i = ptr; i < n; i ++) upper.push_back(a[i]);
  60. upper.push_back(a[0]);
  61. }
  62. int sign(long double x){
  63. if(fabs(x) < eps) return 0;
  64. return x > 0 ? 1 : -1;
  65. }
  66.  
  67. pair<long double, int> get_tangent(vector<point> &convex, point vec){
  68. int l = 0, r = (int) convex.size() - 2;
  69. for(; l + 1 < r;){
  70. int mid = (l + r) / 2;
  71. if(sign((convex[mid + 1] - convex[mid]).det(vec)) > 0) r = mid;
  72. else l = mid;
  73. }
  74. return max(make_pair(vec.det(convex[r]), r), make_pair(vec.det(convex[0]), 0));
  75. }
  76. int binary_search(point u, point v, int l, int r){
  77. int sl = sign((v - u).det(a[l % n] - u));
  78. for(; l + 1 < r;){
  79. int mid = (l + r) / 2;
  80. int smid = sign((v - u).det(a[mid % n] - u));
  81. if(smid == sl) l = mid;
  82. else r = mid;
  83. }
  84. return l % n;
  85. }
  86. int get_tangent(point vec){
  87. pair<long double, int> ret = get_tangent(upper, vec);
  88. ret.second = (ret.second + (int)lower.size() - 1) % n;
  89. ret = max(ret, get_tangent(lower, vec));
  90. return ret.second;
  91. }
  92. bool get_intersection(point u, point v, int &i0, int &i1){
  93. int p0 = get_tangent(u - v), p1 = get_tangent(v - u);
  94. if(sign((v - u).det(a[p0] - u)) * sign((v - u).det(a[p1] - u)) < 0){
  95. if(p0 > p1) swap(p0, p1);
  96. i0 = binary_search(u, v, p0, p1);
  97. i1 = binary_search(u, v, p1, p0 + n);
  98. return true;
  99. }
  100. else{
  101. return false;
  102. }
  103. }
  104. };
  105. int n;
  106. point p[N];
  107. vector<point> a, b;
  108. Convex D;
  109. int m;
  110. long double A, B, C;
  111.  
  112. long double cal(int i0)
  113. {
  114. return fabs((A * D.a[i0].x + B * D.a[i0].y + C) );
  115. }
  116. const double INF = 1e9;
  117. int main()
  118. {
  119. scanf("%d", &n);
  120. for(int i = 0; i < n; i ++) {
  121. //scanf("%lf%lf", &p[i].x, &p[i].y);
  122. double x, y;
  123. scanf("%lf%lf", &x, &y);
  124. p[i].x = x; p[i].y = y;
  125. //a.push_back(p[i]);
  126. }
  127. for(int i = n - 1; i >= 0; i --) a.push_back(p[i]);
  128. int ptr = 0;
  129. for(int i = 1; i < n; i ++){
  130. if(a[ptr] < a[i]) ptr = i;
  131. }
  132. for(int i = ptr; i < n; i ++){
  133. b.push_back(a[i]);
  134. }
  135. for(int i = 0; i < ptr; i ++){
  136. b.push_back(a[i]);
  137. }
  138. D = Convex(b);
  139. scanf("%d", &m);
  140. for(int i = 1; i <= m; i ++){
  141. double AA, BB, CC;
  142. scanf("%lf%lf%lf", &AA, &BB, &CC);
  143. A = AA; B = BB; C = CC;
  144. double ans = 1e18;
  145. int i0, i1;
  146. point p0, p1;
  147. if(A){
  148. p0.y = 0, p0.x = -C / A;
  149. p1.y = INF, p1.x = (- C - B * INF) / A;
  150. }
  151. else if(B){
  152. p0.x = 0, p0.y = -C / B;
  153. p1.x = INF, p1.y = (-C - INF * A) / B;
  154. }
  155. //else while(1);
  156. if(D.get_intersection(p0, p1, i0, i1)){
  157. #define next(i) ((i + 1) % n)
  158. #define pre(i) ((i - 1 + n) % n)
  159. for(int j = 0; j < 10; j ++){
  160. gmin(ans, cal(i0));
  161. i0 = next(i0);
  162. }
  163. for(int j = 0; j < 20; j ++){
  164. gmin(ans, cal(i0));
  165. i0 = pre(i0);
  166. }
  167. for(int j = 0; j < 10; j ++){
  168. gmin(ans, cal(i1));
  169. i1 = next(i1);
  170. }
  171. for(int j = 0; j < 20; j ++){
  172. gmin(ans, cal(i1));
  173. i1 = pre(i1);
  174. }
  175. }
  176. else{
  177. //ans = 0;
  178. //while(1);
  179. for(int j = 0; j < n; j ++) gmin(ans, cal(j));
  180. }
  181. double ANS = ans / sqrt(A * A + B * B);
  182. printf("%.6f\n", ANS);
  183. }
  184. return 0;
  185. }
  186. /*
  187. 【trick&&吐槽】
  188.  
  189. 4
  190. 1 3
  191. 3 1
  192. 1 -1
  193. -1 1
  194. 1
  195. 0 4 -5
  196. 【题意】
  197.  
  198. 【分析】
  199.  
  200. 【时间复杂度&&优化】
  201.  
  202. */

  

M. MIPT Campus

留坑。

XIII Open Grodno SU Championship的更多相关文章

  1. 【DFS】XIII Open Championship of Y.Kupala Grodno SU Grodno, Saturday, April 29, 2017 Problem D. Divisibility Game

    题意:给你一个序列,长度不超过52,每个元素不超过13.让你重新对这个序列排序,sum(i)表示i的前缀和,使得排序过后,对每个i,都有sum(i)%i==0. 深搜,加两个优化:①倒着从后向前搜:② ...

  2. 【二分】【三分】【计算几何】XIII Open Championship of Y.Kupala Grodno SU Grodno, Saturday, April 29, 2017 Problem L. Lines and Polygon

    题意:给你一个凸多边形,和多次询问,每次询问给你一条直线,问你这条直线与凸包上的顶点的最近距离是多少. 记当前询问的直线的斜率为K, 先找到与这条直线距离最远的两个点: 就把凸包所有的边当做有向直线进 ...

  3. 【线段树】XIII Open Championship of Y.Kupala Grodno SU Grodno, Saturday, April 29, 2017 Problem J. Jedi Training

    题意:给你一个序列,支持两种操作:单点修改:询问一个区间中所有相邻位置下标奇偶性均不同的子序列中,和最大的是多少. 线段树每个结点维护四个值: 以奇数下标开始到奇数下标结束的最大子序列和: 以偶数下标 ...

  4. 【贪心】【后缀自动机】XIII Open Championship of Y.Kupala Grodno SU Grodno, Saturday, April 29, 2017 Problem E. Enter the Word

    题意:给你一个串,让你从左到右构造这个串,一次操作可以直接在当前串后面添加一个任意字符,或者拷贝当前串的任意一个子串到当前串的后面.问你最少要多少次操作才能构造出这个串. 从前向后贪心,从当前已构造的 ...

  5. HDU-AcmKeHaoWanLe训练实录

    菜鸡队训练实录. 现场赛记录:[名称:奖项/排名] 2017: ICPC Shenyang:Gold/3 CCPC Hangzhou:Gold/3 ICPC Beijing:Gold/13 CCPC ...

  6. URAL 1728. Curse on Team.GOV(STL set)

    题目链接:space=1&num=1728" target="_blank">http://acm.timus.ru/problem.aspx?space= ...

  7. URAL 1873. GOV Chronicles

    唔 神题一道 大家感受一下 1873. GOV Chronicles Time limit: 0.5 secondMemory limit: 64 MB A chilly autumn night. ...

  8. linux su和sudo命令的区别

    一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要添加用户任务,执行useradd ,beinan用 ...

  9. entrar en su zapatilla de deporte en este lugar

    Mientras que yo apareció su campo usando nuestro Nike Glide Wildhorse sólo dos ($ 110) zapatillas de ...

随机推荐

  1. StringJdbc :jdbcTemplate

    Spring框架对Jdbc进行了封装 提供了一个JDBCTemplated对象简化Jdbc开发 步骤 1 导包 2 创建JDBCTemplate 对象 依赖于DataSource 3 调用JDBCTe ...

  2. ZOJ Monthly, January 2018

    A 易知最优的方法是一次只拿一颗,石头数谁多谁赢,一样多后手赢 #include <map> #include <set> #include <ctime> #in ...

  3. django+mysql简单总结

    1.工程下建立APP(以WIN10+PYTHON3.6为例) C:\Users\WYS>django-admin startproject myweb  #建立项目 C:\Users\WYS&g ...

  4. Spring的事务机制

    ---恢复内容开始--- 内定的=>(只需要在xml 中添加一个bean) 在xml 中添加 <bean id="listener" class="com.t ...

  5. NOI-OJ 1.13 ID:23 区间内的真素数

    整体思路 这里需要大量使用素数,必须能够想到只求出M到N之间的素数是不够的,因为M到N之间数字的反序有可能是大于M或小于N的数字,例如M=2,N=20,那么19的反序91大于20,所以使用埃拉拖色尼算 ...

  6. react实战项目开发(1) 搭建react开发环境初始化项目(Create-react-app)

    前言 Create React App npm install -g create-react-app create-react-app my-app cd my-app npm start 执行命令 ...

  7. java PDF2JPG

    import org.apache.commons.lang3.StringUtils; import org.apache.pdfbox.pdmodel.PDDocument; import org ...

  8. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column

    安装了mysql5.7.19后,执行语句中只要含有group by 就会报这个错 [Err] 1055 - Expression #1 of ORDER BY clause is not in GRO ...

  9. vue之vuex学习

    知识点一:vuex是状态管理器(单向数据流) 每个Vuex应用程序的核心是商店.“商店”基本上是一个容纳您的应用程序状态的容器.有两件事使Vuex商店与普通的全局对象不同: Vuex商店是被动的.当V ...

  10. 第一章 Bootstrap简介

    一.Bootstrap简介 Bootstrap是基于 HTML.CSS.JAVASCRIPT 的前端框架,它简洁灵活,使得 Web 开发更加快捷.它由Twitter的设计师Mark Otto和Jaco ...