選機房(貪心模擬)

選機房

Time Limit: 1000ms
Memory Limit: 65535KB
64-bit integer IO format: %lld      Java class name: Main
Type:   
BNU程序設計大賽就要開始了,決賽地點暫時定在電子樓,因爲電子樓有很多各種大小的機房,目前估計參賽的隊伍總數爲n,但是學校可能沒有那麼大的機房容納所有隊伍,可能要將選手分配在幾個小機房中進行比賽,xyjian老大安排你去選機房,爲了讓比賽選手相對集中,要求選中機房的總數最少,另外在滿足這一前提的情況下儘可能選擇較大的機房。現在你得到了BNU所有機房的能容納隊伍數目的情況表,請你編程自動選擇機房。

Input

輸入文件包含多組數據。
文件第一行:一個正整數t<=20表示測試數據的組數。
接下來t行表示t組數據,每組數據按照以下格式:
第一行兩個正整數n<=100000和k<=1000以空格隔開,表示參賽隊的總數和可以選擇的機房總數k。
接下來k行每行一個正整數c<=20000。
第1行表示1號機房所能容納的隊伍總數,第2行表示2號機房能容納的隊伍數,以此類推,已知所有k個機房的大小均不相同,並且所有機房的總容量大於n。

Output

對每組數據輸出所要選擇的機房的編號,每個編號一行,從小到大輸出,每組數據後請輸出一個空行。

Sample Input

2
200 2
300
400
100 5
50
4
20
40
5

Sample Output

2

1
3
4

題目大意:

這道題說的是,首先給你n個人,然後k個機房,讓你給這些人來分配房間,使得所分配的房間能儘可能的大,然後還需要你使用盡可能少的房間數目。

解題思路:

這題一開始寫了兩個cmp,直接WA了,然後用stable_sort調了下,還是WA,就不裝逼了,用了一個b數組專門來記錄下標,然後從小到大sort了一次就A了。

代碼:

# include<cstdio>
# include<iostream>
# include<algorithm>
# include<cstring>
# include<string>
# include<cmath>
# include<queue>
# include<stack>
# include<set>
# include<map>

using namespace std;

# define inf 999999999
# define MAX 10000+4

struct node
{
    int id;
    int val;
}a[MAX];

int b[MAX];

int cmp ( const struct node & x,const struct node & y )
{
    return x.val > y.val;
}

int cmp2 ( const struct node & x,const struct node & y )
{
    return x.id < y.id;
}


int main(void)
{
    int t;cin>>t;
    while ( t-- )
    {
        int pos = 0;
        int n,k;
        cin>>n>>k;
        for ( int i = 0;i < k;i++ )
        {
            cin>>a[i].val;
            a[i].id = i+1;
        }
        stable_sort(a,a+k,cmp);
        int sum = 0;

        int j = 0;
        int i = 0;
        while ( sum <= n )
        {
            sum += a[i].val;
            b[j] = a[i].id;
            i++;
            j++;
        }
        sort(b,b+j);
        for ( i = 0;i < j;i++ )
            cout<<b[i]<<endl;

        if ( t!=0 )
            cout<<endl;

    }


	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章