CodeForces 879D Teams Formation
题意
将一个长度为\(n\)的数组重复\(m\)遍得到一个长度为\(n \times m\)的新序列,然后消掉新序列中连续\(k\)个相同的元素,不断重复这一过程,求最后剩下的序列的长度
分析
首先可以明确一件事就是消除的顺序是任意的,最终得到的序列是相同的
消除的块有两种情况:
- 块在序列内部
- 块在序列交界处
首先可以消掉每个序列内部可以消去的块,然后再考虑第二部分
两个序列首位相接,每遇到\(k\)个连续的相同颜色的块就消去(注意消去的总长度不超过\(n\)),最终将一个序列分为左中右三部分
这样就是前一个序列的右部分和后一个序列的左部分进行消去,\(m\)个序列进行消去后就变成了:
左部分+中间部分重复\(m\)次+右部分
然后再分成三种情况讨论:
- 重复\(m\)次的中间部分是同一种颜色且是\(k\)的倍数,那么最终消去了所有元素
- 重复\(m\)次的中间部分是同一种颜色但不能全部消去,那么答案就是消去中间能消的再加上两端
- 中间部分有两种颜色或以上说明已经没有可消去的块了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define MP make_pair
#define PB push_back
#define REP(i, a, b) for(int i = a; i < b; i++)
#define PER(i, a, b) for(int i = b - 1; i >= a; i--)
const int maxn = 100000 + 10;
int n, m, k;
int a[maxn], S[maxn], cnt[maxn], top;
int main() {
bool single_color = true;
scanf("%d%d%d", &n, &k, &m);
REP(i, 0, n) scanf("%d", a + i);
REP(i, 1, n) if(a[i] != a[i - 1]) { single_color = false; break; }
if(single_color) {
printf("%I64d\n", (LL)n * m % k);
return 0;
}
REP(i, 0, n) {
S[++top] = a[i];
if(top > 1 && S[top] == S[top-1]) cnt[top] = cnt[top-1] + 1;
else cnt[top] = 1;
if(cnt[top] >= k) top -= k;
}
int L = 1, R = top;
LL t = 0;
while(S[L] == S[R] && L < R) {
int l = L, r = R, cnt = 0;
while(S[l] == S[L] && l < r && cnt < k) { cnt++; l++; }
while(S[r] == S[L] && l < r && cnt < k) { cnt++; r--; }
if(cnt == k) { L = l; R = r; t += k; }
else break;
}
single_color = true;
REP(i, L, R) if(S[i] != S[i+1]) { single_color = false; break; }
if(single_color) {
LL mid = (LL)(R-L+1)*m%k;
if(mid) printf("%lld\n", mid + t);
else printf("0\n");
} else {
printf("%lld\n", (LL)(R-L+1)*m + t);
}
return 0;
}
CodeForces 879D Teams Formation的更多相关文章
- 【Codeforces】879D. Teams Formation 思维+模拟
题意 给定$n$个数,重复拼接$m$次,相邻$k$个重复的可消除,问最后序列中有多少个数 首先可以发现当$k>=n$时,如果要使$n$个数可以被消除,那么$n$个数必须一样,否则$n$个数不能被 ...
- Codeforces Round #443 (Div. 1) B. Teams Formation
B. Teams Formation link http://codeforces.com/contest/878/problem/B describe This time the Berland T ...
- cf 443 D. Teams Formation](细节模拟题)
cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...
- codeforces 879 D. Teams Formation(思维)
题目链接:http://codeforces.com/contest/879/problem/D 题意:这题题意我反正是看了很久,可能是我的理解能力有点差,就是将一个数组倍增m倍然后将连续的相同的k个 ...
- 443 D. Teams Formation
http://codeforces.com/contest/879/problem/D This time the Berland Team Olympiad in Informatics is he ...
- Teams Formation
题意: 给定一长度为 n 的整数序列 $a$,将其复制m次,并接成一条链,每相邻K个相同的整数会消除,然后其他的整数继续结成一条链,直到不能消除为止,求问最终剩余多少个整数. 解法: 首先将长度为n的 ...
- Codeforces Round #443 (Div. 2) 【A、B、C、D】
Codeforces Round #443 (Div. 2) codeforces 879 A. Borya's Diagnosis[水题] #include<cstdio> #inclu ...
- codeforces 478B Random Teams
codeforces 478B Random Teams 解题报告 题目链接:cm.hust.edu.cn/vjudge/contest/view.action?cid=88890#probl ...
- Codeforces 552 E. Two Teams
E. Two Teams time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- python 修改xml文件
在百度知道里面有人问了这么一个问题: 有一个xml文件:<root>text <a/> <a/> ...这里省略n个<a/> <root>想 ...
- 通过Exception获取其中的信息
private static String getCrashMessage(Exception ex) { Writer writer = new StringWriter(); Pri ...
- 计算结构体、数组、指针的sizeof
1. 结构体的sizeof 题目: sturct aa{ in num; char name[10];}; struct bb{ int a; float b; struct aa c;}; stru ...
- 安卓装tensorflow
1.https://blog.csdn.net/masa_fish/article/details/54096996 tensorflow有很多中安装方式,但你要想和android结合,就只能采用源码 ...
- matlab中padarray函数在numpy、python中的实现
a = np.arange(6) a = a.reshape((2, 3)) print np.lib.pad(a, 1, 'symmetric') 运行结果: [[ ] [ ] [ ] [ ]]
- 前端css样式及选择器
标题: 1.scc概述 2.行内样式 3.内接样式 4.外接样式(链接式) 推荐使用 5.外接样式(导入式) 6.嵌套规则 7.css选择器 1.scc(Cascading Style Shee ...
- PowerDesigner 图表导出Excel格式
快捷方式打开运行界面:ctrl+shift+X '*************************************************************************** ...
- git(将现有项目加入osChina)
将现有项目加入osChina 在osChina中创建项目 注意不要初始化项目.(其实初始化也没有什么问题,可以直接clone到本地,再把项目添加进去就行了,后续操作一样的) 项目现在基本为空,得到项目 ...
- jquery 表单事件
.blur() 当元素失去焦点的时候触发事件. .blur(handler(eventObject)) handler(eventObject) 每当事件触发时候执行的函数. .blur([event ...
- 1、React-Native的基础入门
React Native (简称RN)是Facebook于2015年4月开源的跨平台移动应用开发框架,是Facebook早先开源的JS框架 React 在原生移动应用平台的衍生产物,目前支持iOS和安 ...