题目链接:传送门

 

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的更多相关文章

  1. Codeforces Round #519 by Botan Investments

    Codeforces Round #519 by Botan Investments #include<bits/stdc++.h> #include<iostream> #i ...

  2. Codeforces Round #519 by Botan Investments(前五题题解)

    开个新号打打codeforces(以前那号玩废了),结果就遇到了这么难一套.touristD题用了map,被卡掉了(其实是对cf的评测机过分自信),G题没过, 700多行代码,码力惊人.关键是这次to ...

  3. Codeforces Round #519 by Botan Investments F. Make It One

    https://codeforces.com/contest/1043/problem/F 题意 给你n个数,求一个最小集合,这个集合里面数的最大公因数等于1 1<=n<=3e5 1< ...

  4. Codeforces Round #519 题解

    A. Elections 题意概述 给出 \(a_1, \ldots, a_n\),求最小的 \(k (k \ge \max a_i)\), 使得 \(\sum_{i=1}^n a_i < \s ...

  5. Codeforces Round #519 by Botan Investments翻车记

    A:枚举答案即可.注意答案最大可达201,因为这个wa了一发瞬间爆炸. #include<iostream> #include<cstdio> #include<cmat ...

  6. 【Codeforces Round #519】

    A:https://www.cnblogs.com/myx12345/p/9872082.html B:https://www.cnblogs.com/myx12345/p/9872124.html ...

  7. 【Codeforces Round #519 by Botan Investments E】Train Hard, Win Easy

    [链接] 我是链接,点我呀:) [题意] [题解] 设每个人做第一题.第二题的分数分别为x,y 我们先假设没有仇视关系. 即每两个人都能进行一次训练. 那么 对于第i个人. 考虑第j个人对它的贡献 如 ...

  8. 【Codeforces Round #519 by Botan Investments A】 Elections

    [链接] 我是链接,点我呀:) [题意] [题解] 枚举k 那么另外一个人的得票就是nk-sum(ai) 找到最小的满足nk-sum(ai)>sum(ai)的k就ok了 [代码] #includ ...

  9. 【 Codeforces Round #519 by Botan Investments B】Lost Array

    [链接] 我是链接,点我呀:) [题意] [题解] 枚举k 不难根据a得到x[0..k-1] 然后再根据a[k+1..n]来验证一下得到的x是否正确就好. [代码] #include <bits ...

随机推荐

  1. java生成word的几种方案

    http://blog.sina.com.cn/s/blog_a5e968370101crtl.html 1. Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建 ...

  2. vue中alert toast confirm loading 公用

    import Vue from 'vue' import { ToastPlugin, AlertPlugin, ConfirmPlugin, LoadingPlugin } from 'vux' / ...

  3. chrome hosts

    # Go Hosts# 2017-05-02 02:13:04# Localhost (DO NOT REMOVE)127.0.0.1    localhost::1    localhost ip6 ...

  4. django自定义标签,int转化为str类型

    1.在app中创建templatetags目录,目录名必须为templatetags 2.在目录templatetags中创建一个.py文件,例如 strFilter.py strFilter.py ...

  5. Spring注入,Ioc的具体配置

    Spring框架的IOC注入: 一.Java部分代码: Person实体类: package com.ioc; import java.util.List; import java.util.Map; ...

  6. 【资料收集】Converting Between cv::Mat and QImage or QPixmap

    参考: 方法一 Convert between cv::Mat and QImage 两种图片类转换 - Grandyang - 博客园 http://www.cnblogs.com/grandyan ...

  7. 第一篇 入门必备 (Android学习笔记)

    第一篇 入门必备 第1章 初识Android 第2章 搭建你的开发环境 第3章 创建第一个程序--HelloWorld 第4章 使用Android工具   ●Android之父 Android安迪·罗 ...

  8. Linux U盘安装

    Ubuntu 15 U盘安装: 用UltraISO把iso文件写入到U盘中,选择hdd+模式. u盘启动后提示not a com32r image,先按tab键,然后输入live进入试用模式,然后再点 ...

  9. final文案+美工

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2476] 文案+美工: 剧情设计+题目设计+美工: 第21关: 剧情: 计算机学 ...

  10. Strict Standards: Declaration of UserModel::toJSON() should be compatible with that of BaseModel::toJSON()

    使用php报了这个错误: 错误的意思是:  严格标准: usermodel中的 toJSON() 方法 应该 同 BaseModel中的toJson() 方法是兼容的. php要求 子类的方法如果同父 ...