本题为多组测试数据,第一行输入一个正整数,代表测试数据组数。
对于每组测试数据,第一行输入两个正整数,代表美食数量以及牛牛感到吃撑时的饱腹感。
第二行输入个数,依次代表每道美食所能共提供的饱腹感,每道美食能提供的饱腹感不会超过
.
对于每组测试数据,如果这些美食能够让牛牛吃撑,那么请在第一行输出牛牛最少吃几份能吃撑,在第二行输出对应的吃美食方案,若存在多种方案,则任意输出一种即可;如果这些美食不能让牛牛吃撑,那么只需要在一行输出即可。
2 4 10 1 2 9 5 1 10 9
2 1 3 -1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
int nums, satiety;
cin >> nums >> satiety;
//> 同时记住位置
vector<pair<int, int>> food(nums);
for (int i = 0; i < nums; ++i)
{
int a;
cin >> a;
food[i] = {a,i+1};
}
sort(food.begin(), food.end(),
[](pair<int,int> a, pair<int,int> b)
{return a.first > b.first;});
int sum = 0;
int index = 0;
//> 贪心
while (sum < satiety && index < nums)
{
sum += food[index++].first;
}
if (sum >= satiety)
{
cout << index << endl;
for (int i = 0; i < index; ++i)
{
cout << food[i].second << " ";
}
cout << endl;
}
else
cout << -1 << endl;
}
return 0;
}