Codeforces Round #666 (Div. 2)
比赛链接:https://codeforces.com/contest/1397
A. Juggling Letters
题意
给出 $n$ 个字符串,可在字符串间任意移动字母,问最终能否使这 $n$ 个字符串相同。
题解
如果可以,因为 $n$ 个字符串相同,所以每个字母的数量一定是 $n$ 的倍数。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n;
cin >> n;
vector<int> cnt(26);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (char c : s) ++cnt[c - 'a'];
}
bool ok = all_of(cnt.begin(), cnt.end(), [&](int x) {
return x % n == 0;
});
cout << (ok ? "YES" : "NO") << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}
B. Power Sequence
题意
给出一个大小为 $n$ 的数组 $a$,允许操作如下:
- 重排数组,无花费
- 将一个数 $+1$ 或 $-1$,花费为 $1$
找出使得数组 $a$ 满足 $a_i = c^i$ 的最小花费。
题解
指数增长,枚举 $c$ 即可。
因为 $3 \le n \le 10^5, 1 \le a_i \le 10^9$,所以可以将上限设为 $10^{14}$,一旦有 $c^i$ 大于这个上限就停止枚举。
代码
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a.begin(), a.end());
ll ans = LLONG_MAX;
for (int c = 1; true; c++) {
bool too_big = false;
ll change = 0;
ll cur = 1;
for (int i = 0; i < n; i++) {
change += abs(a[i] - cur);
cur *= c;
if (cur > 1e14) {
too_big = true;
break;
}
}
if (too_big) break;
ans = min(ans, change);
}
cout << ans << "\n";
}
C. Multiples of Length
题意
给出一个大小为 $n$ 的数组 $a$,操作如下:
- 选择一个区间,给其中的每个数加上区间长度的一个倍数
共要进行 $3$ 次操作,找出使得数组 $a$ 满足 $a_i = 0$ 的一种方案。
题解
$gcd(n,n-1)=1$,所以选择一次长为 $n$ 的区间,两次长为 $n - 1$ 的区间。
代码
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
if (n == 1) {
cout << 1 << ' ' << 1 << "\n" << -a[0] << "\n"
<< 1 << ' ' << 1 << "\n" << 0 << "\n"
<< 1 << ' ' << 1 << "\n" << 0 << "\n";
return 0;
}
cout << 1 << ' ' << n << "\n";
for (int i = 0; i < n; i++) {
cout << -a[i] * n << " \n"[i == n - 1];
}
cout << 1 << ' ' << n - 1 << "\n";
for (int i = 0; i < n - 1; i++) {
cout << a[i] * (n - 1) << " \n"[i == n - 2];
}
cout << 2 << ' ' << n << "\n";
for (int i = 1; i < n; i++) {
cout << a[i] * (i == n - 1 ? n - 1 : 0) << " \n"[i == n - 1];
}
}
D. Stoned Game
题意
有 $n$ 堆石子,两个人每次从另一个人没取过的石子堆中拿走一颗石子,无法再取者败,判断最终胜者。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n;
cin >> n;
vector<int> a(n);
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
sort(a.begin(), a.end());
if (a.back() > sum - a.back())
cout << "T" << "\n";
else
cout << (sum & 1 ? "T" : "HL") << "\n";
} int main() {
int t;
cin >> t;
while (t--) solve();
}
Codeforces Round #666 (Div. 2)的更多相关文章
- Codeforces Round #666 (Div. 2) Power Sequence、Multiples of Length 思维
题目链接:Power Sequence 题意: 给你n个数vi,你可以对这个序列进行两种操作 1.可以改变其中任意个vi的位置,无成本 2.可以对vi进行加1或减1,每次操作成本为1 如果操作之后的v ...
- Codeforces Round #666 (Div. 2) C. Multiples of Length (构造,贪心)
题意:有一个长度为\(n\)的序列,可以操作\(3\)次,每次选取一段区间,然后区间的元素加减区间长度的倍数,\(3\)次操作后使得序列所有元素为\(0\),问具体操作情况. 题解:假如我们能选择一整 ...
- Codeforces Round #666 (Div. 2) B. Power Sequence (枚举)
题意:有一个长度为\(n\)的序列,你每次可以对序列重新排序,然后花费\(1\)使某个元素加减\(1\),多次操作后使得新序列满足\(a_{i}=c^i\),\(c\)是某个正整数,求最小花费. 题解 ...
- Codeforces Round #666 (Div. 2) C. Multiples of Length (贪心)
题意:给你一个由\(0,1,?\)组成的字符串,你可以将\(?\)任意改成\(0\)或\(1\),问你操作后能否使得该字符串的任意长度为\(k\)的区间中的\(0\)和$1的个数相等. 题解:我们首先 ...
- Codeforces Round #532 (Div. 2) 题解
Codeforces Round #532 (Div. 2) 题目总链接:https://codeforces.com/contest/1100 A. Roman and Browser 题意: 给出 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
随机推荐
- 知识图谱和neo4j的基本操作
一.知识图谱的简介 1.知识图谱是什么 知识图谱本质上是语义网络(Semantic Network)的知识库 可以理解为一个关系图网络. 2.什么是图 图(Graph)是由节点(Vertex)和边(E ...
- Spring Boot GraphQL 实战 03_分页、全局异常处理和异步加载
hello,大家好,我是小黑,又和大家见面啦~ 今天我们来继续学习 Spring Boot GraphQL 实战,我们使用的框架是 https://github.com/graphql-java-ki ...
- 【JDBC核心】实现 CRUD 操作
实现 CRUD 操作 操作和访问数据库 数据库连接被用于向数据库服务器发送命令和 SQL 语句,并接受数据库服务器返回的结果.其实一个数据库连接就是一个 Socket 连接. java.sql 包中有 ...
- 【Spring】Spring 入门
Spring 入门 文章源码 Spring 概述 Spring Spring 是分层的 Java SE/EE 应用全栈式轻量级开源框架,以 IOC(Inverse Of Control,反转控制)和 ...
- 天梯赛练习 L3-010 是否完全二叉搜索树 (30分) 数组建树模拟
题目分析: 本题的要求是将n个数依次插入一个空的二叉搜索树(左大右小,且没有重复数字),最后需要输出其层次遍历以及判断是否是完全二叉搜索树,通过观察我们发现, 如果这个树是用数组建立的,那么最后输出的 ...
- 一道有趣的golang排错题
很久没写博客了,不得不说go语言爱好者周刊是个宝贝,本来想随便看看打发时间的,没想到一下子给了我久违的灵感. go语言爱好者周刊78期出了一道非常有意思的题目. 我们来看看题目.先给出如下的代码: p ...
- poj-Decoding Morse Sequences(动态规划)
Description Before the digital age, the most common "binary" code for radio communication ...
- Linux下nginx的安装以及环境配置
参考链接 https://blog.csdn.net/qq_42815754/article/details/82980326 环境: centos7 .nginx-1.9.14 1.下载 并解压 ...
- spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)
spring-3.1.2.cxf-3.1.3.mybaties.mysql 整合实现webservice需要的完整jar文件 地址:http://download.csdn.net/detail/xu ...
- 编年史:OI算法总结
目录(按字典序) A --A* D --DFS找环 J --基环树 S --数位动规 --树形动规 T --Tarjan(e-DCC) --Tarjan(LCA) --Tarjan(SCC) --Ta ...