[关闭]
@sensitive-cs 2017-04-15T14:55:05.000000Z 字数 6228 阅读 798

2017.4.10训练题题解

题解


A - Binary Simulation
题意:
给出一个只包含0和1的字符串,有两种操作,第一是改变下标从i到j的数,让0变成1,1变成0,;第二种操作是查询下标为i的是0还是1.
思路:
线段树,每次翻转,就让所在区间改变的次数加1,不对数进行改变,最后根据改变次数的奇偶性判断是否改变。
代码:

  1. #include <string.h>
  2. #include <stdio.h>
  3. int a[100005];
  4. int add[400005];
  5. int sum[400005];
  6. char b[100005];
  7. void pushup(int rt)
  8. {
  9. sum[rt] = sum[rt << 1] + sum[rt << 1|1];
  10. }
  11. void build(int l,int r,int rt)
  12. {
  13. if (l == r)
  14. {
  15. sum[rt] = a[l];
  16. return;
  17. }
  18. int m = (l+r) >> 1;
  19. build(l,m,rt << 1);
  20. build(m+1,r,rt << 1|1);
  21. pushup(rt);
  22. }
  23. void pushdown(int rt,int ln,int rn)
  24. {
  25. if (add[rt])
  26. {
  27. add[rt << 1] += add[rt];
  28. add[rt << 1|1] += add[rt];
  29. sum[rt << 1] += ln * add[rt];
  30. sum[rt << 1|1] += rn *add[rt];
  31. add[rt] = 0;
  32. }
  33. }
  34. void update(int ll,int rr,int c,int l,int r,int rt)
  35. {
  36. if (ll <= l && r <= rr)
  37. {
  38. sum[rt] += c*(r-l+1);
  39. add[rt] += c;
  40. return;
  41. }
  42. int m = (l+r) >> 1;
  43. pushdown(rt,m-l+1,r-m);
  44. if (ll <= m) update(ll,rr,c,l,m,rt << 1);
  45. if (rr > m) update(ll,rr,c,m+1,r,rt << 1|1);
  46. pushup(rt);
  47. }
  48. long long query(int ll,int rr,int l,int r,int rt)
  49. {
  50. if (ll <= l && r <= rr)
  51. {
  52. return sum[rt];
  53. }
  54. int m = (l+r) >> 1;
  55. pushdown(rt,m-l+1,r-m);
  56. long long ans = 0;
  57. if (ll <= m) ans += query(ll,rr,l,m,rt << 1);
  58. if (rr > m) ans += query(ll,rr,m+1,r,rt << 1|1);
  59. return ans;
  60. }
  61. int main()
  62. {
  63. int t;
  64. scanf ("%d",&t);
  65. int cnt = 0;
  66. while (t--)
  67. {
  68. memset(a,0,sizeof(a));
  69. memset(add,0,sizeof(add));
  70. memset(sum,0,sizeof(sum));
  71. printf("Case %d:\n",++cnt);
  72. int q = 0,sl = 0;
  73. scanf("%s",b);
  74. sl = strlen(b);
  75. build(1,sl,1);
  76. scanf("%d",&q);
  77. for (int i = 0;i < q;i++)
  78. {
  79. char s[5];
  80. int x,y;
  81. scanf("%s",s);
  82. if (s[0] == 'I')
  83. {
  84. scanf("%d%d",&x,&y);
  85. update(x,y,1,1,sl,1);
  86. }
  87. if (s[0] == 'Q')
  88. {
  89. scanf("%d",&x);
  90. int ans = query(x,x,1,sl,1);
  91. if (ans % 2 == 0)
  92. {
  93. printf("%c\n",b[x-1]);
  94. }
  95. else
  96. {
  97. if (b[x-1] == '0') printf("1\n");
  98. else printf("0\n");
  99. }
  100. }
  101. }
  102. }
  103. return 0;
  104. }

