传送门

A. Changing Volume

签到。

Code
/*
* Author: heyuhhh
* Created Time: 2019/11/19 22:37:33
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5; int a, b; void run(){
cin >> a >> b;
int d = abs(a - b);
int ans = 0;
if(d >= 5) {
ans += d / 5;
d -= ans * 5;
}
if(d == 3 || d == 4) ans += 2;
if(d == 2 || d == 1) ans += 1;
cout << ans << '\n';
} int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T;
while(T--) run();
return 0;
}

B. Fridge Lockers

形成环后贪心即可。

Code
/*
* Author: heyuhhh
* Created Time: 2019/11/19 22:48:14
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1005; int n, m;
int a[N]; void run(){
cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i];
if(n > m || n == 2) {
cout << -1 << '\n';
return;
}
int ans = 0;
for(int i = 1; i <= n; i++) ans += a[i] + a[i];
m -= n;
int mn1 = INF, mn2 = INF;
int p1, p2;
for(int i = 1; i <= n; i++) {
if(a[i] < mn1) {
p2 = p1;
p1 = i;
mn2 = mn1;
mn1 = a[i];
} else if(a[i] < mn2){
mn2 = a[i];
p2 = i;
}
}
ans += m * (mn1 + mn2);
cout << ans << '\n';
for(int i = 1; i <= m; i++) {
cout << p1 << ' ' << p2 << '\n';
}
for(int i = 2; i <= n; i++) cout << i << ' ' << i - 1 << '\n';
cout << 1 << ' ' << n << '\n';
} int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T;
while(T--) run();
return 0;
}

C. League of Leesins

确定两个数后第三个也就确定了。

所以\(map\)乱搞一下。

Code
/*
* Author: heyuhhh
* Created Time: 2019/11/19 23:14:24
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
//#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2e5 + 5; int n;
int q[N][3];
map <pii, vector<int>> mp;
map <int, pii> mp2;
int cnt[N];
int b[N];
int s, e;
bool vis[N]; bool go(int pos, int last1, int last2) {
dbg(pos, last1, last2);
vis[last2] = 1;
b[pos] = last2;
if(pos == n) {
if(last2 != e) return false;
return true;
}
if(last1 > last2) swap(last1, last2);
vector<int> nxt = mp[MP(last1, last2)];
bool f = true, t = false;
for(auto it : nxt) {
dbg(pos, it);
if(!vis[it]) {
f = go(pos + 1, b[pos], it);
t = true;
}
}
dbg(f, t);
return (f && t);
} void run(){
for(int i = 1; i < n - 1; i++) {
cin >> q[i][0] >> q[i][1] >> q[i][2];
sort(q[i], q[i] + 3);
mp[MP(q[i][0], q[i][1])].push_back(q[i][2]);
mp[MP(q[i][1], q[i][2])].push_back(q[i][0]);
mp[MP(q[i][0], q[i][2])].push_back(q[i][1]);
mp2[q[i][0]] = MP(q[i][1], q[i][2]);
mp2[q[i][1]] = MP(q[i][0], q[i][2]);
mp2[q[i][2]] = MP(q[i][0], q[i][1]);
for(int j = 0; j < 3; j++) {
++cnt[q[i][j]];
}
}
s = -1, e;
for(int i = 1; i <= n; i++) {
if(cnt[i] == 1) {
if(s == -1) s = i;
else e = i;
}
}
dbg(e);
b[1] = s;
vis[s] = 1;
pii p = mp2[s];
int nxt1 = p.fi, nxt2 = p.se;
dbg(nxt1, nxt2);
if(go(2, s, nxt1)) {
for(int i = 1; i <= n; i++) cout << b[i] << " \n"[i == n];
} else {
memset(vis, 0, sizeof(vis));
vis[s] = 1;
go(2, s, nxt2);
for(int i = 1; i <= n; i++) cout << b[i] << " \n"[i == n];
}
} int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n) run();
return 0;
}

D. Feeding Chicken

模拟题。蛇形填数即可。注意一下细节,比方说当前要找\(cnt\)个,找完\(cnt\)个过后,不要及时修改答案变量,等到找到\(cnt+1\)个过后再修改。

Code
/*
* Author: heyuhhh
* Created Time: 2019/11/20 0:05:52
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
//#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 105; int n, m, k;
char s[N][N];
char ans[N][N];
int Max, Min;
int need, num; char Get(int x) {
if(x < 26) return x + 'a';
x -= 26;
if(x < 26) return x + 'A';
x -= 26;
return x + '0';
} void go(int x, int y, int cnt, int d) {
if(y == m + 1) {
++x; y = m; d = -1;
}
if(y == 0) {
++x; y = 1; d = 1;
}
if(x > n) return;
if(s[x][y] == 'R') --cnt;
if(cnt == -1) ++num;
ans[x][y] = Get(num);
if(cnt == -1) {
if(--need >= 0) {
go(x, y + d, Max - 1, d);
} else {
go(x, y + d, Min - 1, d);
}
return;
}
go(x, y + d, cnt, d);
} void run(){
cin >> n >> m >> k;
int tot = 0;
for(int i = 1; i <= n; i++) {
cin >> (s[i] + 1);
for(int j = 1; j <= m; j++) {
if(s[i][j] == 'R') ++tot;
}
}
Min = tot / k, Max = tot / k + 1;
need = tot - k * Min;
dbg(Min, need);
num = 0;
if(--need >= 0) go(1, 1, Max, 1);
else go(1, 1, Min, 1);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cout << ans[i][j];
}
cout << '\n';
}
} int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T;
while(T--) run();
return 0;
}

E1. Send Boxes to Alice (Easy Version)

题意:

给出\(n\)个位置,每个位置上面有数\(a_i,0\leq a_i\leq 1\)。

现在执行一次操作为:将\(i\)位置上面的一个数放到\(i-1\)位置或者\(i+1\)位置(如果位置存在的话)。

问最少需要操作多少次,使得最终每个位置上面的数能够被\(k\)整除。

思路:

  • 求出\(cnt=\sum a_i\),显然根据\(cnt\)所有的质因子来分组求解即可。
  • 因为如果找的两个数\(a,b\)满足\(a=kb\),那么按\(b\)分组不会比按\(a\)分组差。
  • 然后显然每组内要合为一堆,那么肯定是往中间的数移动。
  • 每组根据中位数算贡献即可。

代码如下:

Code
/*
* Author: heyuhhh
* Created Time: 2019/11/20 11:56:43
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
//#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5; int n;
int a[N]; ll gao(int d) {
ll res = 0;
int cur;
for(int i = 1, j; i <= n; i = j) {
j = i + 1;
if(a[i] == 1) {
cur = 1;
res -= i;
while(j <= n && cur < d) {
if(a[j] == 1) {
++cur;
if(cur < (d + 1) / 2) res -= j;
else if(cur > (d + 1) / 2) res += j;
}
++j;
}
}
}
return res;
} void run(){
int cnt = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
cnt += a[i];
}
if(cnt == 1) {
cout << -1;
return;
}
ll ans = 1e18;
for(int i = 1; i * i <= cnt; i++) {
if(cnt % i == 0) {
if(i > 1) ans = min(ans, gao(i));
if(cnt / i > 1) ans = min(ans, gao(cnt / i));
}
}
cout << ans << '\n';
} int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n) run();
return 0;
}

E2. Send Boxes to Alice (Hard Version)

题意:

同E1,只是\(a_i\)的数据范围变为了\(0\leq a_i\leq 10^6\)。

思路:

  • 还是要找最小质因子,但是之后统计答案的时候可能不会合成一堆。
  • 对于一个数\(a\),假设当前的最小质因子为\(d\),根据贪心的思想,这个位置要满足能被\(d\)整除,肯定它往后面搬运\(d\% k\)个石子或者后面往它搬运\(k-d\% k\)个石子。
  • 当当前位置满足过后,因为只会对下一个位置产生影响,所以下一个位置的值算上其影响,之后又是一个新的子问题。
  • 所以最终答案即为\(\sum_{i=1}^n min(pre[i], d-pre[i])\)。

虽然代码不长,但思路感觉还是挺巧妙的。。子问题的转化这里感觉不好直接想到。

Code
/*
* Author: heyuhhh
* Created Time: 2019/11/20 11:56:43
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
//#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e6 + 5; int n;
int a[N]; ll gao(ll d) {
ll res = 0, pre = 0;
for(int i = 1; i <= n; i++) {
pre = (pre + a[i]) % d;
res += min(pre, d - pre);
}
return res;
} void run(){
ll cnt = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i]; cnt += a[i];
}
if(cnt == 1) {
cout << -1;
return;
}
ll ans = 1e18;
for(int i = 2; 1ll * i * i <= cnt; i++) {
if(cnt % i == 0) {
ans = min(ans, gao(i));
while(cnt % i == 0) cnt /= i;
}
}
if(cnt > 1) ans = min(ans, gao(cnt));
cout << ans << '\n';
} int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n) run();
return 0;
}

Codeforces Round #601 (Div. 2)的更多相关文章

  1. 【cf比赛记录】Codeforces Round #601 (Div. 2)

    Codeforces Round #601 (Div. 2) ---- 比赛传送门 周二晚因为身体不适鸽了,补题补题 A // http://codeforces.com/contest/1255/p ...

  2. Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version)

    Codeforces Round #601 (Div. 2) E2. Send Boxes to Alice (Hard Version) N个盒子,每个盒子有a[i]块巧克力,每次操作可以将盒子中的 ...

  3. Codeforces Round #601 (Div. 2) E1 Send Boxes to Alice (Easy Version)

    #include <bits/stdc++.h> using namespace std; typedef long long ll; ; int a[N]; int n; bool pr ...

  4. Codeforces Round #601 (Div. 2) D Feeding Chicken

    //为了连贯,采取一条路形式,从第一行开始 也就是s型 #include <bits/stdc++.h> using namespace std; ; char str[MAXN][MAX ...

  5. Codeforces Round #601 (Div. 2) C League of Leesins

    把每一次输入的一组数字存下来,然后把每个数字出现的组数存下来 然后找只出现过一次的数字a,那么这个数字a不是开头就是结尾,默认为开头(是哪个都无所谓),然后去找和它出现在同一组的两个数字b和c,而b和 ...

  6. Codeforces Round #601 (Div. 2) B Fridge Lockers

    //题目要求的是每一个点最少要有两条边连接,所以可以先构成一个环.然后再把剩余的最短的边连接起来 #include<iostream> #include<algorithm> ...

  7. Codeforces Round #601 (Div. 2) A Changing Volume

    好吧,其实我拿到这个题的时候,首先想到了bfs,写完之后,开开森森的去交代码,却在第二个数据就TEL,然后优化半天,还是不行. 最终,我盯着1,2,5发呆半天,wc,然后直接贪心 #include&l ...

  8. Codeforces Round #601 (Div. 2)E(寻找质因子,DP)

    先分解质因数,对于当前a[i],假设当前的质因数为x,这个位置要满足能被k整除,有两个可能,要么是它向后一个转移x%k个,要么是后一个向它转移k-x%k个. 对于每一个a[i]满足后,因为只会对下一个 ...

  9. Codeforces Round #601 (Div. 2)D(蛇形模拟)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; vector<char>an ...

随机推荐

  1. 30分钟连接树莓派到微软云 Azure IoT Hub,并将数据进行可视化

    更多内容,关注公众号: 树莓派是很多动手达人必备的小玩具,本节内容,让我们拿出树莓派,在30分钟内,将树莓派连接到微软云Azure的IoT Hub,然后将温湿度曲线可视化.(本节内容完整视频在文章末尾 ...

  2. 使用vue脚手架快速创建vue项目(入门)

    1.安装环境 为了方便,以下操作大多数中命令行中运行,window可以用cmd,powershell,gitbash等. 安装node.js 打开它的官网,或者中文网站,然后直接下载就可以了,然后跟安 ...

  3. 一分钟教你编写Linux全局内置命令

    前言:在linux命令使用中,有些命令总是又长又难记,就算是经常使用的命令每次都敲也真的很烦,所以今天教大家一个方法,来简化命令,创建我们自己的内建命令!!! 创建内置命令 创建命令存储目录 现在li ...

  4. JavaWeb 错误/异常时页面提示

    经常我们会遇到发生页面404错误,服务器 500 异常,如果默认方式处理,则是将异常捕获之后跳到 Tomcat 缺省的异常页面,如下图所示.

  5. 【iOS】Swipe与Pan区别分析

    By definition, a swipe gesture is necessarily also a pan gesture -- both involve translational movem ...

  6. 团队项目之Scrum6

    小组:BLACK PANDA 时间:2019.11.26 每天举行站立式会议 提供当天站立式会议照片一张 2 昨天已完成的工作 2 编辑功能优化 实现主页内容展示 今天计划完成的工作 2 内容展示 根 ...

  7. ActiveMQ下载与安装(消息中间件JMS)

    下载 官方网站下载:http://activemq.apache.org/ 1.3.2安装(Linux) (1)将apache-activemq-5.12.0-bin.tar.gz 上传至服务器 (2 ...

  8. rpm包安装java jar开机自启

    1.安装jdk: rpm -ivh jdk-8u201-linux-x64.rpm 2.配置jdk路径 打开/etc/profile增加以下内容: export JAVA_HOME=/usr/java ...

  9. 学习java需要英语很好吗?

    学习java需要英语很好吗? 编程语言起源于美国,是由英文构成的,其中包括几十个英文的关键字以及几百个英文的函数,除非需要对文本进行处理,否则一般不会出现中文.但是,它们都是孤立的单词,不构成任何语句 ...

  10. ubuntu vscode 写一个C++程序

    博客转载:https://blog.csdn.net/weixin_43374723/article/details/84064644 Visual studio code是微软发布的一个运行于 Ma ...