A

  1. /*Huyyt*/
  2. #include<bits/stdc++.h>
  3. #define mem(a,b) memset(a,b,sizeof(a))
  4. #define pb push_back
  5. using namespace std;
  6. typedef long long ll;
  7. typedef unsigned long long ull;
  8. const ll LLmaxn = 2e18;
  9. int main()
  10. {
  11. string a;
  12. cin >> a;
  13. int b=, c=;
  14. for (int i = ; i < a.size(); i++)
  15. {
  16. if (a[i] == '-')
  17. {
  18. b++;
  19. }
  20. else
  21. {
  22. c++;
  23. }
  24. }
  25. //cout<<b<<" "<<c<<endl;
  26. if (c == )
  27. {
  28. cout << "YES" << endl;
  29. return ;
  30. }
  31. if (b % c != )
  32. {
  33. cout << "NO" << endl;
  34. }
  35. else
  36. {
  37. cout << "YES" << endl;
  38. }
  39. return ;
  40. }

B

解:

注意只要是上下对称或者是左右对称就可以使得 1-4 有一条路径的话 2-3 也有相对应的一条路径

剩下的就容易构造了

  1. /*Huyyt*/
  2. #include<bits/stdc++.h>
  3. #define mem(a,b) memset(a,b,sizeof(a))
  4. #define pb push_back
  5. using namespace std;
  6. typedef long long ll;
  7. typedef unsigned long long ull;
  8. const ll LLmaxn = 2e18;
  9. char ans[][];
  10. int main()
  11. {
  12. int n, k;
  13. cin >> n >> k;
  14. for (int i = ; i <= ; i++)
  15. {
  16. for (int j = ; j <= n; j++)
  17. {
  18. ans[i][j] = '.';
  19. }
  20. }
  21. int sum = (n - ) * ;
  22. if (k > sum)
  23. {
  24. cout << "NO" << endl;
  25. return ;
  26. }
  27. cout << "YES" << endl;
  28. if (k % == )
  29. {
  30. int cur = k;
  31. for (int j = ; j <= n - && cur; j++)
  32. {
  33. for (int i = ; i <= && cur; i++)
  34. {
  35. ans[i][j] = '#';
  36. cur--;
  37. }
  38. }
  39. }
  40. else
  41. {
  42. k--;
  43. ans[][n / + ] = '#';
  44. for (int i = ; i <= && k; i++)
  45. {
  46. for (int j = ; j <= n / && k; j++)
  47. {
  48. ans[i][j] = ans[i][n - j + ] = '#';
  49. k -= ;
  50. }
  51. }
  52. }
  53. for (int i = ; i <= ; i++)
  54. {
  55. for (int j = ; j <= n; j++)
  56. {
  57. cout << ans[i][j];
  58. }
  59. cout << endl;
  60. }
  61. return ;
  62. }

C

解:

可以用并查集维护每个点最小到达的地方 也因为N<=1E5,K<=256 直接暴力模拟也可以

  1. /*Huyyt*/
  2. #include<bits/stdc++.h>
  3. #define mem(a,b) memset(a,b,sizeof(a))
  4. #define pb push_back
  5. using namespace std;
  6. typedef long long ll;
  7. typedef unsigned long long ull;
  8. const ll LLmaxn = 2e18;
  9. char ans[][];
  10. int main()
  11. {
  12. int n, k;
  13. cin >> n >> k;
  14. for (int i = ; i <= ; i++)
  15. {
  16. for (int j = ; j <= n; j++)
  17. {
  18. ans[i][j] = '.';
  19. }
  20. }
  21. int sum = (n - ) * ;
  22. if (k > sum)
  23. {
  24. cout << "NO" << endl;
  25. return ;
  26. }
  27. cout << "YES" << endl;
  28. if (k % == )
  29. {
  30. int cur = k;
  31. for (int j = ; j <= n - && cur; j++)
  32. {
  33. for (int i = ; i <= && cur; i++)
  34. {
  35. ans[i][j] = '#';
  36. cur--;
  37. }
  38. }
  39. }
  40. else
  41. {
  42. k--;
  43. ans[][n / + ] = '#';
  44. for (int i = ; i <= && k; i++)
  45. {
  46. for (int j = ; j <= n / && k; j++)
  47. {
  48. ans[i][j] = ans[i][n - j + ] = '#';
  49. k -= ;
  50. }
  51. }
  52. }
  53. for (int i = ; i <= ; i++)
  54. {
  55. for (int j = ; j <= n; j++)
  56. {
  57. cout << ans[i][j];
  58. }
  59. cout << endl;
  60. }
  61. return ;
  62. }

