牛客网提高组Day2

T1 方差

  第一眼看就知道要打暴力啊,然而并没有想到去化简式子。。。

  可能因为昨晚没睡好,今天上午困死

  导致暴力打了一个半小时,还不对。。。

  1. #include <algorithm>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cmath>
  5. using namespace std;
  6. typedef long long LL;
  7. const int M = ;
  8. int n, m;
  9. double sum;
  10. LL a[M], s[M], f[M];
  11.  
  12. double mul(double a, int b) {
  13. double res = 0.0;
  14. while (b) {
  15. if (b & ) res += a;
  16. a += a;
  17. b >>= ;
  18. }
  19. return ;
  20. }
  21.  
  22. int main() {
  23. scanf("%d", &n);
  24. m = n - ;
  25. for (int i = ; i <= n; i++) {
  26. scanf("%d", &a[i]);
  27. sum += a[i];
  28. }
  29. for (int i = ; i <= n; i++) {
  30. if (f[a[i]] != ) {
  31. if(i != n) printf("%lld ", f[a[i]]);
  32. else return printf("%lld\n", f[a[i]]), ;
  33. continue;
  34. }
  35. double tmp = 1.0 * (sum - a[i]) / m;
  36. double ss;
  37. for (int j = ; j <= n; j++) {
  38. if (i == j) continue;
  39. double t = abs(a[j] - tmp);
  40. t = 1.0 * t * t;
  41. ss += t;
  42. }
  43. f[a[i]] = mul(ss, m);
  44. if(i != n) printf("%lld ", f[a[i]]);
  45. else printf("%lld\n", f[a[i]]);
  46. }
  47. return ;
  48. }

假的暴力

正解:

  题中公式可进行化简,转化为只需维护序列元素的和,平方和即可

  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4. const int M = ;
  5. int n;
  6. long long s, s2, ans;
  7. long long a[M];
  8.  
  9. int main() {
  10. scanf("%d", &n);
  11. for (int i = ; i <= n; i++) {
  12. scanf("%lld", &a[i]);
  13. s += a[i];
  14. s2 += a[i] * a[i];
  15. }
  16. for (int i = ; i <= n; i++) {
  17. ans = (n - ) * (s2 - a[i] * a[i]) - (s - a[i]) * (s - a[i]);
  18. printf("%lld ", ans);
  19. }
  20. return ;
  21. }

T2 分糖果

  暴力搜索 然而并没有分 qwq。。。

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdio>
  4. using namespace std;
  5. const int M = ;
  6. int n, ans;
  7. int f[M], a[M], vis[M];
  8.  
  9. void dfs(int step) {
  10. if(step == n + ) ans++;
  11. else for(int j = ; j <= f[step]; j++) {
  12. if(!vis[j] && step == n && (a[step - ] != j) && a[] != j) {
  13. a[step] = j;
  14. dfs(step + );
  15. }
  16. else if(!vis[j] && (a[step - ] != j) && step != n) {
  17. a[step] = j;
  18. dfs(step + );
  19. }
  20. }
  21.  
  22. }
  23.  
  24. int main() {
  25. scanf("%d", &n);
  26. for(int i = ; i <= n; i++)
  27. scanf("%d", &f[i]);
  28. dfs();
  29. printf("%d\n", ans);
  30. return ;
  31. }

暴力

正解:

  

暴力复杂度为O(n^2),所以考虑优化:

  线段树优化:O(nlogn) 80pts

  单调队列优化:O(n) 100pts

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int M = ;
  4. const int P = 1e9 + ;
  5. int n, B[M], A[M];
  6. int dp[M], sum[];
  7. int q[][M], val[][M], l[], r[];
  8.  
  9. void insert(int f, int x, int s) {
  10. while (l[f] < r[f] && q[f][r[f] - ] >= x) {
  11. r[f]--;
  12. s = (s + val[f][r[f]]) % P;
  13. sum[f] = (sum[f] - 1ll * val[f][r[f]] * q[f][r[f]] % P + P) % P;
  14. }
  15. val[f][r[f]] = s;
  16. q[f][r[f]++] = x;
  17. sum[f] = (sum[f] + 1ll * s * x % P) % P;
  18. s = ;
  19. while (l[ - f] < r[ - f] && q[ - f][r[ - f] - ] >= x) {
  20. r[ - f]--;
  21. s = (s + val[ - f][r[ - f]]) % P;
  22. sum[ - f] = (sum[ - f] - 1ll * val[ - f][r[ - f]] * q[ - f][r[ - f]] % P + P) % P;
  23. }
  24. if (s) {
  25. val[ - f][r[ - f]] = s;
  26. q[ - f][r[ - f]++] = x;
  27. sum[ - f] = (sum[ - f] + 1ll * s * x % P) % P;
  28. }
  29. }
  30.  
  31. int main() {
  32. scanf("%d", &n);
  33. int mi = , ans = ;
  34. for (int i = ; i <= n; i++) {
  35. scanf("%d", &B[i]);
  36. if (B[mi] > B[i])mi = i;
  37. }
  38. int len = ;
  39. for (int i = mi; i <= n; i++) A[++len] = B[i];
  40. for (int i = ; i < mi; i++) A[++len] = B[i];
  41. dp[] = ;
  42. l[] = r[] = l[] = r[] = ;
  43. for (int i = ; i <= n; i++) {
  44. int mi = A[i];
  45. int f = i & ;
  46. insert(i & , A[i], dp[i - ]);
  47. dp[i] = (sum[f] - sum[ - f] + P) % P;
  48. if (i > ) ans = (dp[i] - ans + P) % P;
  49. }
  50. printf("%d\n", ans);
  51. return ;
  52. }