B - Points in Segments
题意:
给出n个数,后面给出m个区间,对于每个区间,查询再次区间内的数有多少个。
思路:
线段树+离散化。需要注意的是离散化时需要对不连续的两个数之间加入一个在两数之间的数,这样才能保证不出错。第二是线段树初始化的时候区间长度加上2*10e5,这样保证了所有数据都在区间内;第三呢,对输入的区间进行特判,如果左端点大于最大数或者右端点小于最小数,那就输出0.
代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. const int len = 1e5 + 5;
  7. vector<int> v;
  8. int a[len];
  9. bool pos[len * 10];
  10. int ans = 0;
  11. struct ss
  12. {
  13. int l,r;
  14. int sum;
  15. }node[len * 10];
  16. void build(int rt,int l,int r)
  17. {
  18. node[rt].l = l,node[rt].r = r;
  19. if (l == r)
  20. {
  21. if (pos[l]) node[rt].sum = 1;
  22. else node[rt].sum = 0;
  23. return;
  24. }
  25. int mid = (l + r) >> 1;
  26. build(rt << 1,l,mid);
  27. build(rt << 1|1,mid+1,r);
  28. node[rt].sum += (node[rt << 1].sum+node[rt << 1|1].sum);
  29. }
  30. void query(int rt,int L,int R)
  31. {
  32. if (node[rt].l >= L && node[rt].r <= R)
  33. {
  34. ans += node[rt].sum;
  35. return;
  36. }
  37. int mid = (node[rt].l + node[rt].r) >> 1;
  38. if (L <= mid) query(rt << 1,L,R);
  39. if (R > mid) query(rt << 1|1,L,R);
  40. }
  41. int main()
  42. {
  43. int t,cnt = 0;
  44. scanf("%d",&t);
  45. while (t--)
  46. {
  47. memset(a,0,sizeof(a));
  48. v.clear();
  49. memset(pos,0,sizeof(pos));
  50. memset(node,0,sizeof(node));
  51. int n,q;
  52. scanf("%d%d",&n,&q);
  53. for (int i = 0;i < n;i++)
  54. {
  55. scanf("%d",&a[i]);
  56. v.push_back(a[i]);
  57. }
  58. printf("Case %d:\n",++cnt);
  59. sort(a,a+n);
  60. sort(v.begin(),v.end());
  61. int tt = v.size();
  62. for (int i = 1;i < tt;i++)
  63. {
  64. if (v[i] - v[i-1] > 1) v.push_back(v[i] - 1);
  65. }
  66. int ls = v.size();
  67. sort(v.begin(),v.end());
  68. for (int i = 0;i < n;i++)
  69. {
  70. vector<int>::iterator it;
  71. it = lower_bound(v.begin(),v.end(),a[i]);
  72. pos[it - v.begin()+1] = 1;
  73. }
  74. build(1,1,ls + 2*len);
  75. for (int i = 0;i < q;i++)
  76. {
  77. int x,y;
  78. scanf("%d%d",&x,&y);
  79. if (x > a[n-1] || y < a[0])
  80. {
  81. printf("%d\n",0);
  82. continue;
  83. }
  84. ans = 0;
  85. vector<int>::iterator it;
  86. x = lower_bound(v.begin(),v.end(),x) - v.begin() + 1;
  87. y = lower_bound(v.begin(),v.end(),y) - v.begin() + 1;
  88. query(1,x,y);
  89. printf("%d\n",ans);
  90. }
  91. }
  92. return 0;
  93. }

C - Calm Down
题意:
给出大圆的半径和小圆的个数,求出小圆的最大半径,保证所有的小圆半径相同且所有的小圆相切。
思路:
所有相邻小圆的圆心相连构成一个正多边形,根据正多边形的内角和公式和三角函数以及长度关系可以推出大圆与小圆的半径的关系。数学题。
代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. int main()
  5. {
  6. int t;
  7. scanf("%d",&t);
  8. int cnt = 0;
  9. while (t--)
  10. {
  11. int n;
  12. double r;
  13. scanf("%lf%d",&r,&n);
  14. double ans = 0;
  15. const double pi = atan(1)*4;
  16. if (n == 2)
  17. {
  18. ans = r / 2;
  19. }
  20. else
  21. {
  22. double s = (n-2) * 180.0 / n;
  23. s = s * pi / 180 / 2;
  24. ans = r * cos(s) / (1 + cos(s));
  25. }
  26. printf("Case %d: %.8f\n",++cnt,ans);
  27. }
  28. return 0;
  29. }

D - Neighbor House
题意:
给出n个房子,每个房子可涂3种颜色,涂每种颜色的价格不同,相邻两间房子的颜色不能相同,问最小的花费。
思路:
动态规划,转移方程 : dp[i][1] = min(dp[i-1][2]+a[i][1],dp[i-1][3]+a[i][1]),其他与此类似。
代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. using namespace std;
  5. int a[25][5];
  6. int dp[25][5];
  7. const int inf = 0x3f3f3f3f;
  8. int main()
  9. {
  10. int t;
  11. int cnt = 0;
  12. scanf("%d",&t);
  13. while (t--)
  14. {
  15. memset(a,0,sizeof(a));
  16. int n;
  17. scanf("%d",&n);
  18. for (int i = 1;i <= n;i++)
  19. for (int j = 1;j <= 3;j++)
  20. scanf("%d",&a[i][j]);
  21. memset(dp,inf,sizeof(dp));
  22. dp[1][1] = a[1][1];
  23. dp[1][2] = a[1][2];
  24. dp[1][3] = a[1][3];
  25. for (int i = 2;i <= n;i++)
  26. {
  27. dp[i][1] = min(dp[i-1][2] + a[i][1],dp[i-1][3] + a[i][1]);
  28. dp[i][2] = min(dp[i-1][1] + a[i][2],dp[i-1][3] + a[i][2]);
  29. dp[i][3] = min(dp[i-1][2] + a[i][3],dp[i-1][1] + a[i][3]);
  30. }
  31. int minn = inf;
  32. for (int i = 1;i <= 3;i++)
  33. if (dp[n][i] < minn) minn = dp[n][i];
  34. printf("Case %d: %d\n",++cnt,minn);
  35. }
  36. return 0;
  37. }

