Codeforces Round #601 (Div. 2)
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)的更多相关文章
- 【cf比赛记录】Codeforces Round #601 (Div. 2)
Codeforces Round #601 (Div. 2) ---- 比赛传送门 周二晚因为身体不适鸽了,补题补题 A // http://codeforces.com/contest/1255/p ...
- 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]块巧克力,每次操作可以将盒子中的 ...
- 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 ...
- Codeforces Round #601 (Div. 2) D Feeding Chicken
//为了连贯,采取一条路形式,从第一行开始 也就是s型 #include <bits/stdc++.h> using namespace std; ; char str[MAXN][MAX ...
- Codeforces Round #601 (Div. 2) C League of Leesins
把每一次输入的一组数字存下来,然后把每个数字出现的组数存下来 然后找只出现过一次的数字a,那么这个数字a不是开头就是结尾,默认为开头(是哪个都无所谓),然后去找和它出现在同一组的两个数字b和c,而b和 ...
- Codeforces Round #601 (Div. 2) B Fridge Lockers
//题目要求的是每一个点最少要有两条边连接,所以可以先构成一个环.然后再把剩余的最短的边连接起来 #include<iostream> #include<algorithm> ...
- Codeforces Round #601 (Div. 2) A Changing Volume
好吧,其实我拿到这个题的时候,首先想到了bfs,写完之后,开开森森的去交代码,却在第二个数据就TEL,然后优化半天,还是不行. 最终,我盯着1,2,5发呆半天,wc,然后直接贪心 #include&l ...
- Codeforces Round #601 (Div. 2)E(寻找质因子,DP)
先分解质因数,对于当前a[i],假设当前的质因数为x,这个位置要满足能被k整除,有两个可能,要么是它向后一个转移x%k个,要么是后一个向它转移k-x%k个. 对于每一个a[i]满足后,因为只会对下一个 ...
- Codeforces Round #601 (Div. 2)D(蛇形模拟)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; vector<char>an ...
随机推荐
- .NET Core 3.0 Preview 6中对ASP.NET Core和Blazor的更新
我们都知道在6月12日的时候微软发布了.NET Core 3.0的第6个预览版.针对.NET Core 3.0的发布我们国内的微软MVP-汪宇杰还发布的官翻版的博文进行了详细的介绍.具体的可以关注&q ...
- Java设计模式 - - 单例模式 装饰者模式
Java设计模式 单例模式 装饰者模式 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 静态代理模式:https://www.cnblogs.com/StanleyBlogs/p/1 ...
- mysql字符集与比较规则
MySQL有4个级别的字符集和比较规则,分别是: 服务器级别 数据库级别 表级别 列级别 查看服务器级别命令: mysql> SHOW VARIABLES LIKE 'character_set ...
- Flask 模板语言,装饰器
Jinja2模板语言 # -*- coding: utf-8 -*- from flask import Flask, render_template, request, redirect, ...
- 控制台提示“Invalid string length”的原因
控制台提示“Invalid string length”,浏览器直接卡掉,是为什么呢? 答:因为在写嵌套循环时,定义的变量重名了,内层和外层用了同一个i变量. -THE END-
- 微信wxid搜索用户不存在,怎么加好友?
点击上方↑↑↑蓝字[协议分析与还原]关注我们 " 根据wxid微信号找到用户的方法及周边资料大全." 最近经常会收到好友请求的消息提醒,但却不是通过搜索好友和群聊来添加的,因此研究 ...
- Pinpoint-agent监控springboot编译的jar启动方式
由于springboot在打包发版时已经将tomcat容器内嵌到jar文件中,可以通过以下命令来使pinpoint-agent监控生成的jar服务 java -javaagent:D:\Softwar ...
- 用PYTHON首选的GUI库WXPYTHON做程序界面
大家好,我是A8U神经网络,今天又要跟大家分享一下wxWidgets开发神经网络程序界面的一些经验,希望对开发有兴趣的朋友有所帮助.跨平台的GUI工具库以GTK +,Qt和wxWidgets闻名. G ...
- Windows下使用virtualenv创建虚拟环境
操作系统 : windowns10_x64Python版本:3.6.8virtualenv版本:16.7.7virtualenvwrapper版本:1.2.5 方式一:直接使用virtualenv 1 ...
- SpringBoot入门(简单详细教程)
Spring Boot 简介 简化Spring应用开发的一个框架:整个Spring技术栈的一个大整合:J2EE开发的一站式解决方案: 微服务 martin fowler:微服务:架构风格(服务微化): ...