Codeforces Round #609 (Div. 2) 题解
Equation
\]
这题做法很多,甚至可以直接暴力判断
view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
bool ok(int a) {
for(int i=2; i*i<=a; i++) {
if(a%i == 0) return 1;
}
return 0;
}
bool che(int a, int b) {
return ok(a) && ok(b);
}
int main() {
int d;
scanf("%d", &d);
for(int i=1000000000; i>=d; i--) {
if(che(i, i-d)) return 0*printf("%d %d\n", i, i-d);
}
return 0;
}
Modulo Equality
\]
首先 \(a[1]\) 一定会变成 \(b\) 中的某个元素,那么就可以枚举 \(a[1]\) 变成了多少,把这个数确定为 \(x\),然后判断合法性并找出所有的 \(x\)。
view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e3 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
int a[maxn], b[maxn];
int s[maxn];
int main() {
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++) scanf("%d", &a[i]);
for(int i=1; i<=n; i++) scanf("%d", &b[i]);
sort(a+1, a+1+n);
sort(b+1, b+1+n);
int ans = inf;
for(int i=1; i<=n; i++) {
int d = (b[i]-a[1]+m)%m;
for(int j=1; j<=n; j++) s[j] = (a[j]+d)%m;
sort(s+1, s+1+n);
int f = 1;
for(int j=1; j<=n; j++) {
if(s[j] != b[j]) {
f = 0;
break;
}
}
if(f) ans = min(ans, d);
}
printf("%d\n", ans);
return 0;
}
Long Beautiful Integer
\]
可以发现最后的数字一定是以 \(k\) 为循环节一直循环的,那么我们就可以考虑一开始给出数字的前 \(k\) 位,看用这 \(k\) 位循环能否更大,如果不能的话,把这 \(k\) 位数字加一,然后在开始循环。
由于用 \(k\) 个 \(9\) 来循环一定是可以的,所以不用担心加一后位数变多的问题。
view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
char s[maxn], s1[maxn], s2[maxn];
bool ok() {
for(int i=1; i<=n; i++) {
if(s2[i] > s[i]) return true;
if(s2[i] < s[i]) return false;
}
return true;
}
int main() {
scanf("%d%d", &n, &m);
scanf("%s", s+1);
for(int i=1; i<=m; i++) s1[i] = s[i];
for(int i=1, j=1; i<=n; i++) {
s2[i] = s1[j];
j = j%m+1;
}
if(ok()) return 0*printf("%d\n%s\n", n, s2+1);
for(int i=1; i<=m; i++) s1[i] = s1[i]-'0';
s1[m]++;
for(int i=m; i>=1; i--) {
if(s1[i]>=10) {
s1[i] -= 10;
s1[i-1] += 1;
}
s1[i] += '0';
}
for(int i=1, j=1; i<=n; i++) {
s2[i] = s1[j];
j = j%m+1;
}
printf("%d\n%s\n", n, s2+1);
return 0;
}
Domino for Young
\]
思维题杀我,但是这题的思路真是太优雅了。
我们把整个图看成一个国际棋盘,国际棋盘是黑白相间的,那么也就是说答案一定是 \(min\) (黑格子,白格子),因为我选了较少的那个,另一个我就一定可以找相邻的凑出来。
view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
int main() {
scanf("%d", &n);
ll ans1 = 0, ans2 = 0;
for(int i=1, x; i<=n; i++) {
scanf("%d", &x);
if(i&1) ans1+=x/2, ans2+=(x+1)/2;
else ans2+=x/2, ans1+=(x+1)/2;
}
printf("%lld\n", min(ans1, ans2));
return 0;
}
K Integers
\]
对于一个 \(k\),我们操作的规则都是先把 \(1-k\) 所有数字移动到一块,然后再进行还原。
对于还原过程,是一个很经典的问题,只需要求他的逆序数就可以了,并且这些步数是必不可少的。在每一步操作中,只需要加上 \(k\) 带来的贡献就可以,那么只要知道 \(k\) 的位置后面有多少个已经插入的数字就可以了,这一部分可以用树状数组来实现。
那么只要计算出花费最少的步数使 \(1-k\) 在一块就可以了。
我们把整个序列中,\(a[x]<=k\) 的位置看成 \(s[x]=0\),\(a[x]>k\) 的位置看成 \(s[x]=1\)。
那么现在你得到了一个长度为 \(n\),\(1\) 的个数为 \(k\),形如 \(1、0、0、1、1、0、1、1、0\) 的数组,而你想要让所有的 \(1\) 靠在一块。
令 \(p[i]\) 为 \(s[i]\) 的前缀和,那么对于每一个 \(s[i]=0\),把这个 \(0\) 移出去的最少步数是 \(min(p[i], k-p[i])\),而你需要的步数就是 \(\sum s[i]\),因为你只要从两侧开始操作,每一个 \(0\) 都可以用最少的步数移出去并让中间一块全为 \(1\)。
由于 \(k\) 高达 \(2e5\),所以这部分必须高效的维护,我们发现,我们可以找出第 \(\frac{k+1}{2}\) 个 \(1\) 在哪里,然后左侧的 \(s[i]=0\) 的贡献都是 \(p[i]\),右侧的 \(s[i]=0\) 的贡献都是 \(k-p[i]\)。而每次只多一个数字,所以这个 \(\frac{k+1}{2}\) 的位置只会往左或者往后移动一个 \(1\) 或者不动。那么只要用一个 \(set\) 来维护这个 \(1\) 的位置,然后维护一下新插入的 \(1\) 造成的贡献就可以了。
view
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
set<int> st;
int a[maxn], p[maxn];
ll sum[maxn];
void update(int x) {
for(int i=x; i; i-=lowbit(i)) sum[i]++;
}
ll query(int x) {
if(x==0) return 0;
int ans = 0;
for(int i=x; i<=n; i+=lowbit(i))
ans += sum[i];
return ans;
}
int main() {
scanf("%d", &n);
for(int i=1; i<=n; i++) scanf("%d", &a[i]), p[a[i]] = i;
ll ans = 0, k = 0;
printf("0 ");
update(p[1]);
st.insert(p[1]);
auto it = st.begin();
for(int i=2; i<=n; i++) {
ans -= min(query(p[i]+1), i-1-query(p[i]+1));
ans += query(p[i]+1);
update(p[i]), st.insert(p[i]);
if(i%2==0 && p[i]<(*it)) ans += abs((*it)-(*(--it)))-1;
if(i%2==1 && p[i]>(*it)) it++;
if(p[i] > (*it)) ans += p[i]-(*it)+1 - query((*it))+query(p[i]+1);
else ans += (*it)-p[i]+1 - query(p[i])+query((*it)+1);
printf("%lld ", ans);
}
return 0;
}
Codeforces Round #609 (Div. 2) 题解的更多相关文章
- Codeforces Round #609 (Div. 2)前五题题解
Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
随机推荐
- k8s 二进制部署详解
环境说明: 192.168.1.101 -- master01 + etcd01 192.168.1.102 -- etcd02 192.168.1.103 -- etcd03 192.168.1.1 ...
- docker 更新内存限制步骤
停止容器: docker stop id 更新配额: docker update -m 80G id 内存参数和大小 容器ID重启容器:docker start id
- Python之np.random.permutation()函数的使用
官网的解释是:Randomly permute a sequence, or return a permuted range. 即随机排列序列,或返回随机范围.我的理解就是返回一个乱序的序列.下面通过 ...
- 如何在yii1.0.7中设置数据库连接超时?
继承CDbConnection, 覆盖 init()方法 在 parent::init() 之前设置 $this->setAttribute(PDO::ATTR_TIMEOUT, $this-& ...
- Winform中设置ZedGraph的曲线符号Symbol以及对应关系
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- qt 界面去掉系统边框2.0版
之前的一版存在bug.如果将鼠标放移动到界面内某个可点击的widget上(如:QPushButton)上,按住鼠标左键不放,界面可能会出现界面非预期移动的问题. 那是因为当鼠标移动到可点击的widge ...
- JDK1.8新特性——Stream API
JDK1.8新特性——Stream API 摘要:本文主要学习了JDK1.8的新特性中有关Stream API的使用. 部分内容来自以下博客: https://blog.csdn.net/icarus ...
- Java基础—实现多线程的三种方法
Java虚拟机(JVM,是运行所有Java程序的抽象计算机,是Java语言的运行环境)允许应用程序并发地运行多个线程.在Java语言中,多线程的实现一般有以下三种方法: 1.实现Runnable接口, ...
- Windows+Qt使用gRPC
上篇文章<Windows+VS2017使用gRPC>编译出了Windows下可用的gRPC静态lib库文件,在此基础上要想在Qt上使用,需要使用MSVC2017 64bit构建组件进行构建 ...
- linux清屏
clear 这个命令将会刷新屏幕,本质上只是让终端显示页向后翻了一页,如果向上滚动屏幕还可以看到之前的操作信息 reset 这个命令将完全刷新终端屏幕,之前的终端输入操作信息将都会被清空,这样虽然比较 ...