SPOJ - ACMCEG2C Pick the Candies【水题】

题面:

Many children went to a sweet shop. There were n candy varieties and each variety is kept in a separate bowl. The sweetness of each variety is written on the bowl. All the children wanted the candy with highest sweetness value. As there are only limited candies in each variety, the shop keeper makes a rule. According to the rule, the shopkeeper will show selectively chosen k varieties to every children. The children can pick any one of those varieties and move away. To make it easy for him, the shop keeper shows the

varieties 1,2,…,k to children1,

varieties 2,3,…,k+1 to children2,

varieties 3,4,…,k+2 to children3 and so on…

All the children are good at math. Find what variety each child will choose.
Input Specification:
The first line contains an integer t, the number of test cases. For each test case the input consists of two lines. The first line contains two integers n(number of candy varieties) and k. The next line contains n number of integers, the sweetness values of all the candy varieties.

Output Specification:

For each test case, print the candy varieties chosen by the children.

Input Constraints:
1<=t<=1000
1<=n<=10000
1<=k<=n
0<=Sweetness value<=10000

Sample Input:

3
5 3
1 2 3 4 5
4 2
7 1 6 8
9 5
7 14 3 0 2 2 2 2 2

Sample Output:

3 4 5
7 6 8
14 14 3 2 2
Hint: use deque

题意:

T组数据 ,每组数据n种糖果和k个小孩,每个小孩只吃规则中最甜的。
规则:
第一个小孩吃 1到k中最甜的
第二个小孩吃 2到k+1中最甜的
第三个小孩吃 3到k+2中最甜的

思路:

双端队列+模拟操作,跟滑动窗口类似,每次都把前面的弹出。
扩展函数:*max_element()直接找区间最大值。
AC代码:

#include <bits/stdc++.h>
using namespace std;
signed main()
{
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t, k, i, n;
  cin >> n;
  while (n--)
  {
    cin >> t >> k;
    deque<int> q;
    for (i = 0; i < t; i++)
    {
      int r;
      cin >> r;
      q.push_back(r);
    }
    int a = t;
    for (i = 0; i < t - k + 1; i++)
    {
      if (q.empty())
        break;
      else
      {
        cout << *max_element(q.begin(), q.begin() + k) << " ";
        q.pop_front();
      }
    }
    cout << endl;
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值