T3 集合划分

  直接输出“-1”没有分  差评。。

正解:

  

  1. #include<bits/stdc++.h>
  2. #define gc getchar()
  3. #define pc putchar
  4. using namespace std;
  5. typedef long long li;
  6. const int M = ;
  7. bool fg[M], vst[M], pt[];
  8. int n, m, k, mx, a[];
  9. int h, t, ft, st[];
  10. int q[M], f[M], tj[M], lst[M];
  11. int as[M];
  12. li s1 = , s2 = ;
  13. li s3 = , srd;
  14.  
  15. li read() {
  16. li x = , y = , c = gc;
  17. while (!isdigit(c)) y = c, c = gc;
  18. while (isdigit(c)) x = (x << ) + (x << ) + (c ^ ''), c = gc;
  19. return y == '-' ? -x : x;
  20. }
  21.  
  22. void print(li q) {
  23. if (q < ) {
  24. pc('-');
  25. q = -q;
  26. }
  27. if (q >= ) print(q / );
  28. pc(q % + '');
  29. }
  30.  
  31. li rd() {
  32. return srd = (srd * s1 + s2 + rand()) % s3;
  33. }
  34.  
  35. int main() {
  36. srand(time());
  37. rd();
  38. int i, j, l;
  39. n = read(); m = read(); k = read();
  40. mx = ( << n) - ;
  41. for (i = ; i <= m; ++i)
  42. a[i] = read(), f[a[i]] = a[i];
  43. if (m > k) return puts("-1"), ;
  44. for (i = ; i <= mx; ++i)
  45. tj[i] = tj[i >> ] + (i & );
  46. for (i = ; i <= mx; i <<= )
  47. for (j = ; j <= mx; j += (i << ))
  48. for (l = j; l < j + i; ++l)
  49. f[l + i] |= f[l];
  50. int q1 = , q2 = ;
  51. for (i = ; i <= mx; ++i) fg[i] = ;
  52. for (i = ; i <= n; ++i) {
  53. if (k & ( << n - i)) ++q1;
  54. else ++q2;
  55. for (j = ; j <= mx; ++j)
  56. if (n - tj[j] == q1 && tj[j] - tj[f[j]] < q2)
  57. fg[j] = ;
  58. }
  59. if (!fg[mx]) {
  60. puts("-1");
  61. return ;
  62. }
  63. int nw, nxt;
  64. q[++t] = mx;
  65. vst[mx] = ;
  66. while (h < t) {
  67. nw = q[++h];
  68. for (i = ; i <= n; ++i)
  69. if (nw & ( << i - )) {
  70. nxt = nw ^ ( << i - );
  71. if (!fg[nxt] || vst[nxt]) continue;
  72. vst[nxt] = ;
  73. lst[nxt] = nw;
  74. q[++t] = nxt;
  75. }
  76.  
  77. }
  78. if (!vst[]) {
  79. puts("-1");
  80. return ;
  81. }
  82. for (nw = ; nw != mx; nw = lst[nw])
  83. st[++ft] = nw;
  84. st[++ft] = mx;
  85. for (i = ; i <= n; ++i) {
  86. if (k & ( << n - i)) {
  87. while (pt[ft - ]) --ft;
  88. nw = st[ft] ^ st[ft - ];
  89. --ft;
  90. for (j = ; j <= mx; ++j)
  91. if (!as[j] && (j & nw)) as[j] = ;
  92. }
  93. else {
  94. l = ;
  95. for (j = ; j <= m; ++j)
  96. if (!as[a[j]]) l |= a[j];
  97. for (j = ; j < ft; ++j)
  98. if (!pt[j] && ((st[j] ^ st[j + ]) & l) == ) {
  99. nw = st[j] ^ st[j + ];
  100. pt[j] = ;
  101. break;
  102. }
  103. for (j = ; j <= mx; ++j)
  104. if (!as[j] && (j & nw)) as[j] = ;
  105. }
  106. }
  107. for(i = ; i <= mx; ++i)
  108. pc(as[i] - + '');
  109. pc('\n');
  110. return ;
  111. }

