传送门

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. Dubbo学习系列之十四(Seata分布式事务方案AT模式)

    一直说写有关最新技术的文章,但前面似乎都有点偏了,只能说算主流技术,今天这个主题,我觉得应该名副其实.分布式微服务的深水区并不是单个微服务的设计,而是服务间的数据一致性问题!解决了这个问题,才算是把分 ...

  2. 对python函数后面有多个括号的理解?

    一般而言,函数后面只有一个括号.如果看见括号后还有一个括号,说明第一个函数返回了一个函数,如果后面还有括号,说明前面那个也返回了一个函数.以此类推. 比如fun()() def fun(): prin ...

  3. Django Forms ChoiceField 选项更新问题

    今天使用django的forms,发现单选后台新增数据后,前端表单选项不能刷新 class UserForm(forms.Form): name = forms.ChoiceField( choice ...

  4. Angular框架入门

    今天简单学习了AngularJS框架,在这里我想简单的总结一下我所了解的AngularJS和一些入门案例! 首先,我们要知道什么是AngularJS? AngularJS  诞生于2009年,由Mis ...

  5. 升级python2.7至python3.7

    最近在centos7下执行命令时,出现以下提示: DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020 ...

  6. 列表list常用的方法

    列表 name = ['Lisa', 'Mike', 'Jone', 'Lucy'] name.sort() # 排序,根据对应的ASCII码值,可以排各种类型的字符 name.append('Ms ...

  7. Centos手动安装PHP

    下载PHP的源码,我下的是7.2版本,看了一下安装的参数太多了,也没有时间依次了解每个参数的意思,直接从网上复制了一个,先尝试安装起来.并记录一下步骤,基本的步骤就是解压.配置.编译.运行.1.下载P ...

  8. 如何计算Data Guard环境中Redo所需的网络带宽传输 (Doc ID 736755.1)

    How To Calculate The Required Network Bandwidth Transfer Of Redo In Data Guard Environments (Doc ID ...

  9. 11.2 Data Guard Physical Standby Switchover Best Practices using SQL*Plus (Doc ID 1304939.1)

    11.2 Data Guard Physical Standby Switchover Best Practices using SQL*Plus (Doc ID 1304939.1) APPLIES ...

  10. 017.Kubernetes二进制部署检查集群

    一 验证集群功能 1.1 检查节点状态 [root@k8smaster01 ~]# kubectl get nodes 1.2 创建测试文件 [root@k8smaster01 ~]# cd /opt ...