题目: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. shell脚本学习总结04--终端信息的获取和设置

    tput tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作.通过使用 tput,您可以更改几项终端功能,如移动或更改光标.更改文本属性,以及清除终端屏幕的特定区域. 文本属 ...

  2. 了解 IMyInterface.Stub

    Service中的IBinder 还记得我们在MyService中利用new IMyInterface.Stub()向上转型成了IBinder然后在onBind方法中返回的.那我们就看看IMyInte ...

  3. 我使用过的Linux命令之sftp - 安全文件传输命令行工具

    用途说明 sftp命令可以通过ssh来上传和下载文件,是常用的文件传输工具,它的使用方式与ftp类似,但它使用ssh作为底层传输协议,所以安全性比ftp要好得多. 常用方式 格式:sftp <h ...

  4. 【BZOJ3120】Line 矩阵乘法

    [BZOJ3120]Line Description Wayne喜欢排队……不对,是Wayne所在学校的校长喜欢看大家排队,尤其是在操场上站方阵.某日课间操时,校长童心大发想了一个极具观赏性的列队方案 ...

  5. LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  6. vue 阻止表单默认的提交事件

    form <form autocomplete="off" @submit.prevent="onSubmit"> <input type=& ...

  7. jdk1.7访问https报javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure问题解决

    本地jdk版本java version "1.8.0_31",代码中已对https做了相应处理:信任所有来源证书,运行正常:上包到服务器(服务器jdk版本java version ...

  8. 在虚拟机中的搭建Web服务器(CentOS)

    1.制作本地yum源 相关可查看:http://www.cnblogs.com/xiaomingzaixian/p/8424290.html 2.安装JDK 上传上传jdk-7u45-linux-x6 ...

  9. SSH整合中,使用父action重构子类action类.(在父类中获取子类中的泛型对象)

    import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import com.opensymphony.x ...

  10. 确定比赛名次---hdu1285(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 拓扑序就是求一个序列 数 a 出现在数 b 前面,最终输出满足条件的序列即可: 过程就是每次选取 ...