cf 443 D. Teams Formation](细节模拟题)
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](细节模拟题)的更多相关文章
- 443 D. Teams Formation
http://codeforces.com/contest/879/problem/D This time the Berland Team Olympiad in Informatics is he ...
- 【Codeforces】879D. Teams Formation 思维+模拟
题意 给定$n$个数,重复拼接$m$次,相邻$k$个重复的可消除,问最后序列中有多少个数 首先可以发现当$k>=n$时,如果要使$n$个数可以被消除,那么$n$个数必须一样,否则$n$个数不能被 ...
- CF 352 D 罗宾汉发钱 模拟题+贪心
D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 420B Online Meeting 模拟题
只是贴代码,这种模拟题一定要好好纪念下 TAT #include <cstdio> #include <cstring> #include <algorithm> ...
- sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)
The Android University ACM Team Selection Contest Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里 ...
- csu 1312 榜单(模拟题)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1312 1312: 榜单 Time Limit: 1 Sec Memory Limit: 128 ...
- NOIP模拟题汇总(加厚版)
\(NOIP\)模拟题汇总(加厚版) T1 string 描述 有一个仅由 '0' 和 '1' 组成的字符串 \(A\),可以对其执行下列两个操作: 删除 \(A\)中的第一个字符: 若 \(A\)中 ...
- Capture the Flag ZOJ - 3879(模拟题)
In computer security, Capture the Flag (CTF) is a computer security competition. CTF contests are us ...
随机推荐
- windown reids
记录Windown安装Redis和php_redis扩展 和Linux系统不同windown中不需要编译安装:只需要下对版本拖拽过去即可: 首先安装redis服务: 可以百度下一个,只要注意系统版本即 ...
- tomcat createSecureRandom 花费了将近10分钟
http://www.th7.cn/Program/java/201603/776312.shtml 启动tomcat很慢,检查后发现:[localhost-startStop-1] org.apac ...
- WeTest功能优化第3期:业内首创,有声音的云真机
第3期功能优化目录 [云真机远程调试]音频同步传输实现测试有声 [兼容性测试报告]新增视频助力动态定位问题 [云真机远程调试]菜单栏优化助力机型选择 本期介绍的新功能,秉承创造用户需求的理念,在云真机 ...
- python一标准异常总结大全(非常全)
Python标准异常总结 AssertionError 断言语句(assert)失败 AttributeError 尝试访问未知的对象属性 EOFError 用户输入文件末尾标志EOF(Ctrl+d) ...
- phpanalysis提取关键字
最近在开发一个文章模块功能,设计那边提出要给文章生成对应标签,用于文章关联推送,这里和大家分享一下实现过程: 这里需要用到PHPAnalysis,下载链接如下 链接:https://pan.baidu ...
- hello word!------为什么开通博客以及自我介绍
来北京已经一年半的日子了,已经完全成功熟练的成为了另一个我,没有了半年前刚来时的那种焦虑.急躁和格格不入. 回想起来那段时间,大概是我人生中非常重要的时期了,去年那个夏天,只身一人背上行囊踏上了北上的 ...
- ORACLE高级部分内容
1.pl/sql基本语句 DECLARE BEGIN END; / 循环语句 DECLARE I NUMBER(2):=1; BEGIN WHILE I<100 LOOP I:=I+1; EN ...
- [SHELL]结构化命令之条件语句
1.if-then语句 #!/bin/bash username="root" if grep $username /etc/passwd then echo "the ...
- java常见的异常类型
Exception分为两类:非运行是异常和运行时异常. java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常.A:NullPointerExcepti ...
- 软件工程 作业part1
自我介绍 老师您好,我叫宋雨,本科在长春理工大学,专业是计算机科学与技术. 1.回想一下你曾经对计算机专业的畅想:当初你是如何做出选择计算机专业的决定?你认为过去接触的课程是否符合你对计算机专业的期待 ...