18/9/16牛客网提高组Day2的更多相关文章

  1. 18/9/9牛客网提高组Day1

    牛客网提高组Day1 T1 中位数 这好像是主席树??听说过,不会啊... 最后只打了个暴力,可能是n2logn? 只过了前30%  qwq #include<algorithm> #in ...

  2. 牛客网 提高组第8周 T1 染色

    染色 链接: https://ac.nowcoder.com/acm/contest/176/A 来源:牛客网 题目描述 \(\tt{fizzydavid}\)和\(\tt{leo}\)有\(n\)个 ...

  3. 牛客网 提高组第8周 T2 推箱子 解题报告

    推箱子 链接: https://ac.nowcoder.com/acm/contest/176/B 来源:牛客网 题目描述 在平面上有\(n\)个箱子,每个箱子都可以看成一个矩形,两条边都和坐标轴平行 ...

  4. nowcoder(牛客网)提高组模拟赛第一场 解题报告

    T1 中位数(二分) 这个题是一个二分(听说是上周atcoder beginner contest的D题???) 我们可以开一个数组b存a,sort然后二分b进行check(从后往前直接遍历check ...

  5. nowcoder(牛客网)提高组模拟赛第四场 解题报告

    T1 动态点分治 就是模拟..... 但是没有过!! 看了题解之后发现.... 坑点:有可能 \(x<=r\),但是

  6. 牛客网提高组模拟赛第七场 T3 洞穴(附bitset介绍)

    就是DP. 我们可以很简单的想到要枚举中间点,进行边数的转移. 但是因为边长数据范围很大,所以我们考虑log的倍增. 状态设计为\(dp[i][j][k]\),为从节点\(i\)走\(2^k\)步能否 ...

  7. 牛客网提高组模拟赛第七场 T2 随机生成树

    其实看懂题就很水啦qwq,就是求\(1-N\)的约数啦. 暴力求的话时间复杂度是\(O(NlogN)\)的,其实正解是枚举每个数的倍数......这样的时间复杂度是\(\frac{N}{1}+\fra ...

  8. 牛客网提高组模拟赛第五场 T1同余方程(异或)(位运算)

    区间不好做,但是我们可以转化成前缀来做.转化为前缀之后之后就是二维前缀和. 但是我还是不怎么会做.所以只能去看吉老师的题解 (确定写的那么简单真的是题解???). 我们要求模一个数余0,就等于找它的倍 ...

  9. 牛客网提高组第二场---solution

    T1 方差 根据题目要求将式子先写出来注意下面式子中的 $n$ 全部都是 $n-1$$$\begin{aligned}ans&=n^2\times \frac{1}{n}\times \sum ...

随机推荐

  1. python in操作引发 TypeError

    在看 networkx 源代码的时候认为疑惑.为什么外层 for 要注意 TypeError.里面就不用.相同是 in, 一直纠结 node 是不是有问题,比方 node 不能够被迭代什么的,那么里面 ...

  2. 关于android studio几种常见的错误解决

    我也是从ec转到as的,没办法,大势所趋嘛,然而,在使用as的过程中遇到了非常多匪夷所思的错误,如今就说一下今天我遇到的这个错误. 美工妹子给了我一张图片,用来当做button的背景图,当然,这个图也 ...

  3. How to search Installed Updates

    Windows本身的控制面板中自带的搜索,无法根据补丁编号进行搜索 可以将补丁信息导出到文本,再用文本编辑器进行查找 https://www.concurrency.com/blog/w/search ...

  4. .ds_store是什么文件

    .ds_store是什么文件 .DS_Store是Mac OS保存文件夹的自定义属性的隐藏文件,如文件的图标位置或背景色,相当于Windows的desktop.ini. 1,禁止.DS_store生成 ...

  5. 远程带参数POST访问接口,返回数据

    1. string token = GetRequest.GetString("token"); int customer_id = GetRequest.GetInt(" ...

  6. css3 实现动画效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Ubunut PPA源概述

    Ubuntu 自带的“软件”应用,可以安装海量软件,既包括发行者支持的软件.社区支持的软件,也包括专有驱动和版权软件.有时,我们需要的软件通过这些渠道仍然无法找到.这时,可以到 PPA 软件源中查找. ...

  8. CentOS7 PXE安装批量安装操作系统

    1.安装相关软件 yum -y install tftp-server httpd dhcp syslinux 2.配置DHCP cp /usr/share/doc/dhcp-4.2.5/dhcpd. ...

  9. ssm框架的多表查询和增删查改

    必须声明本文章==>http://www.cnblogs.com/zhu520/p/7883273.html 一: 1):我的运行环境 我使用myeclipse(你也可以使用eclipse),t ...

  10. 洛谷 P2690 接苹果

    P2690 接苹果 题目背景 USACO 题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从 ...