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 题解 直接 ...
随机推荐
- C语言基础五 数组
数组跟变量的区别? 数组是可以在内存中连续存储多个元素的结构,所有元素必须属于相同类型. 格式:元素类型 数组名[元素个数]: 数组的特点: 只能存放单一元素的数据,里面存放的数据成为元素. 数组的声 ...
- Java的引用类型的内存分析
一. jdk的内存:jdk的bin目录常见命令 1. javac.exe:编译java源代码的,生成java字节码文件(*.class) 2. java.exe:启动一个jvm,来运行指定class字 ...
- JavaScript的算数,赋值,比较和逻辑运算符
类似a=1+1这样的表达式称为运算符,js的运算符分为算数,赋值,比较和逻辑运算符:常见的算数有:+ - * / %(加减乘除,取模),比方说5/4=4*1+1:5%4=1,js算数顺序:从左往右,先 ...
- 05.JS函数
前言: 学习一门编程语言的基本步骤(01)了解背景知识(02)搭建开发环境(03)语法规范(04)常量和变量(05)数据类型(06)数据类型转换(07)运算符(08)逻辑结构(09)函数9.函数——f ...
- 使用 Apache James 3.3.0(开源免费) 搭建内网电子邮件服务器(基于 Windows + Amazon Corretto 8)
电子邮件服务器,对于很多公司,都是需要的. 虽然现在很多人,使用 QQ .微信进行一对一的工作沟通,使用QQ 群.微信群进行多人沟通,但这些即时聊天工具,与电子邮件相比,仍有很多不足: a. 电子邮件 ...
- Android Studio 学习笔记(四):Adapter和RecyclerView说明
在现版本中,滚动控件有多种,而相比于ListView,GridView,RecyclerView的用途更广,因此将前两者作为Adapter适配器的引入,再对RecyclerView进行简单讲解. MV ...
- redis 5.0.7 源码阅读——动态字符串sds
redis中动态字符串sds相关的文件为:sds.h与sds.c 一.数据结构 redis中定义了自己的数据类型"sds",用于描述 char*,与一些数据结构 typedef c ...
- 在window上安装mysql - MySQL5.7.24 版本
1.下载安装包 下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 点击Download,选择 No thanks, just ...
- [Contract] Solidity 遍历 mapping 的一种方式
思路:为需要遍历的 mapping 再准备一个 list,之后通过 for 循环遍历 list 取得 mapping 的 key. mapping (address => uint) users ...
- CSP2019第二轮-划水游记
又是 颓废的 一年 眨眼间已经初三了 到了NOIPCSP的时节 Day0 学校(没错,全校事件)抽风地把二晚停了,于是也就有了机会早早回家van♂耍 和母上大人简单地收拾收拾东西,回了姥姥家 以&qu ...