题目: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. [转]Loadrunner随机生成15位数字串

    Loadrunner随机生成15位数字串 PS:http://www.51testing.com/html/43/6343-19789.html 今天看到一个网友的问题,是想生成一个15位的数字串来进 ...

  2. 学习《深入理解C#》—— 委托的构成、合并与删除和总结 (第二章1.1---1.4)

    目录 简单委托的构成 合并和删除委托 委托总结 简单委托的构成 委托四部曲: 声明委托类型. 必须有一个方法包含了要执行的方法. 必须创建一个委托实例. 必须调用委托(invoke)实例 ① 声明委托 ...

  3. [转]C# 超高速高性能写日志 代码开源

      1.需求 需求很简单,就是在C#开发中高速写日志.比如在高并发,高流量的地方需要写日志.我们知道程序在操作磁盘时是比较耗时的,所以我们把日志写到磁盘上会有一定的时间耗在上面,这些并不是我们想看到的 ...

  4. C# 笔记 Func<TResult> 委托、Action<T> 委托

    https://blog.csdn.net/wanglui1990/article/details/79303894 Func<ΤResult> 委托:代理(delegate)一个返回类型 ...

  5. 用Java实现自己的ArrayList

    利用自己对ArrayList的理解,重写了Java的ArrayList工具类,旨在理解源码的精髓: public class MyArrayList<T> { //成员变量 private ...

  6. 1:TwoSum(如果两个和为某个数,找出这俩数的位置)

    package leetcode; import java.util.HashMap; import java.util.Map; /** * @author mercy *Example: *Giv ...

  7. Android中的 style 和 theme

    通过设置 view 控件的属性,达到设置android UI的目的,如果某些 属性值复用率很高,可以考虑将属性单独声明在 style中,这样就可以达到复用的效果. 一.style Style 概念:A ...

  8. 记录一次gitlab->github企业版的迁移

    cd到你想要存放新的工程的文件夹内, 1.使用git clone --mirror命令制作旧git的镜像 $ git clone --mirror git@git.aaaa.com:mario/my- ...

  9. 并发编程 - IO模型 - 1.io模型/2.阻塞io/3.非阻塞io/4.多路复用io

    1.io模型提交任务得方式: 同步:提交完任务,等结果,执行下一个任务 异步:提交完,接着执行,异步 + 回调 异步不等结果,提交完任务,任务执行完后,会自动触发回调函数同步不等于阻塞: 阻塞:遇到i ...

  10. ubuntu 打开 gbk编码的txt乱码

    iconv -f gbk -t utf8 filename.txt > filename.txt.utf8