Educational Codeforces Round 114 (Rated for Div. 2) 个人题解
中秋节快乐!
A. Regular Bracket Sequences
题意
输出nnn个不同的长度为2n2n2n的合法括号序列.
分析
先输出一个"()()()…"序列.
然后依次输出"(())()", “()(())”,…,也就是每次把第iii个和第i+1i+1i+1个交换,其中iii从下标2到n-2,这样恰好n−1n-1n−1个,加上最开始的共nnn个。
代码
#include <bits/stdc++.h>
#define fors(i, a, b) for(int i = (a); i <= (b); ++i)
#define lson k<<1
#define rson k<<1|1
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mem(a) memset(a, 0, sizeof(a))
#define DDLC_ESCAPE_PLAN_FAILED ios::sync_with_stdio(false), cin.tie(0)
#define int long long
const int inf = 0x3f3f3f3f;
const double dinf = 1e100;
typedef long long ll;
//const ll linf = 9223372036854775807LL;
// const ll linf = 1e18;
using namespace std;signed main()
{DDLC_ESCAPE_PLAN_FAILED;int t;cin >> t;while(t--){int n;cin >> n;string s;for(int i = 0; i < n; ++i){s += '(';s += ')';}cout << s << endl;for(int i = 1; i + 1 < s.size(); i += 2){s[i] = '(';s[i + 1] = ')';cout << s << endl;s[i] = ')';s[i + 1] = '(';}}return 0;
}
B. Combinatorics Homework
题意
你需要判断是否存在一个字符串:
- 恰好a个’A’,b个’B’,c个‘C’
- 没有其他字符
- 恰好m对相邻且相同的字符对(如"AA")
分析
目标是寻找可以满足要求的mmm取值范围。
将三种字符的数量排序,找到其个数最多的,假设为’A’,记’B’和’C’的数量和为sss。
mmm最大取值显然是a+b+c−3a+b+c-3a+b+c−3.
找mmm的最小值。把sss个字符排开,其有s+1s+1s+1个位置可以插入’A’。若’A’的数量不超过s+1s+1s+1,那么肯定可以达到最小状态,即不存在相邻且相同的字符对。可能会想到’A’没有填满所有空缺时有可能’B’或者’C’会出现连续的情况,但其实这种情况发生的话’A’肯定就不是个数最多的了。反之,若’A’的数量超过了s+1s+1s+1,那么至少出现的相同字符对就是s+1−as+1-as+1−a对。
代码
#include <bits/stdc++.h>
#define fors(i, a, b) for(int i = (a); i <= (b); ++i)
#define lson k<<1
#define rson k<<1|1
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mem(a) memset(a, 0, sizeof(a))
#define DDLC_ESCAPE_PLAN_FAILED ios::sync_with_stdio(false), cin.tie(0)
#define int long long
const int inf = 0x3f3f3f3f;
const double dinf = 1e100;
typedef long long ll;
//const ll linf = 9223372036854775807LL;
// const ll linf = 1e18;
using namespace std;signed main()
{DDLC_ESCAPE_PLAN_FAILED;int t;cin >> t;while(t--){int a, b, c, m;cin >> a >> b >> c >> m;int mx = a + b + c - 3;int p[3] = {a, b, c};sort(p, p + 3);int r2 = p[0] + p[1] + 1, r1 = p[2];int mn = max(0LL, r1 - r2);if(mx >= m && mn <= m){cout << "YES" << endl;}else cout << "NO" << endl;}return 0;
C. Slay the Dragon
题意
有mmm条龙和nnn个勇士,每个勇士的力量是aia_iai,每个龙的防御力为xix_ixi,攻击力为yiy_iyi。对每条龙iii,你需要派遣一个勇士,要求力量大于等于xix_ixi,如果不足xix_ixi,需要支付等量的金币补足差值;同时要求剩余的勇士力量总和大于等于yiy_iyi,不足用金币补足。问击杀每条龙需要支付的金币至少是多少(每条龙之间分别计算,相互独立)
分析
贪心,对勇士力量升序排序。对每条龙,lower_bound(a+1,a+1+n, x[i])
查找出一个勇士sss,如果要派遣一个大于等于xix_ixi的勇士去,那么必定是派sss去。
但又有可能这个sss超过xix_ixi很多而其他的总和不足yiy_iyi,这个时候s−1s-1s−1也可能是答案。
所以答案要么是派sss去,要么是派s−1s-1s−1去。
代码
#include <bits/stdc++.h>
#define fors(i, a, b) for(int i = (a); i <= (b); ++i)
#define lson k<<1
#define rson k<<1|1
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mem(a) memset(a, 0, sizeof(a))
#define DDLC_ESCAPE_PLAN_FAILED ios::sync_with_stdio(false), cin.tie(0)
#define int long long
const int inf = 0x3f3f3f3f;
const double dinf = 1e100;
typedef long long ll;
//const ll linf = 9223372036854775807LL;
// const ll linf = 1e18;
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn];
int x[maxn], y[maxn];
int pre[maxn];
signed main()
{DDLC_ESCAPE_PLAN_FAILED;int t;t = 1;a[0] = 0;while(t--){int n;cin >> n;fors(i, 1, n) cin >> a[i];int m;cin >> m;fors(i, 1 , m) cin >> x[i] >> y[i];sort(a + 1, a + 1 + n);pre[0] = 0;fors(i, 1, n) pre[i] = pre[i - 1] + a[i];fors(i, 1, m){int now = lower_bound(a + 1, a + 1 + n, x[i]) - a;int ans = 0;if(now == n + 1){ans += x[i] - a[n];ans += (pre[n - 1] >= y[i] ? 0 : y[i] - pre[n - 1]);cout << ans << endl;}else{// 要么选now,要么选now-1.ans = (pre[n] - a[now] >= y[i] ? 0 : y[i] - (pre[n] - a[now]));if(now == 1){cout << ans << endl;continue;}int res = 0;now--;res += x[i] - a[now];res += (pre[n] - a[now] >= y[i] ? 0 : y[i] - (pre[n] - a[now]));ans = min(ans, res);cout << ans << endl;}}}return 0;
}
D. The Strongest Build
题意
有n(n≤10)n(n\leq 10)n(n≤10)个单调不下降数组,每个数组长cic_ici,第iii个数组的第jjj个元素表示为aija_{ij}aij,保证∑ci≤2⋅105\sum c_i\leq2·10^5∑ci≤2⋅105,你需要从每个数组中选一个元素,但约定有m(m≤105)m(m\leq 10^5)m(m≤105)个选择方案是不允许的。求一个方案使得所有选择的元素和最大,输出方案。
分析
暴力枚举。首先考虑所有数组都选最后一个元素(最大的),如果这个方案不行,那就枚举所有被禁止的方案。例如,假如某个方案{a,b,c}是禁止的,那么就看{a-1,b,c},{a,b-1,c},{a,b,c-1}有没有禁止,没有就更新答案,这样枚举出来的一定是最优解。
贪心思路:
若{ac1,ac2,...}\{a_{c_1},a_{c_2},...\}{ac1,ac2,...}这个在没有限制条件下的方案被限制了的话,可能的最优解就从{ac1−1,ac2,...},{ac1,ac2−1,...}\{a_{c_1-1},a_{c_2},...\},\{a_{c_1},a_{c_2-1},...\}{ac1−1,ac2,...},{ac1,ac2−1,...}里面产生。如果这些方案中也有被限制的,那么就再从这些中枚举每个位置分别将其减一再更新答案。这样可以保证每次都是“退而求其次”,但一定是可选择的里面最优的。毕竟m≤105m\leq 10^5m≤105,故最多需要枚举mnmnmn次。加上二分查找,时间复杂度为O(mnlogm)O(mn\log m)O(mnlogm)
代码
#include <bits/stdc++.h>
#define fors(i, a, b) for(int i = (a); i <= (b); ++i)
#define lson k<<1
#define rson k<<1|1
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mem(a) memset(a, 0, sizeof(a))
#define DDLC_ESCAPE_PLAN_FAILED ios::sync_with_stdio(false), cin.tie(0)
#define int long long
const int inf = 0x3f3f3f3f;
const double dinf = 1e100;
typedef long long ll;
//const ll linf = 9223372036854775807LL;
// const ll linf = 1e18;
using namespace std;
const int maxn = 2e5 + 10;
int a[12][maxn];
int n;
set<vector<int> > v;
signed main()
{DDLC_ESCAPE_PLAN_FAILED;cin >> n;fors(i, 1, n){cin >> a[i][0];fors(j, 1, a[i][0]) cin >> a[i][j];}int m, p;cin >> m;vector<int> tmp;fors(I, 1, m){fors(j, 1, n){cin >> p;tmp.pb(p);}v.insert(tmp);tmp.clear();}vector<int> ideal; fors(i, 1, n) ideal.pb(a[i][0]);// bool flag = 0;if(v.find(ideal) == v.end()){for(auto x : ideal) cout << x << ' ';cout << endl; return 0;}else{int ans = 0;vector<int> f;for(auto x : v){tmp = x;int sum = 0;for(int i = 0; i < x.size(); ++i){sum += a[i + 1][x[i]];}for(int j = 0; j < x.size(); ++j){if(x[j] - 1 == 0) continue;sum -= a[j + 1][x[j]];sum += a[j + 1][x[j] - 1];x[j]--;if(v.find(x) == v.end() && sum > ans) ans = sum, f = x;x[j]++;sum += a[j + 1][x[j]];sum -= a[j + 1][x[j] - 1];}}for(auto x : f) cout << x << ' ';cout << endl;}return 0;
}
Educational Codeforces Round 114 (Rated for Div. 2) 个人题解相关推荐
- Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...
- Educational Codeforces Round 114 (Rated for Div. 2) D. The Strongest Build 暴力 + bfs
传送门 文章目录 题意: 思路: 题意: 你有nnn个装备槽,每个槽里面有cic_ici个力量加成,对于每个槽只能选一个力量加成,现在给你mmm个力量组合[b1,b2,...,bn][b_1,b_2 ...
- Educational Codeforces Round 104 (Rated for Div. 2)A-E题解
Educational Codeforces Round 104 (Rated for Div. 2)A-E题解 比赛链接:https://codeforces.ml/contest/1487 A题 ...
- Educational Codeforces Round 114 (Rated for Div. 2)C. Slay the Dragon
题目链接:Problem - 1574C - Codeforces Recently, Petya learned about a new game "Slay the Dragon&quo ...
- 1574D The Strongest Build (Educational Codeforces Round 114 (Rated for Div. 2))
题意 给定n个从小到大的数组,从每个数组中选出一个下标构成一个序列,但是有m种序列被ban了,要求选出的序列对应的数字和最大且没有被ban. 思路 因为有m个序列被ban了,那么最坏的情况可以假设为最 ...
- Educational Codeforces Round 90 (Rated for Div. 2)部分题解
A - Donut Shops 题解: 1.我们首先特判一下包装盒里面只有一个物品的情况,如果只装了一个物品,那我们就直接比较这两个物品的单价即可. 2.如果盒子里面装的不止是一键物品,那么我们就需要 ...
- Educational Codeforces Round 119 (Rated for Div. 2) EFG 题解
Solution 赛时切了 ABCDEG,不会 F. E 考虑使用若干个集合 S i S_i Si 维护各个数出现的位置,那么两个操作分别可以被抽象为: 在一个集合中加入一个数. 将集合 S x S ...
- Educational Codeforces Round 131 (Rated for Div. 2) A-D题解
题目 A. Grass Field B. Permutation C. Schedule Management D. Permutation Restoration A. Grass Field 题解 ...
- Educational Codeforces Round 105 (Rated for Div. 2) A-D题解
A. ABC String 题意 给你一个只包含 A.B.C的字符串,每类字符都有可能代表左右括号中的一种,问你是否能找到一个方式使得括号匹配合法 三个字符,每个字符有两种情况 总计 2^3 = 8次 ...
最新文章
- SpringCloud系列二:Restful 基础架构(搭建项目环境、创建 Dept 微服务、客户端调用微服务)...
- cvs update 用法_WinCVS的配置与使用方法
- Android实现仿美图秀秀给图片加框
- Python、Perl 垫底,C语言才是最环保的编程语言
- numpy 矩阵与向量相乘_有人把NumPy画成了花,生动又形象
- JFinal interceptor - Deal with session attributes
- 随笔_拉普拉斯变换的困惑点
- AAC规格(LC,HE,HEv2)及性能对比
- [转载] 数组快速排序python_python实现快速排序
- Equals() 和 运算符 == 重载准则 (C# 编程指南)
- 弱电工程标书制作,从入门到精通
- 百度贴吧客户端(Android)网络通信行为分析
- Python爬取Facebook公共主页帖子
- 学大伟业:2019年数学竞赛学习经验分享
- 计算机组成原理 启航教育,2021计算机考研:计算机组成原理知识点CPU的功能和基本结构...
- Dell戴尔笔记本电脑G3 3579原装出厂Windows10系统恢复原厂oem系统
- Linux tar压缩和解压
- Go --- 使用各服务商的短信服务,实现短信验证等需求
- 梳理 | 机器人学习(Robot Learning)的发展
- python 下载qq群文件_python获取所有qq好友、全部群所有成员部分信息,并保存列表至电子表格文件...