Teams Formation
题意:
给定一长度为 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的更多相关文章
- 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 443 D. Teams Formation](细节模拟题)
cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...
- 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\)遍得到一个长度为\(n \times m\)的新序列,然后消掉新序列中连续\(k\)个相同的元素,不断重复这一过程,求最后剩下的序列的长度 分析 首先可 ...
- 【Codeforces】879D. Teams Formation 思维+模拟
题意 给定$n$个数,重复拼接$m$次,相邻$k$个重复的可消除,问最后序列中有多少个数 首先可以发现当$k>=n$时,如果要使$n$个数可以被消除,那么$n$个数必须一样,否则$n$个数不能被 ...
- codeforces 879 D. Teams Formation(思维)
题目链接:http://codeforces.com/contest/879/problem/D 题意:这题题意我反正是看了很久,可能是我的理解能力有点差,就是将一个数组倍增m倍然后将连续的相同的k个 ...
- Codeforces Round #443 (Div. 2) 【A、B、C、D】
Codeforces Round #443 (Div. 2) codeforces 879 A. Borya's Diagnosis[水题] #include<cstdio> #inclu ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...
- 第十二届浙江省大学生程序设计大赛-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 ...
随机推荐
- freopen - C/C++文件输入输出利器
freopen以前经常使用,比较方便,可以当作模板,在中间替换为自己的代码即可使用. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h&g ...
- c# 根据枚举Value 获得名称
// 定义枚举类型enum sotype : int { book=1, pen=2, other=3 } // 输出名称 switch (Enum.GetName(typeof(sotype), 1 ...
- P3382 【模板】三分法
题目描述 如题,给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减.试求出x的值. 输入输出格式 输入格式: 第一行一次包含一个正整数N和两个实数l.r,含 ...
- 【BZOJ1835】[ZJOI2010]base 基站选址 线段树+DP
[BZOJ1835][ZJOI2010]base 基站选址 Description 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯 ...
- 安装配置Django
安装配置Django 以下是基于python3.5 pip install Django 把python环境目录Scripts配置到环境变量,主要在命令行中随时可以使用django-admin 验证 ...
- maven工作的过程
1 建立各个module之间的依赖关系 2 越底层的依赖的module先生成 3 下载远程库中的依赖 4 先生成本地被依赖的module 问题是,如何保证本次module和远程库中的包不重名?
- m*n matrix min rank square matrix
m*n matrix m*n=1000 f(A)=25 https://www.cs.princeton.edu/courses/archive/spring12/cos598C/svdchapter ...
- eclipse 安装tomcat
- SAP-财务会计
[转自 http://blog.itpub.net/195776/viewspace-1023913/] 一.FI组织架构1主数据1.1总帐科目1.2帐户组.1.3统驭科目1.4 总帐未清项管理2 凭 ...
- Bootstrap——组件
1.字体图标 <span class="glyphicon glyphicon-star" aria-hidden="true"></span ...