传送门

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. Python项目搬迁,快捷导出环境依赖包到requirements.txt

    项目搬迁的时候,需要把当前的环境依赖包导出,然后到部署项目的服务器上安装依赖. 我们可以通过下面的命令执行,把依赖包导出到requirements.txt文件里. 生成requirements.txt ...

  2. 4.Ansible Task控制

    1.tag标签(调试) --skip-tags install_nfs 跳过此标签 -t 指定标签名 [root@manager tasks]# cat task_nfs.yml - hosts: w ...

  3. 基于Maven 的 Spring MVC

    Spring MVC 他是基于MVC的设计模式做出来的,他是Spring对Servlet的进一步的封装 MVC:Model  View  Controller 如何使用Spring MVC?(Spri ...

  4. Vue初始化过程

    用vue也有一两年了,始终对vue一知半解,不怎么了解内部的执行过程,最近在看vue源码,还是不少收获的,其中不乏浏览器事件轮询机制.闭包.设计模式等,还是非常值得一读.本篇简要记录下vue的初始化过 ...

  5. Kingfisher

    Kingfisher ima1.kf.setImage(with: url) 为了兼容macOS, IOS 通过协议定义了kf,实际上就是本身 KingfisherOptionsInfo 可以配置一些 ...

  6. stream根据条件过滤List<Object>

    List<String> filterUser= new ArrayList<>(); filterUser.add("张三"); List<User ...

  7. SQL Server事务复制(sql 2008 r2)

    一.环境准备 1.两个虚拟服务器 主机1:XINXIBU01  作为发布和分发服务器   主 机2:XINXIBU02 192.168.1.160  作业阅服务器 2.SQL SERVER sql 2 ...

  8. 【转】开发一个这样的 APP 要多长时间?

    作者:蒋国刚 www.cnblogs.com/guogangj/p/4676836.html 呵呵. 这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着 ...

  9. String对象及正则表达式

    1,String和string还是有区别的,一个就是用双引号或单引号包括起来的数据就是字符串,另一个本质是数组多个字符串组成的只读字符数组: 2,你说string他是数组吧,他和数组还是有点区别的,他 ...

  10. tensorflow-- Dataset创建数据集对象

    tf.data模块包含: experimental 模块 Dataset 类  FixedLengthRecordDataset 类 TFRecordDataset 类 TextLineDataset ...