Codeforces Round #617 (Div. 3) 题解
又是隔了一年才来补题的我
A、B水题就不用说了
C - Yet Another Walking Robot
C题我居然卡了一会,最后决定用map水,结果出来看了看题解,居然真的是map...没想到会出这样题解用stl的方法,是我失策了
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
const int N = * ;
char s[N];
typedef pair <int, int> p;
map <p, int> k;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
scanf("%s", s);
k.clear();
int c1 = , c2 = ;
int ans2 = 0x3f3f3f3f, ans1 = ;
k[make_pair(, )] = ;
for (int i = ; i < n; i++) {
if (s[i] == 'L') c1--;
else if (s[i] == 'R') c1++;
else if (s[i] == 'U') c2++;
else c2--;
if (k.find(make_pair(c1, c2)) == k.end()) {
k[make_pair(c1, c2)] = i + ;
continue;
}
int z = k[make_pair(c1, c2)];
if (i - z < ans2 - ans1)
ans2 = i + , ans1 = z + ;
k[make_pair(c1, c2)] = i + ;
}
if (ans1 == )
puts("-1");
else
printf("%d %d\n", ans1, ans2);
}
return ;
}
//LRUD
// 6 LLLLUUURRRRDDDLL
// LLLLLLUUURRDDDL
D - Fight with Monsters
比较水,先处理出一定能杀死的,把最后一轮剩下的丢尽一个优先队列里(直接排序也行),从小到大判断要用几次秘密武器才能打怪成功,注意要判断优先队列是不是非空(不然就会TLE一次...
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int hp[ * ];
priority_queue < int, vector < int >, greater < int > > Q;
int main() {
int n, a, b, k, ans = ;
scanf("%d %d %d %d", &n, &a, &b, &k);
for (int i = ; i < n; i++) {
scanf("%d", &hp[i]);
if (hp[i] % (a + b) == )
Q.push(b);
else {
int res = hp[i] % (a + b);
if (res <= a)
ans++;
else
Q.push(res - a);
}
}
while (k > && !Q.empty()) { //就是这里
int u = Q.top(); Q.pop();
int cnt = u / a;
if (u % a != ) cnt++;
if (cnt > k)
break;
else
ans++, k -= cnt;
}
printf("%d", ans);
return ;
}
E1 - String Coloring (easy version)
题解是贪心或者dp,而我是二分图染色(迷惑行为),主要还是没看出来本质是划分子集,所以用二分图水过了,简单说一下题解做法吧,因为两个字母不按照字母顺序就要交换,所以如果把每个编号的视为一组,那么同组就是递增的,反之也是一样,如果能划分成两个上升的子序列,那一定可以按题目方式染色,因为你可以把不同颜色的字母随意改变顺序。还有一个方法是贪心,这个方法很有意思,目前有两个序列,判断当前这个能不能加在1后面,如果不行,能不能加在2后面,都不行就不能划分了,这种贪心方式的正确性在于,首先新的字母,无论加在序列1还是2后面产生的效果都是最后一个变成了s[i],由于先考虑序列1后考虑序列2,所以序列二的结尾总是小于序列1的,所以先加在1后面能保证序列二结尾尽可能的小,从而保证了贪心的正确性。
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
char s[];
vector < int > e[];
int col[];
inline void add(int a, int b) {
e[a].push_back(b);
e[b].push_back(a);
}
int ans = ;
void dfs(int a, int c) {
col[a] = c;
for (int i = ; i < e[a].size(); i++) {
int u = e[a][i];
if (col[u] == -)
dfs(u, (c + ) % );
else if (col[u] == col[a]) {
ans = -;
return ;
}
}
} int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
memset(col, 0xff, sizeof(col));
for (int i = ; i < n; i++)
for (int j = i + ; j < n; j++)
if (s[i] > s[j])
add(i, j);
for (int i = ; i < n; i++)
if (col[i] == -)
dfs(i, );
if (ans == -)
puts("NO");
else {
puts("YES");
for (int i = ; i < n; i++)
printf("%d", col[i]);
}
return ;
}
E2 - String Coloring (hard version)
下面这道题也很有意思,我刚做完的牛客比赛里正好有类似的题...有了easy版的推论,我们能看出就是最少分成几个不下降子序列,那就应用Dilworth定理,链划分的最少集合数等于最长反链长度,翻译成人话就是算最长下降子序列,这个nlogn的方法应该都知道,所以麻烦之处就在于划分,但是我们可以发现对于第i个字母s[i],以s[i]结尾的最长下降子序列长度就是它在的集合,首先相同长度一定是不下降的子序列,再者,对于s[i]而言,无法加到以大于s[i]结尾的序列后面,所以就只能成为一个新的序列。
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = * 1e5 + ;
char s[N];
int dp[N], ans[N];
inline bool cmp(int a, int b) {
return a > b;
}
int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
int len = ;
dp[] = s[] - 'a';
ans[] = ;
for (int i = ; i < n; i++) {
if (s[i] - 'a' < dp[len]) {
dp[++len] = s[i] - 'a';
ans[i] = len;
}
else {
int pos = lower_bound(dp + , dp + len + , s[i] - 'a', cmp) - dp;
ans[i] = pos;
dp[pos] = s[i] - 'a';
}
}
printf("%d\n", len);
for (int i = ; i < n; i++)
printf("%d ", ans[i]);
return ;
}
Codeforces Round #617 (Div. 3) 题解的更多相关文章
- 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个折扣的时候,你必须 ...
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
随机推荐
- codewars--js--RGB To Hex Conversion
问题描述: The rgb() method is incomplete. Complete the method so that passing in RGB decimal values will ...
- Netty——知识点总结
引言 Netty blablabla…… Netty 知识点
- ES6 - 基础学习(3): 变量的解构赋值
解构赋值概述 1.解构赋值是对赋值运算符的扩展. 2.它是一种针对数组或者对象进行模式匹配,然后对其中的变量进行赋值. 3.代码书写上显得简洁且易读,语义更加清晰明了:而且还方便获取复杂对象中的数据字 ...
- iOS 中事件的响应链和传递链
iOS事件链有两条:事件的响应链:Hit-Testing事件的传递链 响应链:由离用户最近的view向系统传递.initial view –> super view –> ….. –> ...
- 区间操作---树状数组&&线段树
涉及区间操作的一些套路必须要会呀 区间加减为了偷懒能不写线段树so我选择树状数组!! 但是区间乘除,最大值我想了想还是用线段树分块吧. 树状数组: 这里用网上的一张图: 这里灰色数组是原本的数组(a[ ...
- javascript中如何使用js脚本模拟"request"获取url后参数值呢?
转自:猫猫小屋--js获取url后参数信息 摘要: 下文讲述javascript中使用js代码获取url地址后面的参数值的方法分享,如下所示: 实现思路: 使用正则表达式对参数值进行匹配,获取参数后的 ...
- linux centos7 非root用户安装源码版docker
注意:非root用户必须要有sudo权限 一.安装前的准备 1.查看当前主机是否有docker组 若没有输出结果则新建 再次查看,发现已经有了docker组 2.新增拥有sudo权限的用户(若知道ro ...
- NIO学习笔记,从Linux IO演化模型到Netty—— Linux零拷贝
这里只是感性地认识Linux零拷贝,不涉及具体细节. 1.Linux传统的数据拷贝 用户进程是不能直接访问文件系统的,要先切换到内核态,发起系统调用,DMA把磁盘中的数据写入内核空间,内核再把数据拷贝 ...
- C++\CLI使用.net委托,*Callback注意"this"
今天在使用c++\cli的时候遇到了点关于委托,callback使用的问题,简单记录一下 首先贴段简单的C#中使用System.Threading.Timer的代码. Timer GameTim ...
- [CF1311F] Moving Points - 树状数组
Solution 按 \(x\) 关键字升序排序,依次枚举每个点 考虑对任意 \(x_j < x_i\),那么当 \(v_j \leq v_i\) 时,它们不会相交,且 \(dis\) 就是它们 ...