正文

排队买票2007-09-05 18:38:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/29144.html

分享到:

Buy tickets
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Problem description
There are N people who want to buy some tickets, and there are M places to serve them. These people are numbered from 1 to N , and person i will be served in Ti minites. If N > M, some people will have to wait. Now we want to rearrange the sequence of these people so that the sum of the waiting time of all the people will be minimized, and your task is to calculate the minimum waiting time.

Input
The first line is two integers N(1 <= N <= 1000) and M(1 <= M <= 100), which is the number of the people and the number of places to serve them. The following n lines is the time which the N people need to be served.

Output
Only one integer, the minimum waiting time.

Sample Input
5 3
3
4
2
8
4
Sample Output
5

//// 第一行输入 n m  ,n 表示需要买票的总人数,m 表示提供服务的窗口数目,接下来的 n 行每行一个整数表示第 i (1<=i<=n) 个人需要的服务时间,计算所有人总共的等待时间,思路“先给需要服务时间少的人提供服务”。。///水题 ,仅为充数 。。。

#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int wait[1002],n,m;
    cin>>n>>m;
    for(int i=0; i<n; i++)
        cin>>wait[i];
    if(m>=n)
        cout<<0<<endl;
    else{
        sort(&wait[0],&wait[n]);
        int times=0,i=m;
        do{
            times += wait[0]*(n-i);
            for(int j=0,t=wait[0]; j<m && i<n; j++){
                wait[j]=wait[j]-t;
                if(wait[j]==0)
                    wait[j]=wait[i++];
            }
            sort(&wait[0],&wait[m]);
        }while(i<n);
        cout<<times<<endl;
    }
    return 0; 
}

阅读(3267) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册