-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathArray_Transform.cpp
More file actions
53 lines (47 loc) · 1.41 KB
/
Array_Transform.cpp
File metadata and controls
53 lines (47 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
bool comp(const pair<int, int> &a, const pair<int, int> &b)
{
return a.second < b.second;
}
int main()
{
int t;
cin >> t;
while ( t-- )
{
int n, K;
cin >> n >> K;
map<int, int> numbers;
for (int i = 1; i <= n; ++i)
{
int number;
cin >> number;
/**
We can decrement a number by K + 1 for arbitrary times.
For example:
n = 4, K = 10.
numbers = 1 11 2 3
Lets try to decrement 11 by 10.
1 11 2 3
Step 1. -9 1 -9 -7 // Decrement all numbers by 1 for K times.
Step 2. 1 0 2 3 // Decrease the number 1 but increment other numbers by K.
*/
++numbers[number % (K + 1)];
}
/** If at most two differnt numbers exist and at least n - 1 numbers are the same,
then it's possible to perform operations such that exactly n - 1 numbers become 0. */
if (numbers.size() <= 2)
{
if (max_element(numbers.begin(), numbers.end(), comp)->second >= n - 1)
{
cout << "YES" << endl;
continue;
}
}
cout << "NO" << endl;
}
return 0;
}