题意

给定$n$个数,重复拼接$m$次,相邻$k$个重复的可消除,问最后序列中有多少个数


首先可以发现当$k>=n$时,如果要使$n$个数可以被消除,那么$n$个数必须一样,否则$n$个数不能被消除

当$k<n$时,首先对序列能够消除的消除,用栈记录每个数和这个数的相同的相邻个数$f$,将序列压缩,考虑一次拼接,记$L$为第二个序列的开始元素,$R$为第一个序列的最后一个元素,如果$L!=R$那么所有序列将不能压缩,如果$LR,f_L+f_R!=k$,那么这两个序列拼接之后会合并成一个不可被消去的元素,减去$m-1$次的贡献得到答案,如果$LR,f_L+f_R==k$,那么这两个可以完全被消除,那么按照相同方式计算$L+1,R-1$对应的贡献,最后如果整个序列都能合并到最后,如果$n$为奇数,表示最后会剩下一个元素,计算这个元素对应答案,如果$n$为偶数,那么答案为$0$

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int st[100100], f[100100];
int n, k, m, a, flag = 0, top = 1;
LL ans = 0;
int main() {
scanf("%d%d%d", &n, &k, &m);
st[0] = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d", &a);
if(st[top - 1] != a) st[top] = a, f[top++] = 1;
else {
if(f[top - 1] + 1 < k) f[top - 1] += 1;
else if(f[top - 1] + 1 == k) top--;
}
}
if(k >= n) {
for(int i = 1; i < top - 1; ++i) if(st[i] != st[i + 1]) flag = 1;
if(!flag) cout << 1LL * n * m % k << endl; else cout << 1LL * n * m << endl;
return 0;
}
for(int i = 1; i < top; ++i) ans += f[i];
if(m == 1 || ans == 0) {cout << ans << endl; return 0;}
ans = 1LL * m * ans;
int L = 1, R = top - 1;
while(L < R) {
if(st[L] != st[R]) {cout << ans << endl; return 0;}
if(f[L] + f[R] == k) {L++; R--;ans -= 1LL * (m - 1) * k; continue;}
if(f[L] + f[R] > k) ans -= 1LL * (m - 1) * k;
cout << ans << endl; return 0;
}
if(L > R) {cout << 0 << endl; return 0;}
if(L == R) {
LL x = 1LL * f[L] * m;
if(x % k == 0) {cout << 0 << endl;}
else {cout << ans - x / (LL)k * (LL)k << endl;}
}
return 0;
}

【Codeforces】879D. Teams Formation 思维+模拟的更多相关文章

  1. CodeForces 879D Teams Formation

    题意 将一个长度为\(n\)的数组重复\(m\)遍得到一个长度为\(n \times m\)的新序列,然后消掉新序列中连续\(k\)个相同的元素,不断重复这一过程,求最后剩下的序列的长度 分析 首先可 ...

  2. cf 443 D. Teams Formation](细节模拟题)

    cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...

  3. codeforces 887A Div. 64 思维 模拟

    A. Div. 64 time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  4. Codeforces 1087C Connect Three (思维+模拟)

    题意: 网格图选中三个格,让你选中一些格子把这三个格子连起来,使得选中的格子总数最小.最后输出方案 网格范围为1000 思路: 首先两点间连起来最少需要的格子为他们的曼哈顿距离 然后连接方案一定是曼哈 ...

  5. 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 ...

  6. CodeForces.158A Next Round (水模拟)

    CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...

  7. codeforces 879 D. Teams Formation(思维)

    题目链接:http://codeforces.com/contest/879/problem/D 题意:这题题意我反正是看了很久,可能是我的理解能力有点差,就是将一个数组倍增m倍然后将连续的相同的k个 ...

  8. Codeforces Round #706 (Div. 2)B. Max and Mex __ 思维, 模拟

    传送门 https://codeforces.com/contest/1496/problem/B 题目 Example input 5 4 1 0 1 3 4 3 1 0 1 4 3 0 0 1 4 ...

  9. Codeforces 758D:Ability To Convert(思维+模拟)

    http://codeforces.com/problemset/problem/758/D 题意:给出一个进制数n,还有一个数k表示在n进制下的值,求将这个数转为十进制最小可以是多少. 思路:模拟着 ...

随机推荐

  1. python--文本处理1

    1.字符和字符值之间的转换 内建函数:ord(),chr() >>> print ord("a") 97 >>> print chr(97) a ...

  2. Android View 布局流程(Layout)完全解析

    前言 上一篇文章,笔者详细讲述了View三大工作流程的第一个,Measure流程,如果对测量流程还不熟悉的读者可以参考一下上一篇文章.测量流程主要是对View树进行测量,获取每一个View的测量宽高, ...

  3. sqlalchemy如何实现时间列自动更新?

    目标:数据表的时间列在其他列内容更新的时候,自动更新时间列到更新的时间 方法:数据库表模型如下:server_default表示初始时间,onupdate表示更新的时间 class MonitorDa ...

  4. [Algorithms] Queue & Priority Queue

    In this lesson, you will learn how to create a queue in JavaScript. A queue is a first-in, first-out ...

  5. HDU 1253:胜利大逃亡(简单三维BFS)

    pid=1253">胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/ ...

  6. Triangle 三角形——找出三角形中从上至下和最小的路

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. Hdu-1565 方格取数(1) (状态压缩dp入门题

    方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  8. WPF03(样式)

    说起样式,大家第一反应肯定是css,好的,先上一段代码. 1 html{border:0;} 2 ul,form{margin:0; padding:0} 3 body,div,th,td,li,dd ...

  9. Django-Rest-Framework部分源码流程分析

    class TestView(APIView): ''' 调用这个函数的时候,会自动触发authentication_classes的运行,所以会先执行上边的类 ''' authentication_ ...

  10. Process Autocad by python

    一.处理AutoCad模块 -pyautocad 1.安装 pip install pyautocad 注:1.该操作会自动安装 comtypes模块,如果其他方式安装,请自行安装comtypes模块 ...