Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)——B - Coloring

225 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第26天,点击查看活动详情

Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)——B - Coloring

Problem - B - Codeforces

Cirno_9baka has a paper tape with nn cells in a row on it. As he thinks that the blank paper tape is too dull, he wants to paint these cells with mm kinds of colors. For some aesthetic reasons, he thinks that the ii-th color must be used exactly aiai times, and for every kk consecutive cells, their colors have to be distinct.

Help Cirno_9baka to figure out if there is such a way to paint the cells.

Input

The first line contains a single integer tt (1≤t≤100001≤t≤10000) — the number of test cases. The description of test cases follows.

The first line of each test case contains three integers nn, mm, kk (1≤k≤n≤1091≤k≤n≤109, 1≤m≤1051≤m≤105, m≤nm≤n). Here nn denotes the number of cells, mm denotes the number of colors, and kk means that for every kk consecutive cells, their colors have to be distinct.

The second line of each test case contains mm integers a1,a2,⋯,ama1,a2,⋯,am (1≤ai≤n1≤ai≤n) — the numbers of times that colors have to be used. It's guaranteed that a1+a2+…+am=na1+a2+…+am=n.

It is guaranteed that the sum of mm over all test cases does not exceed 105105.

Output

For each test case, output "YES" if there is at least one possible coloring scheme; otherwise, output "NO".

You may print each letter in any case (for example, "YES", "Yes", "yes", and "yEs" will all be recognized as positive answers).

Example

input

2
12 6 2
1 1 1 1 1 7
12 6 2
2 2 2 2 2 2

output

NO
YES

问题解析

题目是说有一个长度为n个格子的纸条,给你m种不同的颜色,每种颜色的数量之和等于k,现在要你给这n个格子涂颜色,使得连续k个格子内的颜色没有相同的。

我们可以把纸条分为长度为k的多个部分,每个部分内部的颜色都不同,且每一个颜色距离上一个相同颜色的距离为k。

可以看出,只要有颜色的数量多于n/k,那显然就是不符合条件的。

要注意的点是最后一个部分的纸条,长度不一定为k,此时这个部分的纸条的长度就表示了最多能有几个颜色的数量等于n/k,如果不满足,也是NO。

AC代码

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include <random>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<fstream>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>//#pragma GCC optimize(2)
//#pragma GCC optimize(3)#define endl '\n'
//#define int ll
#define PI acos(-1)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 998244353;
​
void solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    vector<int>v(m);
    for (int i = 0; i < m; i++)
    {
        cin >> v[i];
    }
    if (k > m)
    {
        cout << "NO" << endl;
        return;
    }
    sort(v.begin(), v.end(), greater<int>());
    int x = n / k, y = n % k;
    for (int i = 0; i < m; i++)
    {
        if (v[i] <= x)break;
        else if (y != 0 && v[i] <= x + 1)y--;
        else if (v[i] > x)
        {
            cout << "NO" << endl;
            return;
        }
    }
    cout << "YES" << endl;
}
​
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}