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 ...
随机推荐
- springMVC定时任务总是执行两次
情况: springmvc的定时任务在本机上测试的时候没有问题,但是放到测试服务器上的时候总是执行两次: 探索:(网上搜索) 一.spring注入的时候实例化了多次,说是spring-servlet. ...
- drupal7图片样式无法生成与显示
正常设置了图像的样式,并且为内同类型正确设置了显示的样式.但是上传图片后,却并没有在对应的文件夹下生成对应的缩略图.自然没有显示. 网上查询了一下,需要为nginx添加两个设置: location @ ...
- html基础-a标签-img标签-绝对/相对路径(3)
美好的星期六,今天多写一点,争取早点写js这个有点小无聊. 一.先来讲点网页之间的跳转 (1).<a href=""></a> href="这里 ...
- react 使用map 的时候提示 没有返回值
因为map 的函数体里 用了if判断,在if块之外return 一个值就可以了 <div className="service-entry"> {!!services ...
- centos安装redis,并设置开机自动启动项
安装Redis 1.下载.解压.编译.安装 下载.解压 https://redis.io/download 官网下载redis的*.tar.gz安装包.版本可根据自己需要下载. tar -zxvf r ...
- LeetCode题解Transpose Matrix
1.题目描述 2.题目描述 直接申请内存,转置即可. 3.代码 vector<vector<int>> transpose(vector<vector<int> ...
- 如何监视和更新 Azure 中的 Linux 虚拟机
为确保 Azure 中的虚拟机 (VM) 正常运行,可以查看启动诊断.性能指标,并管理程序包更新. 本教程介绍如何执行下列操作: 在 VM 上启用启动诊断 查看启动诊断 在 VM 上启用诊断扩展 基于 ...
- docker的网络基础配置
一.端口映射实现访问容器 当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-P或-p参数来指定端口映射.当使用-P标记时,Docker会随机映射一个49000~49900的端口至容器内部开放 ...
- iOS7中UIView的animateKeyframesWithDuration方法讲解
iOS7中UIView的animateKeyframesWithDuration方法讲解 在iOS7中,给UIView添加了一个方法用来直接使用关键帧动画而不用借助CoreAnimation来实现,那 ...
- 全自动LTI部署OS
全自动LTI部署OS:零.通过ADK制作WinPE(需包含有imagex.exe工具,用来捕获映像)一.使用WinPE中的imagex捕获映像(install.wim)二.使用MDT制作启动映像(bo ...