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

题意:

给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证消除的顺序对答案不会造成影响)

\(n <= 10^{5} ,m <= 10^{9} ,k <= 10^{9},a_i <= 10^{5}\)

思路:

直接上题解好了

首先要用栈预处理序列本身,然后考虑处理后的序列

比较头和尾是否能够消除,细节需要处理好。

#include<bits/stdc++.h>
#define P pair<int,int>
#define LL long long using namespace std;
const int N = 1e5 + 10;
P st[N];
int main(){ int x,n,k,m;
while(cin>>n>>k>>m){
int top = 0;
for(int i = 1;i <= n;i++){
scanf("%d",&x);
if(top && x == st[top].first) st[top].second++;
else st[++top].first = x,st[top].second = 1;
if(st[top].second == k) top--;
}
if(top == 1){
cout<<1LL * st[1].second * m % k<<endl;
continue;
}
int res = 0;
for(int i = 1;i <= top;i++) res += st[i].second;
if(m == 1) {
cout<<res<<endl;
continue;
}
int p = 0;
for(int i = 1;i <= top - i + 1;i++){
if(st[i].first != st[top - i + 1].first || st[i].second + st[top - i + 1].second != k) break;
p = i;
} if(p >= top - top / 2) { ///超过一半,全消除的情况
if(m % 2 == 0) cout<<0<<endl;
else cout<<res<<endl;
}else{
if(top % 2 == 0 || p != top / 2) {
LL dec = p * k;
if(st[p+1].first == st[top - p].first) dec += st[p+1].second + st[top - p].second - (st[p+1].second + st[top - p].second)%k;
cout<<1LL * m * res - (m - 1) * dec<<endl;
}
else if(p == top / 2){
if(1LL * m * st[p + 1].second % k == 0) cout<<0<<endl;
else {
res = 0;
for(int i = 1;i <= p;i++) res += st[i].second + st[top - i + 1].second;
cout<<res + 1LL * m * st[p + 1].second % k <<endl;
}
}
}
}
return 0;
}

cf 443 D. Teams Formation](细节模拟题)的更多相关文章

  1. 443 D. Teams Formation

    http://codeforces.com/contest/879/problem/D This time the Berland Team Olympiad in Informatics is he ...

  2. 【Codeforces】879D. Teams Formation 思维+模拟

    题意 给定$n$个数,重复拼接$m$次,相邻$k$个重复的可消除,问最后序列中有多少个数 首先可以发现当$k>=n$时,如果要使$n$个数可以被消除,那么$n$个数必须一样,否则$n$个数不能被 ...

  3. CF 352 D 罗宾汉发钱 模拟题+贪心

    D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

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

  5. CF 420B Online Meeting 模拟题

    只是贴代码,这种模拟题一定要好好纪念下 TAT #include <cstdio> #include <cstring> #include <algorithm> ...

  6. sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)

    The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里 ...

  7. csu 1312 榜单(模拟题)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1312 1312: 榜单 Time Limit: 1 Sec  Memory Limit: 128 ...

  8. NOIP模拟题汇总(加厚版)

    \(NOIP\)模拟题汇总(加厚版) T1 string 描述 有一个仅由 '0' 和 '1' 组成的字符串 \(A\),可以对其执行下列两个操作: 删除 \(A\)中的第一个字符: 若 \(A\)中 ...

  9. Capture the Flag ZOJ - 3879(模拟题)

    In computer security, Capture the Flag (CTF) is a computer security competition. CTF contests are us ...

随机推荐

  1. android 学习十四 探索安全性和权限

    1.部署安全性:应用程序必须使用数字证书才能安装到设备上. 2.执行期间的安全性:    2.1 使用独立进程    2.2 使用固定唯一用户ID    2.3  申明性权限模型   3数字证书   ...

  2. Eclipse 常用快捷键 个性设置(Mac)

    推荐编程使用Mac 要是非要一个原因 那就是Apple工程师用Mac Google工程师也用Mac 1. 常用快捷键 Mac自带 Command + ←  跳到当前文本行头 Command + →  ...

  3. Unity操作小技巧

    1.操作类 1)F:选择物体后聚焦 2)V:选择物体的顶点,顶点吸附 3)Ctrl:摁住后拖动物体,可以按照系统设置的步长进行移动(Edit -> Snap setting) 4)Q W E R ...

  4. python中出现ascii编码问题的解决办法

    一劳永逸,一次性全盘解决的办法 环境变量中去设置 以centos 7为例: vim /etc/profile export PYTHONIOENCODING=utf-8 source /etc/pro ...

  5. 传入中文参数-->服务器_转码的方法

    如果要传入 中文参数到 服务器 使用lr_convert_string_encoding()                            LR_ENC_SYSTEM_LOCALE ,  转为 ...

  6. Maven编译Java项目

    Spring在线参考文档: http://spring.io/guides/gs/maven/ 下载安装 Downloadand unzip the source repository for thi ...

  7. ajax 和 mock 数据

    ajax ajax是一种技术方案,但并不是一种新技术.它依赖的是现有的CSS/HTML/Javascript,而其中最核心的依赖是浏览器提供的XMLHttpRequest对象,是这个对象使得浏览器可以 ...

  8. 单机部署Fastfds+nginx

    一.环境 centos6.8 x64  IP:192.168.134.128 所需软件包: libfastcommon-1.0.7.zip,FastDFS_v5.05.tar.gz,nginx-1.7 ...

  9. Java中String类

    String类 一.String类的用途 对字符串的存储与操作,即包含多个字符的字符数据,这个String类提供了数值不可改变的字符串. String类为什么不可改变的字符串,因为String类是fa ...

  10. [贪心经典算法]Kruskal算法

    Kruskal算法的高效实现需要一种称作并查集的结构.我们在这里不介绍并查集,只介绍Kruskal算法的基本思想和证明,实现留在以后讨论. Kruskal算法的过程: (1) 将全部边按照权值由小到大 ...