题目: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的更多相关文章

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

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

  3. 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: ...

  4. 【二分】【字符串哈希】【二分图最大匹配】【最大流】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的循环子串都哈希出来,相当于对每个字符串,找一个与其他字符串所选 ...

  5. 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: ...

  6. 【二分图】【并查集】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),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸 ...

  7. 【动态规划】【滚动数组】【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 ...

  8. 【枚举】【最小表示法】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game

    给你一个n*m的字符矩阵,将横向(或纵向)全部裂开,然后以任意顺序首尾相接,然后再从中间任意位置切开,问你能构成的字典序最大的字符串. 以横向切开为例,纵向类似. 将所有横排从大到小排序,枚举最后切开 ...

  9. 【推导】【构造】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进制数 ...

随机推荐

  1. 怎么隐藏MathType标尺

    因为MathType公式编辑能力非常的好用,所以非常的受大家的欢迎.MathType用现有的模板可以直接输入输出各种公式,而且MathType中有着各式各样的数学符号满足了大家日常公式的需求,为大家的 ...

  2. hdu 2918(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2918 思路:这道题与前面几道类似,可以说是被秒杀了!!!构造启发式函数h()=(cnt+3)/4(cn ...

  3. 如何使用 awk 复合表达式

    导读 一直以来在查对条件是否匹配时,我们使用的都是简单的表达式.那如果你想用超过一个表达式来查对特定的条件呢?本文中,我们将看看如何在过滤文本和字符串时,结合多个表达式,即复合表达式,用以查对条件. ...

  4. HDU_5538_House Building

    House Building Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  5. HDU3231 Box Relations——三维拓扑排序

    HDU3231 Box Relations 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3231 题目意思:在一个三维空间上有一些棱和坐标轴平行的立方 ...

  6. Logstash之时区问题的建议和修改---filter---and duplicate resolution.

    2. logstash es duplicate https://logstash.jira.com/browse/LOGSTASH-1875 https://logstash.jira.com/br ...

  7. Python 3.5 中的异步HTTP请求写法

    Python 3.5 增加了对async def and await的支持,同样的异步代码看起来干净了很多,也更易读. import aiohttp import asyncio async def ...

  8. python中yield使用

    16.yield使用   列表推导与生成器表达式   当我们创建了一个列表的时候,就创建了一个可以迭代的对象: >>> squares=[n*n for n in range(3)] ...

  9. Android设置透明状态栏和透明导航栏

    Android透明状态栏只有在4.4之后有. 在代码中加入下面几行代码即可实现

  10. golang: multiple http.writeHeader calls

    背景: golang的http服务,读取文件,提供给client下载时候. 出现 multiple http.writeHeader calls 错误. func DownloadFile(w htt ...