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. 韦大仙--Katalon---一款好用的selenium自动化测试插件

    selenium框架是目前使用较广泛的开源自动化框架,一款好的.基于界面的录制工具对于初学者来说可以快速入门:对于老手来说可以提高开发自动化脚本的效率.我们知道Selenium IDE是一款使用较多的 ...

  2. 新的征程 in ZJU

    争取考上了心仪的学校 并进入了心仪的实验室 但是对我来说,未来将是更多的挑战 首先我觉得我学习能力还是不足,无法做到一天的高效率学习 实验室的方向是可视化,我觉得这个是个非常复杂的方向 数学,pyth ...

  3. (C#)设计模式之状态模式

    1.状态模式 当一个对象的内在状态改变时允许改变其行为,这个对象看起像是改变了其类. *状态模式主要解决的是当控制一个对象的状态转换的条件表达式过于复杂时,可以将状态的判断逻辑转移到表示不同状态的一系 ...

  4. JS原型链与继承别再被问倒了

    原文:详解JS原型链与继承 摘自JavaScript高级程序设计: 继承是OO语言中的一个最为人津津乐道的概念.许多OO语言都支持两种继承方式: 接口继承 和 实现继承 .接口继承只继承方法签名,而实 ...

  5. lintcode 二分查找

    题目:二分查找 描述:给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. c ...

  6. DP动态规划练习

    先来看一下经典的背包问题吧 http://www.cnblogs.com/Kalix/p/7617856.html  01背包问题 https://www.cnblogs.com/Kalix/p/76 ...

  7. AttributeError: 'TimeLimit' object has no attribute 'monitor'

    原报错代码部分: env.monitor.start(monitor_path, resume=True, video_callable=lambda count: count % record_vi ...

  8. 简单构建基于RDF和SPARQL的KBQA(知识图谱问答系统)

    本文主要通过python实例讲解基于RDF和SPARQL的KBQA系统的构建.该项目可在python2和python3上运行通过. 注:KBQA即是我们通常所说的基于知识图谱的问答系统.这里简单构建的 ...

  9. [C++] Fucntions

    Statements A break statements terminate the nearest wile, do while, for or switch statement. A break ...

  10. Fluent Python: Mutable Types as Parameter Defaults: Bad Idea

    在Fluent Python一书第八章有一个示例,未看书以先很难理解这个示例运行的结果,我们先看结果,然后再分析问题原因: 定义了如下Bus类: class Bus: def __init__(sel ...