题意:

给定一长度为 n 的整数序列 $a$,将其复制m次,并接成一条链,每相邻K个相同的整数会消除,然后其他的整数继续结成一条链,直到不能消除为止,求问最终剩余多少个整数。

解法:

首先将长度为n的序列消干净,然后接下来的消除操作必然是在两个序列相交处进行消除,对于接在中间的序列,原消法等价于序列的左侧和右侧不停消除直到无法继续。

分类讨论:

1.序列左右消干净了,这样答案为0

2.序列消得剩下一种元素,统计该元素的总个数,如果K|sum,则为0,不然为sum%K个加上左右侧剩余元素。

3.序列剩下一段元素:

考虑计算消除了多少个:

(1)首先剩余的元素拼在一起 如果有%K!=0的相同元素接在一起 将会消成sum%K

(2)其次中间序列左右消除的个数。

这样就解决了,效率$O(n)$

const int N = ;

int n,K,m,tot,a0[N],a[N],b[N];

int main() {
cin >> n >> K >> m;
int cnt = n;
FOR(i,,n) scanf("%d", &a0[i]);
b[] = -;
FOR(i, , n)
{
if(a0[i] == b[tot]) a[tot]++;
else
{
if(tot)
{
cnt -= a[tot]-a[tot]%K; a[tot] %= K;
if(a[tot]==) tot--;
}
if(!tot || a0[i] != b[tot]) a[++tot] = , b[tot] = a0[i];
else a[tot]++;
}
}
cnt -= a[tot]-a[tot]%K;
a[tot] %= K;
if(a[tot]==) tot--;
if(!tot)
{
cout << << endl;
return ;
}
// FOR(i,1,tot) cout<<a[i]<<'-'<<b[i]<<endl;
int l=, r=tot;
while(l<r)
{
if(b[l]!=b[r] || (a[l]+a[r])%K) break;
l++,r--;
}
// debug(l);
// debug(r);
// debug(cnt);
if(l>r)
{
cout << << endl;
return ;
}
else if(l==r)
{
if(1ll*a[l]*m%K==) cout << << endl;
else cout << cnt-a[l]+1ll*a[l]*m%K << endl;
}
else
{
LL ans = 1ll*m*cnt,tmp = cnt;
FOR(i,l,r) tmp -= a[i];
ans -= 1ll*(m-)*tmp;
if(b[l]==b[r]) ans -= 1ll*(m-)*((a[r]+a[l]) - (a[r]+a[l])%K);
cout << ans << endl;
}
return ;
}

Teams Formation的更多相关文章

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

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

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

  3. 443 D. Teams Formation

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

  4. CodeForces 879D Teams Formation

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

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

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

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

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

  7. Codeforces Round #443 (Div. 2) 【A、B、C、D】

    Codeforces Round #443 (Div. 2) codeforces 879 A. Borya's Diagnosis[水题] #include<cstdio> #inclu ...

  8. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  9. 第十二届浙江省大学生程序设计大赛-Team Formation 分类: 比赛 2015-06-26 14:22 50人阅读 评论(0) 收藏

    Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...

随机推荐

  1. [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. visual studio 2010 c++ 打印 Hello world

    由于好奇心驱使温习下c高级简化语言语言(个人解释可能不太准确).下面用visual studio 2010 实现 HelloWord 打印 第一步:visual studio 2010 打开.文件-- ...

  3. WCP人员管理之添加人员

    1.用户管理页面 其中 : var url_formActionUser = "user/form.do";//增加.修改.查看URL 2. 通过 /form 路由, 转到 Use ...

  4. ZOJ 3502 Contest <状态压缩 概率 DP>

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3502 #include <iostream> #incl ...

  5. mysql批量插入测试数据

    一.建表语句 use test; create table student( Sno ) NOT NULL COMMENT '学号', Sname ) NOT NULL COMMENT '姓名', S ...

  6. python mmap使用记录

    1.写文件 with open('??', 'r+b') as f: with contextlib.closing(mmap.mmap(f.fileno(), size, flags=mmap.MA ...

  7. 【docker】学习笔记一:制作自己的centos6.9镜像

    前言: 最近开始研究docker,在这里做一个记录. 本来开始想用centos7系列做镜像,毕竟是最新版本的centos,但是centos7有一个严重的bug,就是正常启动的镜像不能使用systemc ...

  8. Java for LeetCode 082 Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  9. nodejs搭建简单的websocket服务端

    创建websocket服务端使用了nodejs-websocket ,首先要安装nodejs-websocket,在项目的目录下: npm install nodejs-websocket 1.搭建w ...

  10. SDUT OJ 之 连通分量个数 (dfs)

    数据结构实验:连通分量个数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  在无向图中,如果从顶点vi到顶点vj有路径,则称vi ...