XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
题目:Problem J. Terminal
Input file: standard input
Output file: standard input
Time limit: 2 seconds
Memory limit: 256 mebibytes
N programmers from M teams are waiting at the terminal of airport. There are two shuttles at the exit
of terminal, each shuttle can carry no more than K passengers.
Now employees of the airport service need to choose one of the shuttles for each programmer. Note that:
• programmers already formed a queue before assignment to the shuttles;
• each second next programmer in the queue goes to the shuttle he or she is assigned for;
• when all programmers, assigned to the shuttle, are in, shuttle immediately closes door and leaves
the terminal;
• no two programmers from the same team may be assigned to the different shuttles;
• each programmer must be assigned to one of shuttles.
Check if its possible to find such as assignment; if the answer is positive, find minimum sum of waiting
times for each programmer. Waiting time for a person is defined as time when shuttle with this person
left the terminal; it takes one second to programmer to leave the queue and enter the assigned shuttle.
At moment 0 the first programmer begins moving to his shuttle.
Input
First line of the input contains three positive integers N, M and K (M ≤ 2000, M ≤ N ≤ 105, K ≤ 105).
Second line contains description of the queue — N space-separated integers Ai — ids of team of each
programmer in order they are placed in the queue (1 ≤ Ai ≤ M).
Output
If it is impossible to assign programmers to she shuttles following the rules above, print -1. Otherwise
print one integer — minimum sum of waiting times for all programmers.
Examples
| standard input | standard input | 
| 7 3 5 2 2 1 1 1 3 1 | 39 | 
| 12 3 9 1 1 1 2 3 2 2 2 2 2 2 2 | 116 | 
| 2 1 2 1 1 | 4 | 
思路:
如果存在可行解,那么最后一个人一定会上车,不如直接选定上第二辆车,所以第二辆车是第n秒开的。
然后枚举上第一辆车的最后一个队的最后一个人是什么时候上车的。
怎么判断可行呢?01背包即可。
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int n,m,k;
LL ls[],rd[],cnt[];
LL ans=2e18;
bool dp[][];
bool cmp(int a,int b)
{
return ls[a]<ls[b];
}
int main(void)
{
//freopen("in.txt","r",stdin);
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=m;i++) rd[i]=i;
for(int i=,x;i<=n;i++)
scanf("%d",&x),ls[x]=i,cnt[x]++;
sort(rd+,rd+m+,cmp);
dp[][]=;
for(int i=,now=,pre=;i<=m;i++)
{
//printf("%d\n",rd[i]);
for(int j=;j<=k;j++)
if(dp[pre][j]&&j+cnt[rd[i]]<=k&&n-j-cnt[rd[i]]<=k)
ans=min(ans,1LL*(j+cnt[rd[i]])*ls[rd[i]]+1LL*(n-j-cnt[rd[i]])*n);
for(int j=;j<=k;j++)
{
dp[now][j]|=dp[pre][j];
if(j+cnt[rd[i]]<=k)
dp[now][j+cnt[rd[i]]]|=dp[pre][j];
dp[pre][j]=;
}
swap(now,pre);
}
if(ans==2e18) printf("-1\n");
else printf("%lld\n",ans);
return ;
}
XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal的更多相关文章
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
		题目: Problem F. Matrix GameInput file: standard inputOutput file: standard inputTime limit: 1 secondM ... 
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel
		题目:Problem L. Canonical duelInput file: standard inputOutput file: standard outputTime limit: 2 seco ... 
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative
		题目:Problem A. Arithmetic DerivativeInput file: standard inputOutput file: standard inputTime limit: ... 
- 【二分】【字符串哈希】【二分图最大匹配】【最大流】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem I. Minimum Prefix
		给你n个字符串,问你最小的长度的前缀,使得每个字符串任意循环滑动之后,这些前缀都两两不同. 二分答案mid之后,将每个字符串长度为mid的循环子串都哈希出来,相当于对每个字符串,找一个与其他字符串所选 ... 
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures
		题目:Problem D. Clones and TreasuresInput file: standard inputOutput file: standard outputTime limit: ... 
- 【二分图】【并查集】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel
		给你一个网格(n<=2000,m<=2000),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸 ... 
- 【动态规划】【滚动数组】【bitset】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
		有两辆车,容量都为K,有n(10w)个人被划分成m(2k)组,依次上车,每个人上车花一秒.每一组的人都要上同一辆车,一辆车的等待时间是其停留时间*其载的人数,问最小的两辆车的总等待时间. 是f(i,j ... 
- 【枚举】【最小表示法】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
		给你一个n*m的字符矩阵,将横向(或纵向)全部裂开,然后以任意顺序首尾相接,然后再从中间任意位置切开,问你能构成的字典序最大的字符串. 以横向切开为例,纵向类似. 将所有横排从大到小排序,枚举最后切开 ... 
- 【推导】【构造】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem E. Space Tourists
		给你n,K,问你要选出最少几个长度为2的K进制数,才能让所有的n位K进制数删除n-2个元素后,所剩余的长度为2的子序列至少有一个是你所选定的. 如果n>K,那么根据抽屉原理,对于所有n位K进制数 ... 
随机推荐
- MyBitis(iBitis)系列随笔之六:mybitis与spring集成
			目前Spring官方还没有出整合Mybatis的特性,但是mybitis比较给力,开发了一个mybatis-spring插件,达到与Spring的完美整合目的. 在与Spring集成前,一方面我们需要 ... 
- Ehcache缓存框架具体解释
			一.前言 ehcache是一个比較成熟的java缓存框架.它提供了用内存,磁盘文件存储.以及分布式存储方式等多种灵活的cache管理方案.ehcache最早从hibernate发展而来. 因为3.x的 ... 
- vue的递归组件以及三级菜单的制作
			js里面有递归算法,同时,我们也可以利用props来实现vue模板的递归调用,但是前提是组件拥有 name 属性 父组件:slotDemo.vue: <template> <div& ... 
- echarts x轴坐标文字显示不全
			在echarts中应用柱状图或者折线图时,当数据量过多的时候,X轴的坐标就会显示不全(如下图图一),在ECharts图表组件内部有一个机制,用于统计xAxis坐标刻度的个数和图表宽度,从而会自动调整刻 ... 
- 【已解决】Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory
			[已解决]Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 结论是: 当前有个bug: 默认是通过 hw.ramSize=1024 ... 
- Objective-C代码学习大纲(2)
			2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ... 
- css 更改input radio checkbox的样式
			html <label> <input type="checkbox" class="colored-blue"> <span c ... 
- OnePy--构建属于自己的量化回测框架
			本文主要记录我构建量化回测系统的学习历程. 被遗弃的项目:Chandlercjy/OnePy_Old 新更新中的项目:Chandlercjy/OnePy 目录 1. 那究竟应该学习哪种编程语言比较好呢 ... 
- Yii框架2.0的控制器
			控制器是继承[[yii\base\Controller]]类的对象,负责处理请求和生成响应. 具体来说,控制器从应用主体接管控制后会分析请求数据并传送到模型, 传送模型结果到视图,最后生成输出响应信息 ... 
- LRU算法的Python实现
			http://flychao88.iteye.com/blog/1977653文章中介绍了常见的几种缓存淘汰策略 LRU:least recently used,最近最少使用算法.其实就是按使用时间倒 ... 