D

卡题意...

给出一个数组,把里面的数字分组,使得每一个组里面的数两两相乘都是完全平方数.

问最少可以分成的组数k是多少.

现在一个人有一个数组,他想知道这个数组的连续子数组中,使得上面的问题答案分别为1到n的数组有多少个.

第一种做法:

注意当一个数X的因数有完全平方数Y的时候 把这个数替换为X/Y并不影响结果

当一个数是0的时候要特判 要因为0乘任何数都是0可以加入任意一组

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <set>
  5. #include <math.h>
  6. using namespace std;
  7. const int N = ;
  8. int a[N];
  9. int b[N];
  10. int c[N];
  11. bool vis[N];
  12. int mabs(int x)
  13. {
  14. return x >= ? x : -x;
  15. }
  16. int main()
  17. {
  18. int n;
  19. int cnt = ;
  20. scanf("%d",&n);
  21. for (int i = ;i < n;i++)
  22. {
  23. scanf("%d",&a[i]);
  24. if (a[i] != ) cnt++;
  25. }
  26. if (cnt == )
  27. {
  28. printf("%d ",n * (n + ) / );
  29. for (int i = ;i < n;i++) printf("0 ");
  30. }
  31. else
  32. {
  33. for (int i = ;i < n;i++)
  34. {
  35. if (a[i] == ) continue;
  36. int tmp = mabs(a[i]);
  37. for (int j = ;j * j <= tmp;j++)
  38. {
  39. int t = j * j;
  40. while (a[i] % t == )
  41. {
  42. a[i] /= t;
  43. }
  44. //if (a[i] < t) break;加了这个就wa,卡了一晚上,考虑的应该是绝对值的情况
  45. }
  46. }
  47. for (int i = ;i < n;i++) c[i] = a[i];
  48. sort(c,c+n);
  49. int js = unique(c,c+n) - c;
  50. for (int i = ;i < n;i++)
  51. {
  52. if (a[i] == ) continue;
  53. int p = lower_bound(c,c+js,a[i]) - c + ;
  54. a[i] = p;
  55. }
  56. for (int i = ;i < n;i++)
  57. {
  58. memset(vis,,sizeof(vis));
  59. int num = ;
  60. for (int j = i;j < n;j++)
  61. {
  62. if (!vis[a[j]] && a[j] != )
  63. {
  64. num++;
  65. vis[a[j]] = ;
  66. }
  67. int tt = max(num,);
  68. b[tt]++;
  69. }
  70. }
  71. for (int i = ;i <= n;i++)
  72. {
  73. printf("%d ",b[i]);
  74. }
  75. }
  76. return ;
  77. }

第二种做法:

可以证明当A*B为完全平方数B*C也为完全平方数时 A*C也是完全平方数

所以只要N^2遍历 用并查集维护每个数属于哪个集合就行 注意0可以属于任意一个集合

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N = ;
  4. long long a[N];
  5. int ans[N], fa[N], d[N];
  6. int bz[N];
  7. int n, sum, tot;
  8. bool check(long long x)
  9. {
  10. if (x < )
  11. {
  12. return ;
  13. }
  14. long long y = sqrt(x);
  15. return y * y == x || (y - ) * (y - ) == x || (y + ) * (y + ) == x;
  16. }
  17. int main()
  18. {
  19. ios::sync_with_stdio(false);
  20. cin >> n;
  21. for (int i = ; i <= n; i++)
  22. {
  23. fa[i] = i;
  24. }
  25. for (int i = ; i <= n; i++)
  26. {
  27. cin >> a[i];
  28. if (a[i])
  29. {
  30. for (int j = ; j <= d[]; j++)
  31. if (check(a[i]*a[d[j]]))
  32. {
  33. fa[i] = d[j];
  34. break;
  35. }
  36. if (fa[i] == i)
  37. {
  38. d[++d[]] = i;
  39. }
  40. }
  41. }
  42. for (int i = ; i <= n; i++)
  43. {
  44. sum = ;
  45. tot++;
  46. for (int j = i; j <= n; j++)
  47. {
  48. if (a[j] && bz[fa[j]] != tot)
  49. {
  50. bz[fa[j]] = tot, sum++;
  51. }
  52. ans[max(sum, )]++;
  53. }
  54. }
  55. for (int i = ; i <= n; i++)
  56. {
  57. cout << ans[i] << ' ';
  58. }
  59. return ;
  60. }

