18/9/16牛客网提高组Day2
牛客网提高组Day2
T1 方差
第一眼看就知道要打暴力啊,然而并没有想到去化简式子。。。
可能因为昨晚没睡好,今天上午困死
导致暴力打了一个半小时,还不对。。。
- #include <algorithm>
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- using namespace std;
- typedef long long LL;
- const int M = ;
- int n, m;
- double sum;
- LL a[M], s[M], f[M];
- double mul(double a, int b) {
- double res = 0.0;
- while (b) {
- if (b & ) res += a;
- a += a;
- b >>= ;
- }
- return ;
- }
- int main() {
- scanf("%d", &n);
- m = n - ;
- for (int i = ; i <= n; i++) {
- scanf("%d", &a[i]);
- sum += a[i];
- }
- for (int i = ; i <= n; i++) {
- if (f[a[i]] != ) {
- if(i != n) printf("%lld ", f[a[i]]);
- else return printf("%lld\n", f[a[i]]), ;
- continue;
- }
- double tmp = 1.0 * (sum - a[i]) / m;
- double ss;
- for (int j = ; j <= n; j++) {
- if (i == j) continue;
- double t = abs(a[j] - tmp);
- t = 1.0 * t * t;
- ss += t;
- }
- f[a[i]] = mul(ss, m);
- if(i != n) printf("%lld ", f[a[i]]);
- else printf("%lld\n", f[a[i]]);
- }
- return ;
- }
假的暴力
正解:
题中公式可进行化简,转化为只需维护序列元素的和,平方和即可
- #include<iostream>
- #include<cstdio>
- using namespace std;
- const int M = ;
- int n;
- long long s, s2, ans;
- long long a[M];
- int main() {
- scanf("%d", &n);
- for (int i = ; i <= n; i++) {
- scanf("%lld", &a[i]);
- s += a[i];
- s2 += a[i] * a[i];
- }
- for (int i = ; i <= n; i++) {
- ans = (n - ) * (s2 - a[i] * a[i]) - (s - a[i]) * (s - a[i]);
- printf("%lld ", ans);
- }
- return ;
- }
T2 分糖果
暴力搜索 然而并没有分 qwq。。。
- #include <algorithm>
- #include <iostream>
- #include <cstdio>
- using namespace std;
- const int M = ;
- int n, ans;
- int f[M], a[M], vis[M];
- void dfs(int step) {
- if(step == n + ) ans++;
- else for(int j = ; j <= f[step]; j++) {
- if(!vis[j] && step == n && (a[step - ] != j) && a[] != j) {
- a[step] = j;
- dfs(step + );
- }
- else if(!vis[j] && (a[step - ] != j) && step != n) {
- a[step] = j;
- dfs(step + );
- }
- }
- }
- int main() {
- scanf("%d", &n);
- for(int i = ; i <= n; i++)
- scanf("%d", &f[i]);
- dfs();
- printf("%d\n", ans);
- return ;
- }
暴力
正解:
暴力复杂度为O(n^2),所以考虑优化:
线段树优化:O(nlogn) 80pts
单调队列优化:O(n) 100pts
- #include<bits/stdc++.h>
- using namespace std;
- const int M = ;
- const int P = 1e9 + ;
- int n, B[M], A[M];
- int dp[M], sum[];
- int q[][M], val[][M], l[], r[];
- void insert(int f, int x, int s) {
- while (l[f] < r[f] && q[f][r[f] - ] >= x) {
- r[f]--;
- s = (s + val[f][r[f]]) % P;
- sum[f] = (sum[f] - 1ll * val[f][r[f]] * q[f][r[f]] % P + P) % P;
- }
- val[f][r[f]] = s;
- q[f][r[f]++] = x;
- sum[f] = (sum[f] + 1ll * s * x % P) % P;
- s = ;
- while (l[ - f] < r[ - f] && q[ - f][r[ - f] - ] >= x) {
- r[ - f]--;
- s = (s + val[ - f][r[ - f]]) % P;
- sum[ - f] = (sum[ - f] - 1ll * val[ - f][r[ - f]] * q[ - f][r[ - f]] % P + P) % P;
- }
- if (s) {
- val[ - f][r[ - f]] = s;
- q[ - f][r[ - f]++] = x;
- sum[ - f] = (sum[ - f] + 1ll * s * x % P) % P;
- }
- }
- int main() {
- scanf("%d", &n);
- int mi = , ans = ;
- for (int i = ; i <= n; i++) {
- scanf("%d", &B[i]);
- if (B[mi] > B[i])mi = i;
- }
- int len = ;
- for (int i = mi; i <= n; i++) A[++len] = B[i];
- for (int i = ; i < mi; i++) A[++len] = B[i];
- dp[] = ;
- l[] = r[] = l[] = r[] = ;
- for (int i = ; i <= n; i++) {
- int mi = A[i];
- int f = i & ;
- insert(i & , A[i], dp[i - ]);
- dp[i] = (sum[f] - sum[ - f] + P) % P;
- if (i > ) ans = (dp[i] - ans + P) % P;
- }
- printf("%d\n", ans);
- return ;
- }
T3 集合划分
直接输出“-1”没有分 差评。。
正解:
- #include<bits/stdc++.h>
- #define gc getchar()
- #define pc putchar
- using namespace std;
- typedef long long li;
- const int M = ;
- bool fg[M], vst[M], pt[];
- int n, m, k, mx, a[];
- int h, t, ft, st[];
- int q[M], f[M], tj[M], lst[M];
- int as[M];
- li s1 = , s2 = ;
- li s3 = , srd;
- li read() {
- li x = , y = , c = gc;
- while (!isdigit(c)) y = c, c = gc;
- while (isdigit(c)) x = (x << ) + (x << ) + (c ^ ''), c = gc;
- return y == '-' ? -x : x;
- }
- void print(li q) {
- if (q < ) {
- pc('-');
- q = -q;
- }
- if (q >= ) print(q / );
- pc(q % + '');
- }
- li rd() {
- return srd = (srd * s1 + s2 + rand()) % s3;
- }
- int main() {
- srand(time());
- rd();
- int i, j, l;
- n = read(); m = read(); k = read();
- mx = ( << n) - ;
- for (i = ; i <= m; ++i)
- a[i] = read(), f[a[i]] = a[i];
- if (m > k) return puts("-1"), ;
- for (i = ; i <= mx; ++i)
- tj[i] = tj[i >> ] + (i & );
- for (i = ; i <= mx; i <<= )
- for (j = ; j <= mx; j += (i << ))
- for (l = j; l < j + i; ++l)
- f[l + i] |= f[l];
- int q1 = , q2 = ;
- for (i = ; i <= mx; ++i) fg[i] = ;
- for (i = ; i <= n; ++i) {
- if (k & ( << n - i)) ++q1;
- else ++q2;
- for (j = ; j <= mx; ++j)
- if (n - tj[j] == q1 && tj[j] - tj[f[j]] < q2)
- fg[j] = ;
- }
- if (!fg[mx]) {
- puts("-1");
- return ;
- }
- int nw, nxt;
- q[++t] = mx;
- vst[mx] = ;
- while (h < t) {
- nw = q[++h];
- for (i = ; i <= n; ++i)
- if (nw & ( << i - )) {
- nxt = nw ^ ( << i - );
- if (!fg[nxt] || vst[nxt]) continue;
- vst[nxt] = ;
- lst[nxt] = nw;
- q[++t] = nxt;
- }
- }
- if (!vst[]) {
- puts("-1");
- return ;
- }
- for (nw = ; nw != mx; nw = lst[nw])
- st[++ft] = nw;
- st[++ft] = mx;
- for (i = ; i <= n; ++i) {
- if (k & ( << n - i)) {
- while (pt[ft - ]) --ft;
- nw = st[ft] ^ st[ft - ];
- --ft;
- for (j = ; j <= mx; ++j)
- if (!as[j] && (j & nw)) as[j] = ;
- }
- else {
- l = ;
- for (j = ; j <= m; ++j)
- if (!as[a[j]]) l |= a[j];
- for (j = ; j < ft; ++j)
- if (!pt[j] && ((st[j] ^ st[j + ]) & l) == ) {
- nw = st[j] ^ st[j + ];
- pt[j] = ;
- break;
- }
- for (j = ; j <= mx; ++j)
- if (!as[j] && (j & nw)) as[j] = ;
- }
- }
- for(i = ; i <= mx; ++i)
- pc(as[i] - + '');
- pc('\n');
- return ;
- }
18/9/16牛客网提高组Day2的更多相关文章
- 18/9/9牛客网提高组Day1
牛客网提高组Day1 T1 中位数 这好像是主席树??听说过,不会啊... 最后只打了个暴力,可能是n2logn? 只过了前30% qwq #include<algorithm> #in ...
- 牛客网 提高组第8周 T1 染色
染色 链接: https://ac.nowcoder.com/acm/contest/176/A 来源:牛客网 题目描述 \(\tt{fizzydavid}\)和\(\tt{leo}\)有\(n\)个 ...
- 牛客网 提高组第8周 T2 推箱子 解题报告
推箱子 链接: https://ac.nowcoder.com/acm/contest/176/B 来源:牛客网 题目描述 在平面上有\(n\)个箱子,每个箱子都可以看成一个矩形,两条边都和坐标轴平行 ...
- nowcoder(牛客网)提高组模拟赛第一场 解题报告
T1 中位数(二分) 这个题是一个二分(听说是上周atcoder beginner contest的D题???) 我们可以开一个数组b存a,sort然后二分b进行check(从后往前直接遍历check ...
- nowcoder(牛客网)提高组模拟赛第四场 解题报告
T1 动态点分治 就是模拟..... 但是没有过!! 看了题解之后发现.... 坑点:有可能 \(x<=r\),但是
- 牛客网提高组模拟赛第七场 T3 洞穴(附bitset介绍)
就是DP. 我们可以很简单的想到要枚举中间点,进行边数的转移. 但是因为边长数据范围很大,所以我们考虑log的倍增. 状态设计为\(dp[i][j][k]\),为从节点\(i\)走\(2^k\)步能否 ...
- 牛客网提高组模拟赛第七场 T2 随机生成树
其实看懂题就很水啦qwq,就是求\(1-N\)的约数啦. 暴力求的话时间复杂度是\(O(NlogN)\)的,其实正解是枚举每个数的倍数......这样的时间复杂度是\(\frac{N}{1}+\fra ...
- 牛客网提高组模拟赛第五场 T1同余方程(异或)(位运算)
区间不好做,但是我们可以转化成前缀来做.转化为前缀之后之后就是二维前缀和. 但是我还是不怎么会做.所以只能去看吉老师的题解 (确定写的那么简单真的是题解???). 我们要求模一个数余0,就等于找它的倍 ...
- 牛客网提高组第二场---solution
T1 方差 根据题目要求将式子先写出来注意下面式子中的 $n$ 全部都是 $n-1$$$\begin{aligned}ans&=n^2\times \frac{1}{n}\times \sum ...
随机推荐
- python in操作引发 TypeError
在看 networkx 源代码的时候认为疑惑.为什么外层 for 要注意 TypeError.里面就不用.相同是 in, 一直纠结 node 是不是有问题,比方 node 不能够被迭代什么的,那么里面 ...
- 关于android studio几种常见的错误解决
我也是从ec转到as的,没办法,大势所趋嘛,然而,在使用as的过程中遇到了非常多匪夷所思的错误,如今就说一下今天我遇到的这个错误. 美工妹子给了我一张图片,用来当做button的背景图,当然,这个图也 ...
- How to search Installed Updates
Windows本身的控制面板中自带的搜索,无法根据补丁编号进行搜索 可以将补丁信息导出到文本,再用文本编辑器进行查找 https://www.concurrency.com/blog/w/search ...
- .ds_store是什么文件
.ds_store是什么文件 .DS_Store是Mac OS保存文件夹的自定义属性的隐藏文件,如文件的图标位置或背景色,相当于Windows的desktop.ini. 1,禁止.DS_store生成 ...
- 远程带参数POST访问接口,返回数据
1. string token = GetRequest.GetString("token"); int customer_id = GetRequest.GetInt(" ...
- css3 实现动画效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ubunut PPA源概述
Ubuntu 自带的“软件”应用,可以安装海量软件,既包括发行者支持的软件.社区支持的软件,也包括专有驱动和版权软件.有时,我们需要的软件通过这些渠道仍然无法找到.这时,可以到 PPA 软件源中查找. ...
- CentOS7 PXE安装批量安装操作系统
1.安装相关软件 yum -y install tftp-server httpd dhcp syslinux 2.配置DHCP cp /usr/share/doc/dhcp-4.2.5/dhcpd. ...
- ssm框架的多表查询和增删查改
必须声明本文章==>http://www.cnblogs.com/zhu520/p/7883273.html 一: 1):我的运行环境 我使用myeclipse(你也可以使用eclipse),t ...
- 洛谷 P2690 接苹果
P2690 接苹果 题目背景 USACO 题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从 ...