443 D. Teams Formation
http://codeforces.com/contest/879/problem/D
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city ai.
Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a1, a2, ..., an repeated m times).
After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no kneighboring participants from the same city.
Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected.
The first line contains three integers n, k and m (1 ≤ n ≤ 105, 2 ≤ k ≤ 109, 1 ≤ m ≤ 109).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105), where ai is the number of city, person from which must take seat i in the bus.
Output the number of remaining participants in the line.
4 2 5
1 2 3 1
12
1 9 10
1
1
3 2 10
1 2 1
0
In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.
题意
将一个长度为n的数组重复m遍得到一个长度为n×m的新序列,然后消掉新序列中连续k个相同的元素,不断重复这一过程,求最后剩下的序列的长度
先消内部的,再处理交界处的
#include <bits/stdc++.h>
#define ll long long
using namespace std; int n, k, m;
int s[];
int a[];
bool flag = true;
int cnt[]; int main() {
// freopen("trans.in","r",stdin);
// freopen("trans.out","w",stdout);
ios::sync_with_stdio(false);
cin >> n >> k >> m;
for (int i = ; i <= n; i++)
cin >> a[i];
for (int i = ; i <= n; i++) //判断是否全相等
if (a[i] != a[i - ]) {
flag = false;
break;
}
if (flag) {
cout << (ll)n * m % k;
return ;
}
int top = ;
for (int i = ; i <= n; i++) {
s[++top] = a[i];
if (s[top] == s[top - ])
cnt[top] = cnt[top - ] + ;
else
cnt[top] = ;
if (cnt[top] == k)//将每相同k段扔掉,并调整top位置
top -= k;
}
int L = , R = top;
int t = ;
while (s[L] == s[R] && L < R) {//处理边界,开l,r两头走
int l = L, r = R;
int sum = ;
while (s[L] == s[l] && l < r && sum < k)
sum++, l++;
while (s[L] == s[r] && l < r && sum < k)
sum++, r--;
if (sum == k)
L = l, R = r, t += k;
else
break;//不满足k段就没必要继续了,直接跳出
}
flag = true;//跟上面相同操作
for (int i = L + ; i <= R; i++)
if (s[i] != s[i - ]) {
flag = false;
break;
}
if (flag) {
ll mid = (ll)(R - L + ) * m % k;//注意范围,爆int
if (mid)
cout << mid + t;
else
cout << ;
} else
cout << (ll)(R - L + ) * m + t; return ;
}
443 D. Teams Formation的更多相关文章
- cf 443 D. Teams Formation](细节模拟题)
cf 443 D. Teams Formation(细节模拟题) 题意: 给出一个长为\(n\)的序列,重复\(m\)次形成一个新的序列,动态消除所有k个连续相同的数字,问最后会剩下多少个数(题目保证 ...
- 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 ...
- CodeForces 879D Teams Formation
题意 将一个长度为\(n\)的数组重复\(m\)遍得到一个长度为\(n \times m\)的新序列,然后消掉新序列中连续\(k\)个相同的元素,不断重复这一过程,求最后剩下的序列的长度 分析 首先可 ...
- Teams Formation
题意: 给定一长度为 n 的整数序列 $a$,将其复制m次,并接成一条链,每相邻K个相同的整数会消除,然后其他的整数继续结成一条链,直到不能消除为止,求问最终剩余多少个整数. 解法: 首先将长度为n的 ...
- 【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 ...
随机推荐
- jedis、jedisPool、jedisCluster的使用方法
jedis 连接redis(单机): 使用jedis如何操作redis,但是其实方法是跟redis的操作大部分是相对应的. 所有的redis命令都对应jedis的一个方法 1.在macen工程 ...
- thinkphp3.2 success方法和redirect方法
$this->redirect('showlist',array(),3,'添加成功'); $this->success('添加成功',U('showlist'),3);
- drupal7 formAPI给元素加css样式
比如,我定义了一个表单元素,名字叫做包库开始日期, $form['starttime_baoyue']=array( '#type'=>'textfield', '#title'=>t(' ...
- Android Service不能再详细的教程
这篇包含了: Service后台服务.前台服务.IntentService.跨进程服务.无障碍服务.系统服务 几乎所有Android Service相关的东西. 前言 作为四大组件之一的Service ...
- android设计的布局在阿拉伯语下界面错乱的解决方法
(1)正在AndroidManifest.xml声明文件的application元素中,增加” android:supportsRtl=true” (2)建] androidの设计的布局在阿拉伯语下界 ...
- linux 获取命令或配置文件的帮助信息 man、whatis、apropos、--help
man /usr/bin/man man [命令或配置文件]获取帮助信息 man ls /-lman date/-d man services //不需要添加绝对路径/etc/services NAM ...
- window 命令行
清屏 cls 启动服务 net start 服务名(nexus.mysql) 关闭服务 net stop 服务名(nexus.mysql) 删除服务 sc delete 服务名 如果服务名有空格,加引 ...
- Django html标签make_safe
from django.utils.safestring import mark_safe a = mark_safe("<a href='#'>test</a>&q ...
- Charles基础
一.Charles 监控其他设备连接方式 1.XP系统:控制面板——>Internet选项——>连接(tab)——>局域网(LAN)设置——>局域网设置——>代理服务器, ...
- Selenium clear()方法无法清掉数据
问题描述 clear()方法执行过后, 数据还是在. 根本原因 存在镜像节点. 操作clear()清掉数据后, 镜像节点的数据还在, 就会再补充回去. 解决办法 添加下面代码就可以连同镜像的数据一起去 ...