E

给你N个点 组成的一颗树 分别从1标号到N 每个点的粉丝数量为2^i个

要求是选择K个点删除 使得剩下没被删的点保持连通且剩下的粉丝数量最大

一旦某个点被删除则其不能通过且该点的粉丝数量清零

假如做法顺着做 找出需要删除那些点的话 因为要保证连通性所以删除一个点需要删除掉他所有子树的点 不好做

题目提示你K<N 所以点N是一定可以保留的 就以N为根倍增预处理祖先 倒着做 找出不需要删除的点即可

  1. /* Huyyt */
  2. #include <bits/stdc++.h>
  3. #define mem(a,b) memset(a,b,sizeof(a))
  4. #define mkp(a,b) make_pair(a,b)
  5. #define pb push_back
  6. using namespace std;
  7. typedef long long ll;
  8. const long long mod = 1e9 + ;
  9. const int N = 1e6 + ;
  10. inline int readint()
  11. {
  12. char c = getchar();
  13. int ans = ;
  14. while (c < '' || c > '')
  15. {
  16. c = getchar();
  17. }
  18. while (c >= '' && c <= '')
  19. {
  20. ans = ans * + c - '', c = getchar();
  21. }
  22. return ans;
  23. }
  24. vector<int> tree[N];
  25. bool check[N];
  26. int father[N][];
  27. int deep[N];
  28. void dfs(int x, int level)
  29. {
  30. for (int i = ; father[father[x][i]][i]; i++)
  31. {
  32. father[x][i + ] = father[father[x][i]][i];
  33. }
  34. deep[x] = level;
  35. for (int i = ; i < tree[x].size(); i++)
  36. {
  37. int to = tree[x][i];
  38. if (to == father[x][])
  39. {
  40. continue;
  41. }
  42. father[to][] = x;
  43. dfs(to, level + );
  44. }
  45. }
  46. int main()
  47. {
  48. int n, k;
  49. n = readint(), k = readint();
  50. //cout << n << " " << k << endl;
  51. int u, v;
  52. for (int i = ; i < n; i++)
  53. {
  54. u = readint(),v = readint();
  55. tree[u].pb(v);
  56. tree[v].pb(u);
  57. }
  58. k = n - k;
  59. k--, check[n] = ;
  60. dfs(n, );
  61. for (int i = n - ; i >= && k; i--)
  62. {
  63. int aim = -;
  64. int now = i;
  65. if (check[i])
  66. {
  67. continue;
  68. }
  69. for (int j = ; j >= ; j--)
  70. {
  71. if (father[now][j] == || check[father[now][j]])
  72. {
  73. continue;
  74. }
  75. now = father[now][j];
  76. }
  77. if (deep[i] - deep[now] + <= k)
  78. {
  79. now = i;
  80. while (now != && !check[now])
  81. {
  82. check[now] = ;
  83. k--;
  84. now = father[now][];
  85. }
  86. }
  87. }
  88. for (int i = ; i <= n - ; i++)
  89. {
  90. if (!check[i])
  91. {
  92. cout << i << " ";
  93. }
  94. }
  95. return ;
  96. }

Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点的更多相关文章

  1. noip模拟赛 动态仙人掌(并查集,贪心)

    思路: 贪心+并查集 因为45‘,所以可以很方便的算出每个仙人掌的最晚起跳(左端点) 右端点自然也能出来 先按左端点排序 如果他右面的和他相交,就更新 用并查集维护这个更新的关系 更新的同时维护高就好 ...

  2. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  3. hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目大意: 有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种 ...

  4. CodeForces - 1209D 并查集

    题意: 有 n个不同的糖果,从 1到 n编号.有 k个客人.要用糖果招待客人.对于每个客人,这些糖果中恰有两个是其最爱.第 i个客人最爱的糖果编号是 xi和 y.将 k 个客人任意排列,他们按顺序去拿 ...

  5. Vladik and Entertaining Flags CodeForces - 811E (并查集,线段树)

    用线段树维护每一块左右两侧的并查集, 同色合并时若不连通则连通块数-1, 否则不变 #include <iostream> #include <algorithm> #incl ...

  6. CodeForces - 893C-Rumor(并查集变式)

    Vova promised himself that he would never play computer games... But recently Firestorm - a well-kno ...

  7. 洛谷 - P4997 - 不围棋 - 并查集 - 模拟

    https://www.luogu.org/problemnew/show/P4997 首先是改变气的定义,使得容易计算,这个很好理解. 然后使用并查集,因为要维护整个连通块的性质. 最后的难点在于, ...

  8. 0-1-Tree CodeForces - 1156D (并查集)

    大意: 给定树, 边权为黑或白, 求所有有向路径条数, 满足每走过一条黑边后不会走白边. 这题比赛的时候想了个假算法, 还没发现..... 显然所求的路径要么全黑, 要么全白, 要么先全白后全黑, 所 ...

  9. Codeforces 1166F 并查集 启发式合并

    题意:给你一张无向图,无向图中每条边有颜色.有两种操作,一种是询问从x到y是否有双彩虹路,一种是在x到y之间添加一条颜色为z的边.双彩虹路是指:如果给这条路径的点编号,那么第i个点和第i - 1个点相 ...

