Codeforces Round #519
题目链接:传送门
A. Elections (思维+暴力)
思路:
从最小的k开始枚举就好了- -。
#include <bits/stdc++.h> using namespace std;
const int MAX_N = + ; int a[MAX_N]; int main()
{
int N;
cin >> N;
int m = -, sum = ;
for (int i = ; i <= N; i++) {
scanf("%d", a+i);
m = max(m, a[i]);
sum += a[i];
}
int ans = m;
int Awruk = N*m-sum;
while (Awruk <= sum) {
Awruk += N;
ans++;
}
cout << ans << endl;
return ;
}
B. Lost Array (暴力枚举)
思路:
把差分预处理出来,枚举长度,可以跑到最后的就加入答案。
#include <bits/stdc++.h> using namespace std;
const int MAX_N = 1e3 + ; int a[MAX_N];
int b[MAX_N];
int ans[MAX_N]; int main()
{
int N;
cin >> N;
a[] = ;
for (int i = ; i <= N; i++) {
scanf("%d", a+i);
b[i] = a[i] - a[i-];
}
int cnt = ;
for (int i = ; i <= N; i++) {
int j;
for (j = ; j <= N; j++) {
if (b[j] != b[(j-)%i+])
break;
}
if (j == N+)
ans[cnt++] = i;
}
cout << cnt << endl << ans[];
for (int i = ; i < cnt; i++)
cout << ' ' << ans[i];
cout << endl;
return ;
}
C. Smallest Word (思维)
思路:
遇到连续的a就找到最后一个,翻转。
遇到连续的b也找到最后一个,如果下一个是a,翻转。
#include <bits/stdc++.h> using namespace std;
const int MAX_N = 1e3 + ; bool ans[MAX_N]; int main()
{
string s;
memset(ans, false, sizeof ans);
cin >> s;
int len = s.size();
int i = ;
while (i < len) {
while (i < len && s[i] == 'a')
i++;
ans[i-] = true;
while (i < len && s[i] == 'b')
i++;
if (i < len && s[i] == 'a')
ans[i-] = true;
}
cout << ans[];
for (int i = ; i < len; i++)
cout << ' ' << ans[i];
cout << endl;
return ;
}
D. Mysterious Crime (尺取+暴力)
思路:
从第一行开始,往下面几行找最长的匹配。每行从上一行的最左端的数开始往右匹配,匹配到不同的就break,进入下一行。
预处理一个pos[i][j],表示第i行的值为j的数在哪个位置。
找完最后一行后得到最长的匹配的长度为len,计算len对答案产生的贡献。
注意M为1的时候不能匹配要特判一下,答案就是N*(N+1)/2。
#include <bits/stdc++.h> using namespace std;
const int MAX_N = 1e5 + ;
const int MAX_M = + ; int N, M;
int a[MAX_M][MAX_N];
int pos[MAX_M][MAX_N];
bool vis[MAX_N]; int main()
{
cin >> N >> M;
memset(vis, false, sizeof vis);
for (int i = ; i <= M; i++)
for (int j = ; j <= N; j++) {
scanf("%d", &a[i][j]);
pos[i][a[i][j]] = j;
}
long long ans = ;
for (int i = ; i <= N; i++) if (!vis[a[][i]]){
int l = i, r = N;
long long len = ;
for (int j = ; j <= M; j++) {
int nxtl, nxtr;
nxtl = nxtr = pos[j][a[j-][l]];
while (l <= r && nxtr <= N && a[j][nxtr] == a[j-][l]) {
l++;
nxtr++;
}
l = nxtl, r = nxtr-;
if (j == M) {
len = r-l+;
for (int k = l; k <= r; k++)
vis[a[M][k]] = true;
}
}
ans += (len+)*len/;
}
if (M == )
ans = (long long)(N+)*N/;
cout << ans << endl;
return ;
}
E. Train Hard, Win Easy(思维+排序)
思路:
两个人(u,v)组队时他们的得分是min(xu+yv,xv+yu),考虑到:
xu+yv < xv+yu
=>yv-xv < yu-xu
令di = yi-xi,则组队时d小的一方出y,d大的一方出x。
根据d排序后,对于(1 ≤ i < j ≤ N)有di < dj,所以第i个人和第j个人组队的得分为yi+xj。
预处理x和y的前缀和,对于第i个人,他与在他之前的人组队的得分为:
$\sum_{j=1}^{i-1}y_{j}$ + xi * (i-1);
与在他之后的人组队的得分为:
$\sum_{j=i+1}^{N}x_{j}$ + yi * (N-i);
这样可以O(1)算出每个人的得分,然后再减去不能和自己比赛的人的贡献就好了。
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
const int MAX_N = 3e5 + ; struct Node{
int ind;
ll x, y;
ll d;
Node(int _i = , ll _x = , ll _y = ) : ind(_i), x(_x), y(_y) {d = y-x;}
bool operator < (const Node& a) const {
return d < a.d;
}
}nodes[MAX_N]; ll sumx[MAX_N], sumy[MAX_N];
ll ans[MAX_N]; int main()
{
int N, M;
cin >> N >> M;
memset(ans, , sizeof ans);
for (int i = ; i <= N; i++) {
ll x, y;
scanf("%I64d%I64d", &x, &y);
nodes[i] = Node(i, x, y);
}
for (int i = ; i <= M; i++) {
int u, v;
scanf("%d%d", &u, &v);
ll tmp = ;
if (nodes[u] < nodes[v])
tmp = nodes[u].y + nodes[v].x;
else
tmp = nodes[v].y + nodes[u].x;
ans[u] -= tmp;
ans[v] -= tmp;
}
sort(nodes+, nodes++N);
sumx[] = sumy[] = ;
for (int i = ; i <= N; i++) {
sumx[i] = sumx[i-] + nodes[i].x;
sumy[i] = sumy[i-] + nodes[i].y;
}
for (int i = ; i <= N; i++) {
Node cur = nodes[i];
ans[cur.ind] += sumy[i-] + cur.x*(i-);
ans[cur.ind] += sumx[N] - sumx[i] + cur.y*(N-i);
}
bool firstprint = true;
for (int i = ; i <= N; i++) {
if (firstprint)
firstprint = false;
else
printf(" ");
printf("%I64d", ans[i]);
}
puts("");
return ;
}
F. Make It One(组合数学+数论+dp)
思路:
说实话这题比较玄学。
2 * 3 * 5 * 7 * 11 * 13 * 17 ≈ 5e5,所以3e5范围内一个数最多有6个质因数。所以答案是不会超过6的,考虑从1开始枚举答案。
状态:
f[i][j]表示选择的数字数量为i时,最大公约数为j的方案数,若f[i][1] > 0,就说明符合题意,直接输出。
已知所有的ai中j的倍数有cnt[j]个,则由容斥原理知:
状态转移方程:
f[i][j] = $C_{cnt[j]}^{i}-\sum_{k=2}^{\infty}f[i][k*j]$
组合数预处理一个逆元可以O(1)求出,总时间复杂度为O(loga * (N + a))
#include <bits/stdc++.h> using namespace std;
const int MAX_N = 3e5 + ;
const int MOD = 1e9 + ; int fpow(int a, int p) {
int ans = ;
while (p) {
if (p&) ans = (1LL * ans * a) % MOD;
p >>= ;
a = (1LL * a * a) % MOD;
}
return ans;
} int N;
int a[MAX_N];
int f[][MAX_N], cnt[MAX_N];
int fac[MAX_N], inv[MAX_N]; int newton(int m, int n) {
if (n < || m < n) return ;
return ((1LL * fac[m] * inv[n])%MOD * inv[m-n]) % MOD;
} void init() {
fac[] = ;
for (int i = ; i <= MAX_N; i++)
fac[i] = (1LL * fac[i-] * i) % MOD; inv[MAX_N-] = fpow(fac[MAX_N-], MOD-);
for (int i = MAX_N-; i >= ; i--)
inv[i-] = (1LL * inv[i] * i) % MOD;
} void sub(int& a, int b) {
a -= b;
if (a < )
a += MOD;
} int main()
{
init();
scanf("%d", &N);
memset(cnt, , sizeof cnt);
for (int i = ; i <= N; i++) {
scanf("%d", a+i);
cnt[a[i]]++;
}
for (int i = ; i < MAX_N; i++) {
for (int j = *i; j < MAX_N; j += i) {
cnt[i] += cnt[j];
}
}
for (int i = ; i < ; i++) {
for (int j = MAX_N-; j >= ; j--) {
f[i][j] = newton(cnt[j], i);
for (int k = j+j; k < MAX_N; k += j)
sub(f[i][j], f[i][k]);
}
if (f[i][] > ) {
printf("%d\n", i);
return ;
}
}
puts("-1");
return ;
}
Codeforces Round #519的更多相关文章
- Codeforces Round #519 by Botan Investments
Codeforces Round #519 by Botan Investments #include<bits/stdc++.h> #include<iostream> #i ...
- Codeforces Round #519 by Botan Investments(前五题题解)
开个新号打打codeforces(以前那号玩废了),结果就遇到了这么难一套.touristD题用了map,被卡掉了(其实是对cf的评测机过分自信),G题没过, 700多行代码,码力惊人.关键是这次to ...
- Codeforces Round #519 by Botan Investments F. Make It One
https://codeforces.com/contest/1043/problem/F 题意 给你n个数,求一个最小集合,这个集合里面数的最大公因数等于1 1<=n<=3e5 1< ...
- Codeforces Round #519 题解
A. Elections 题意概述 给出 \(a_1, \ldots, a_n\),求最小的 \(k (k \ge \max a_i)\), 使得 \(\sum_{i=1}^n a_i < \s ...
- Codeforces Round #519 by Botan Investments翻车记
A:枚举答案即可.注意答案最大可达201,因为这个wa了一发瞬间爆炸. #include<iostream> #include<cstdio> #include<cmat ...
- 【Codeforces Round #519】
A:https://www.cnblogs.com/myx12345/p/9872082.html B:https://www.cnblogs.com/myx12345/p/9872124.html ...
- 【Codeforces Round #519 by Botan Investments E】Train Hard, Win Easy
[链接] 我是链接,点我呀:) [题意] [题解] 设每个人做第一题.第二题的分数分别为x,y 我们先假设没有仇视关系. 即每两个人都能进行一次训练. 那么 对于第i个人. 考虑第j个人对它的贡献 如 ...
- 【Codeforces Round #519 by Botan Investments A】 Elections
[链接] 我是链接,点我呀:) [题意] [题解] 枚举k 那么另外一个人的得票就是nk-sum(ai) 找到最小的满足nk-sum(ai)>sum(ai)的k就ok了 [代码] #includ ...
- 【 Codeforces Round #519 by Botan Investments B】Lost Array
[链接] 我是链接,点我呀:) [题意] [题解] 枚举k 不难根据a得到x[0..k-1] 然后再根据a[k+1..n]来验证一下得到的x是否正确就好. [代码] #include <bits ...
随机推荐
- nyoj-0469-擅长排列的小明 II(找规律)
nyoj-0469-擅长排列的小明 II 思路:递推分析:为了简便起见,我们用Ai代表第i个数字 , 由于A1一直是1,所以A2只能是2或3.假设dp[n]表示1->n这个序列的方案数 ...
- laravel的工厂模式数据填充:
数据表post中的字段结构. database\factory\UserFactory.php $factory->define(App\Post::class,function (Faker ...
- 【原创】QT简单计算器
代码 //main.cpp #include "calculator_111.h" #include <QtWidgets/QApplication> int main ...
- 深入理解java虚拟机---虚拟机工具jinfo(十五)
作用: 实时查看和调整虚拟机参数. jinfo 是jdk自带的一个工具,它可以用来查看正在运行的java应用程序的扩展参数(JVM中-X标示的参数):甚至支持在运行时修改部分参数. 1.通过以下的命令 ...
- 7系列FPGA远程更新方案-QuickBoot(转)
reference: http://xilinx.eetrend.com/d6-xilinx/article/2014-04/7009.html reference : quickboot meth ...
- 一次练习 发现的问题,malloc free 无效;findfirstfile失败,writefile失败的原因
#include <windows.h> #include <stdio.h> #include <shlwapi.h> #pragma comment(lib,& ...
- adb shell按键操作(input keyevent)
前言:input keyeven操作发送手机上常用的一些按键操作 一.keyevent事件对应数字 电话键 KEYCODE_CALL: 拨号键 KEYCODE_ENDCALL: 挂机键 KEYCODE ...
- L313 珊瑚裸鼠灭绝
This week the Australian government declared the extinction of a tiny rodent called Bramble Cay melo ...
- Android修行之路------ListView自定义布局
主布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...
- python基础题型一
一.执行Python脚本的两种方式 #python name.py Python解析器 #chmod +x name.py 二.简述位.字节的关系 1Byte=8bit 三.简述ascii.uni ...