标签:desc span ++ signed pass 题目 std typedef 一个人
题目: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背包即可。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define MP make_pair 6 #define PB push_back 7 typedef long long LL; 8 typedef pair<int,int> PII; 9 const double eps=1e-8; 10 const double pi=acos(-1.0); 11 const int K=1e6+7; 12 const int mod=1e9+7; 13 14 int n,m,k; 15 LL ls[3000],rd[3000],cnt[3000]; 16 LL ans=2e18; 17 bool dp[2][101005]; 18 bool cmp(int a,int b) 19 { 20 return ls[a]<ls[b]; 21 } 22 int main(void) 23 { 24 //freopen("in.txt","r",stdin); 25 scanf("%d%d%d",&n,&m,&k); 26 for(int i=1;i<=m;i++) rd[i]=i; 27 for(int i=1,x;i<=n;i++) 28 scanf("%d",&x),ls[x]=i,cnt[x]++; 29 sort(rd+1,rd+m+1,cmp); 30 dp[0][0]=1; 31 for(int i=1,now=1,pre=0;i<=m;i++) 32 { 33 //printf("%d\n",rd[i]); 34 for(int j=0;j<=k;j++) 35 if(dp[pre][j]&&j+cnt[rd[i]]<=k&&n-j-cnt[rd[i]]<=k) 36 ans=min(ans,1LL*(j+cnt[rd[i]])*ls[rd[i]]+1LL*(n-j-cnt[rd[i]])*n); 37 for(int j=0;j<=k;j++) 38 { 39 dp[now][j]|=dp[pre][j]; 40 if(j+cnt[rd[i]]<=k) 41 dp[now][j+cnt[rd[i]]]|=dp[pre][j]; 42 dp[pre][j]=0; 43 } 44 swap(now,pre); 45 } 46 if(ans==2e18) printf("-1\n"); 47 else printf("%lld\n",ans); 48 return 0; 49 }
标签:desc span ++ signed pass 题目 std typedef 一个人
原文地址:http://www.cnblogs.com/weeping/p/7292003.html