随机推荐

  1. C# 利用AForge.NET开源类库实现 图像素描效果

    引入DLL: using AForge.Imaging; using AForge.Imaging.Filters; //using AForge.Video.DirectShow;可以使用摄像头图像 ...

  2. IDEA项目添加第三方依赖

    1.在项目WEB-INF文件夹下面新建一个lib文件夹并将我们需要的第三方jar包拷贝进去: Ctrl+Shift+Alt+S打开项目配置页面: 选择我们存放jar包的文件夹: 选择我们需要引入目标依 ...

  3. CSS样式属性单词之Left

    通常left单独在CSS中设置无效,需要在使用position属性下使用才能生效起作用.left左靠多少距离(距离左边多少)的作用. left 一.left认识与语法 left翻译:左边,左 在CSS ...

  4. Eureka入门一(了解概念)

    Eureka注册中心(8761端口) IDEA(开发工具) 1,创建项目勾选Eureka Server 2, 创建yml文件,拷贝配置,下面配置必须为false,意为,该项目不要作为客户端注册,因为本 ...

  5. 关于函数中参数kwarg.setdefault()的用法

    1.kwarg.setdefault() setdefault()是给字典传递一个默认值    当给字典传递数据时 setdefault()是字典默认的一个参数,当有新的数据时,就会使用新的数据

  6. 【图形学手记】Inverse Transform Sampling 逆转换抽样

    需求: 我们通过调查,得知大多数人在20岁左右初恋,以20岁为基准,以随机变量X表示早于或晚于该时间的年数,为了简单,假设X值域为[-5,5],并且PDF(X)是一个正态分布函数(当然可以为任意分布, ...

  7. 【ABAP系列】SAP ABAP获取域(domain)值的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP获取域(doma ...

  8. spring boot 数据源配置YAML格式

    spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf- ...

  9. 使用new关键字创建对象数组(C#,C++,Java)

    今天遇到一个题目 分析下面的代码,判断代码是否有误. using System; namespace Test1 { class Point { public int x; public int y; ...

  10. 【Linux 网络编程】MTU(Maximum Transmission Uint)

    (1)以太网和IEEE802.3对数据帧的长度都是有限制的,其最大分别是1500和1492字节,成为MTU. (2)如果IP层有一个数据要传输,而且数据的长度比链路层的MTU要大,那么IP层就要进行分 ...