Z is planning to visit the Tourist Kingdom for M days. This kingdom is full of lovely cities. During his stay Z would like to visit as many different cities as possible. He cannot visit more than one city on the same day.
Additionally,different cities may require him to stay for a different number of days.For each i,city i counts as visited if Z spends at least d[i] days in the city.
Help Z find the maximal number of cities he can visit during his vacation.
First a integer T,the number of test cases.For each test case,the first line is M and N,the total days Z has and the number of cities.(M,N≤50)
Then N integers,meaning the int[] d.
The maximal number of cities Z can visit.
3 5 3 2 2 2 5 3 5 6 1 6 5 1 1 1 1 1
2 1 5
题目概述:简单地说就是用最少的天数游览最多的城市,需要注意的是 并不是到过每个城市就算游览过,而是 需要在不同的城市停留不同的天数,满足条件时才算游览过。
解题思路:本体解题方法比较简单,用 贪心的思想 就能轻易得出答案。
源代码:
#include<iostream>
#include<set>
using namespace std;
int main()
{
multiset<int>Set; //定义一个set
int T,M,N;
cin>>T;
while(T--)
{
int max=0,temp;
cin>>M>>N;
for(int i=0;i<N;++i) //输入需要在每个城市停留的天数
{
cin>>temp;
Set.insert(temp);
}
multiset<int>::iterator pos; //迭代器
for(pos=Set.begin();pos!=Set.end();++pos)
{
if(M-*pos>=0){++max;M-=*pos;} //贪心法求得答案
else break;
}
Set.clear(); //清空set,否则会影响下一次的结果
cout<<max<<endl;
}
return 0;
}