E - Array Queries
题意:
给出n个数,每次查询下标从i到j的最小值。
思路:
线段树,维护的是区间的最小值。
代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. using namespace std;
  5. struct tt
  6. {
  7. int l,r;
  8. int v;
  9. }node[100005 << 2];
  10. void build(int l,int r,int rt)
  11. {
  12. node[rt].l = l,node[rt].r = r;
  13. if (l == r)
  14. {
  15. scanf("%d",&node[rt].v);
  16. return;
  17. }
  18. int mid = (l + r) >> 1;
  19. build(l,mid,rt << 1);
  20. build(mid + 1,r,rt << 1|1);
  21. node[rt].v = min(node[rt << 1].v,node[rt << 1|1].v);
  22. }
  23. int query(int rt,int ll,int rr)
  24. {
  25. if (node[rt].l >= ll && node[rt].r <= rr)
  26. {
  27. return node[rt].v;
  28. }
  29. int a1 = 1e7,a2 = 1e7;
  30. int mid = (node[rt].l + node[rt].r) / 2;
  31. if (ll <= mid) a1 = query(rt << 1,ll,rr);
  32. if (rr > mid) a2 = query(rt << 1|1,ll,rr);
  33. return min(a1,a2);
  34. }
  35. int main()
  36. {
  37. int t;
  38. scanf("%d",&t);
  39. int cnt = 0;
  40. while (t--)
  41. {
  42. memset(node,0,sizeof(node));
  43. int n,q;
  44. scanf("%d%d",&n,&q);
  45. build(1,n,1);
  46. printf("Case %d:\n",++cnt);
  47. for (int i = 0;i < q;i++)
  48. {
  49. int x,y;
  50. scanf("%d%d",&x,&y);
  51. int ans = query(1,x,y);
  52. printf("%d\n",ans);
  53. }
  54. }
  55. return 0;
  56. }

F - Farthest Nodes in a Tree
题意:
给出一棵树,找出任意两点间距离的最大值。
思路:
求一棵树的直径。结论:从任意一个点总能找到距离它最远的点,此点即是最长路的终点。
所以两次bfs,第一次bfs找到最长路终点,第二次算出终点到所有点的距离,最后找出最大值即可。
代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <vector>
  4. #include <map>
  5. #include <queue>
  6. using namespace std;
  7. int d[30005];
  8. typedef pair<int,int> p;
  9. map<p,int> mmp;
  10. vector<int> g[30005];
  11. bool vis[30005];
  12. int main()
  13. {
  14. int t;
  15. scanf("%d",&t);
  16. int cnt = 0;
  17. while (t--)
  18. {
  19. int n;
  20. mmp.clear();
  21. memset(g,0,sizeof(g));
  22. memset(vis,0,sizeof(vis));
  23. scanf("%d",&n);
  24. for (int i = 0;i < n-1;i++)
  25. {
  26. int x,y,z;
  27. scanf("%d%d%d",&x,&y,&z);
  28. mmp[p(x,y)] = mmp[p(y,x)] = z;
  29. g[x].push_back(y);
  30. g[y].push_back(x);
  31. }
  32. memset(d,0,sizeof(d));
  33. queue<int> s;
  34. s.push(0);
  35. vis[0] = 1;
  36. while (!s.empty())
  37. {
  38. int c = s.front();s.pop();
  39. for (int i = 0;i < g[c].size();i++)
  40. {
  41. if (!vis[g[c][i]])
  42. {
  43. int k = g[c][i];
  44. d[k] = d[c] + mmp[p(c,k)];
  45. vis[k] = 1;
  46. s.push(k);
  47. }
  48. }
  49. }
  50. int m;
  51. int maxnn = 0;
  52. for (int i = 0;i < n;i++)
  53. {
  54. if (maxnn < d[i])
  55. {
  56. m = i;
  57. maxnn = d[i];
  58. }
  59. }
  60. memset(vis,0,sizeof(vis));
  61. memset(d,0,sizeof(d));
  62. s.push(m);
  63. vis[m] = 1;
  64. while (!s.empty())
  65. {
  66. int c = s.front();s.pop();
  67. for (int i = 0;i < g[c].size();i++)
  68. {
  69. if (!vis[g[c][i]])
  70. {
  71. int k = g[c][i];
  72. d[k] = d[c] + mmp[p(c,k)];
  73. vis[k] = 1;
  74. s.push(k);
  75. }
  76. }
  77. }
  78. maxnn = 0;
  79. for (int i = 0;i < n;i++)
  80. {
  81. if (maxnn < d[i])
  82. {
  83. m = i;
  84. maxnn = d[i];
  85. }
  86. }
  87. printf("Case %d: %d\n",++cnt,maxnn);
  88. }
  89. return 0;
  90. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注