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个折扣的时候,你必须 ...
随机推荐
- [LOJ 6433][PKUSC 2018]最大前缀和
[LOJ 6433][PKUSC 2018]最大前缀和 题意 给定一个长度为 \(n\) 的序列, 求把这个序列随机打乱后的最大前缀和的期望乘以 \(n!\) 后对 \(998244353\) 取膜后 ...
- 【新特性速递】单元格导航(上下左右键,TAB键和ENTER键)
上下左右按键 其实单元格导航(上下左右按键,需要启用表格的ShowSelectedCell属性)一直都存在,只不过之前的版本(v5.5.0)有一些小的BUG. BUG1 比如锁定列存在时,上下左右键只 ...
- 物联网架构成长之路(44)-Docker私有仓库Harbor
0. 前言 安装docker.docker-compose,这些在我以前的博客讲过,这里就不继续说明了,有需要的可以参考我之前的博客. https://www.cnblogs.com/wunaozai ...
- 【08月28日】A股滚动市盈率PE历史新低排名
2010年01月01日 到 2019年08月28日 之间,滚动市盈率历史新低排名. 上市三年以上的公司,2019年08月28日市盈率在300以下的公司. 1 - 文山电力(SH600995) - 历史 ...
- hue框架介绍和安装部署
大家好,我是来自内蒙古的小哥,我现在在北京学习大数据,我想把学到的东西分享给大家,想和大家一起学习 hue框架介绍和安装部署 hue全称:HUE=Hadoop User Experience 他是cl ...
- Vue.js 源码分析(十五) 指令篇 v-bind指令详解
指令是Vue.js模板中最常用的一项功能,它带有前缀v-,比如上面说的v-if.v-html.v-pre等.指令的主要职责就是当其表达式的值改变时,相应的将某些行为应用到DOM上,先介绍v-bind指 ...
- C# 调用腾讯即时通信 IM
IM SDK API 概述 https://cloud.tencent.com/document/product/269/33543 /// <summary> /// IM SDK 初始 ...
- 运算符 &(与运算)、|(或运算)、^(异或运算)
按位与运算符(&) 参加运算的两个数据,按二进制位进行“与”运算. 运算规则:0&0=0; 0&1=0; 1&0=0; 1&1=1; 按位或运算符( ...
- maven 学习---Maven构建自动化-Hudson
建立自动化定义场景,依赖项目建设过程中被启动,一旦项目生成成功完成,以确保相关的项目是稳定的. 实例 考虑一个团队正在开发一个项目总线核心API上的其他两个项目的应用程序,网页UI和应用程序的桌面UI ...
- unlink remove
int unlink(const char *pathname); 删除一个文件的目录项并减少它的链接数 unlink()会删除参数pathname指定的文件.如果该文件名为最后连接点,但有其他进程打 ...