10.10周总结
cf
B. Hemose Shopping
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Hemose was shopping with his friends Samez, AhmedZ, AshrafEzz, TheSawan and O_E in Germany. As you know, Hemose and his friends are problem solvers, so they are very clever. Therefore, they will go to all discount markets in Germany.
Hemose has an array of 𝑛
integers. He wants Samez to sort the array in the non-decreasing order. Since it would be a too easy problem for Samez, Hemose allows Samez to use only the following operation:
Choose indices 𝑖
and 𝑗 such that 1≤𝑖,𝑗≤𝑛, and |𝑖−𝑗|≥𝑥. Then, swap elements 𝑎𝑖 and 𝑎𝑗
.
Can you tell Samez if there’s a way to sort the array in the non-decreasing order by using the operation written above some finite number of times (possibly 0
)?
Input
Each test contains multiple test cases. The first line contains the number of test cases 𝑡
(1≤𝑡≤105)
. Description of the test cases follows.
The first line of each test case contains two integers 𝑛
and 𝑥 (1≤𝑥≤𝑛≤105)
.
The second line of each test case contains 𝑛
integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109)
.
It is guaranteed that the sum of 𝑛
over all test cases doesn’t exceed 2⋅105
.
Output
For each test case, you should output a single string.
If Samez can sort the array in non-decreasing order using the operation written above, output “YES” (without quotes). Otherwise, output “NO” (without quotes).
You can print each letter of “YES” and “NO” in any case (upper or lower).
Example
Input
Copy
4
3 3
3 2 1
4 3
1 2 3 4
5 2
5 1 2 3 4
5 4
1 2 3 4 4
Output
Copy
NO
YES
YES
YES
Note
In the first test case, you can’t do any operations.
In the second test case, the array is already sorted.
In the third test case, you can do the operations as follows:
[5,1,2,3,4]
, 𝑠𝑤𝑎𝑝(𝑎1,𝑎3)
[2,1,5,3,4]
, 𝑠𝑤𝑎𝑝(𝑎2,𝑎5)
[2,4,5,3,1]
, 𝑠𝑤𝑎𝑝(𝑎2,𝑎4)
[2,3,5,4,1]
, 𝑠𝑤𝑎𝑝(𝑎1,𝑎5)
[1,3,5,4,2]
, 𝑠𝑤𝑎𝑝(𝑎2,𝑎5)
[1,2,5,4,3]
, 𝑠𝑤𝑎𝑝(𝑎3,𝑎5)
[1,2,3,4,5]
(Here 𝑠𝑤𝑎𝑝(𝑎𝑖,𝑎𝑗)
refers to swapping elements at positions 𝑖, 𝑗
).
题意:可以交换i和i+k以后所有数的位置
思路:1.判断k是否小于二分之一 如果是 则可以交换任意两个数
2.如果k大于二分之一时候 则会在二分之一处向两侧扩散的不能被交换
3.只需要判断不能被交换位置的数是不是原位置即可
代码如下
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<string>
#include<queue>
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
#define oula bool notprime[1001000];int primes[1001000]; void get_prime() {notprime[1] = true;for (int i = 2; i < 1000010; ++i)if (!notprime[i]) {primes[++primes[0]] = i;for (long long j = (long long) i * i; j < 1000010; j += i)notprime[j] = true;}};
#define ksm inline ll pow(ll x, ll a) {ll ret = 1, k = x;for (; a; a >>= 1, k = k * k)if (a & 1) ret = ret * k;return ret;}
#define gcdxy ll gcd(ll a,ll b){if (a<b)swap(a,b);if (b==0)return a;else return gcd(a%b,b);}
#define fread inline int read() {char ch = getchar();int x = 0, f = 1;while (ch < '0' || ch > '9') {if (ch == ' - ') f = -1;ch = getchar();}while ('0' <= ch && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();}return x * f;}
#define xor_ ll xor_n(ll n){ll t = n & 3;if (t & 1) return t / 2ll ^ 1;return t / 2ll ^ n;}
#define phi int euler(int n){int res=n,end=sqrt(n);for(int i=2;i<=end;i++)if(n%i==0){res-=res/i;while(n%i==0) n/=i;}if(n>1) res-=res/n;return res;}
//oula
ksm
gcdxy
fread
xor_
//phi
int a[100010];
int b[100010];
int main(void) {
int t = read();
while (t--) {
int n = read(), m = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
b[i] = a[i];
}
sort(b + 1, b + n + 1);
if (m <= n / 2) {
cout << "YES" << endl;
continue;
}
int pos = (n+1) - m,flag = 1;
//cout << n + 1 - pos << endl;
for(int i = pos; i <= n + 1 - pos; i++)
{
if(a[i] != b[i]){
flag = 0;
cout << "NO" << endl;
break;
}
}
if(flag)
cout << "YES" << endl;
}
return 0;
}
B2. Wonderful Coloring - 2
原题链接
题意:一共k中颜色
两个要求
1.相同数字只能涂不同颜色
2.相邻不能涂同一种颜色
题解:
1.求出需要涂多少个格子
2.对相应每个格子进行构造涂色
代码如下
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<string>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define oula bool notprime[100010];int primes[100010]; void get_prime() {notprime[1] = true;for (int i = 2; i < 100010; ++i)if (!notprime[i]) {primes[++primes[0]] = i;for (long long j = (long long) i * i; j < 100010; j += i)notprime[j] = true;}};
#define ksm inline ll qpow(ll x, ll a) {ll ret = 1, k = x;for (; a; a >>= 1, k = k * k)if (a & 1) ret = ret * k;return ret;}
#define gcdxy ll gcd(ll a,ll b){if (a<b)swap(a,b);if (b==0)return a;else return gcd(a%b,b);}
#define fread inline int read() {char ch = getchar();int x = 0, f = 1;while (ch < '0' || ch > '9') {if (ch == ' - ') f = -1;ch = getchar();}while ('0' <= ch && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();}return x * f;}
#define xor_ ll xor_n(ll n){ll t = n & 3;if (t & 1) return t / 2ll ^ 1;return t / 2ll ^ n;}
#define shuzu int a[110];
//oula
ksm
gcdxy
fread
xor_
int a[200010], ans[200010];
int main(void) {
int t = read();
while (t--) {
int n, m, sum = 0;
memset(ans,0,sizeof (ans));
memset(a,0,sizeof (a));
map<int, vector<int>> mp;
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
if (mp[a[i]].size() < m)
mp[a[i]].push_back(i);
for (auto i: mp)
sum += i.second.size();
sum -= sum % m;
int c = 0, flag = 0;
for (auto it: mp) {
for (auto itt: it.second) {
ans[itt] = ++c;
c %= m;
if (--sum == 0) {
flag = 1;
break;
}
}
if (flag)
break;
}
for (int i = 0; i < n; i++)
cout << ans[i] << ' ';
cout << endl;
}
return 0;
}
acwing
AcWing 3995. 最小的和
原题链接
这道题因为tmp没开longlong wa了一发 因此回顾一下
本题就是优先队列求前k大的模版题
因为k1和k2操作实质上都是(a+b-1)的平方
所以可以直接写前k大的板子
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
#define oula bool notprime[300010];int primes[300010]; void get_prime() {notprime[1] = true;for (int i = 2; i < 300010; ++i)if (!notprime[i]) {primes[++primes[0]] = i;for (long long j = (long long) i * i; j < 300010; j += i)notprime[j] = true;}};
#define ksm inline ll qpow(ll x, ll a) {ll ret = 1, k = x;for (; a; a >>= 1, k = k * k)if (a & 1) ret = ret * k;return ret;}
#define gcdxy ll gcd(ll a,ll b){if (a<b)swap(a,b);if (b==0)return a;else return gcd(a%b,b);}
#define fread inline ll read() {char ch = getchar();ll x = 0, f = 1;while (ch < '0' || ch > '9') {if (ch == ' - ') f = -1;ch = getchar();}while ('0' <= ch && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();}return x * f;}
#define xor_ ll xor_n(ll n){ll t = n & 3;if (t & 1) return t / 2ll ^ 1;return t / 2ll ^ n;}
#define shuzu int a[110];
oula
ksm
gcdxy
fread
xor_
ll a[100010], b[100010];
int main(void) {
ios::sync_with_stdio(false);
int n, k1, k2;
cin >> n >> k1 >> k2;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
priority_queue<ll, vector<ll>, less<ll>> q;
for (int i = 0; i < n; i++) q.push(qpow(a[i] - b[i], 2));
for (int i = 0; i < k1 + k2; i++) {
int tmp = q.top();
q.pop();
q.push(qpow(sqrt(tmp) * 1ll - 1, 2));
}
ll ans = 0;
while (!q.empty()) {
ans += q.top();
q.pop();
}
cout << ans << endl;
}
**AcWing 3996. 涂色**
没补呢 《明天补》
atcoder
Swiss-System Tournament
原题链接
一开始读假了以为是按号码输出
首先题意就是
1.GCP代表石头,剪刀,布
2.排名2k-1和2k进行比赛
3.预先知道所有人的第m场出什么
题解:
用两个优先队列存大小关系,相互弹出和放入强行打表模拟剪刀石头布即可
(写成大模拟了代码又长又臭之后改一改吧)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
#define oula bool notprime[300010];int primes[300010]; void get_prime() {notprime[1] = true;for (int i = 2; i < 300010; ++i)if (!notprime[i]) {primes[++primes[0]] = i;for (long long j = (long long) i * i; j < 300010; j += i)notprime[j] = true;}};
#define ksm inline ll qpow(ll x, ll a) {ll ret = 1, k = x;for (; a; a >>= 1, k = k * k)if (a & 1) ret = ret * k;return ret;}
#define gcdxy ll gcd(ll a,ll b){if (a<b)swap(a,b);if (b==0)return a;else return gcd(a%b,b);}
#define fread inline ll read() {char ch = getchar();ll x = 0, f = 1;while (ch < '0' || ch > '9') {if (ch == ' - ') f = -1;ch = getchar();}while ('0' <= ch && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();}return x * f;}
#define xor_ ll xor_n(ll n){ll t = n & 3;if (t & 1) return t / 2ll ^ 1;return t / 2ll ^ n;}
#define shuzu int a[110];
oula
ksm
gcdxy
fread
xor_
struct cmp {
bool operator()(pair<int, int> a, pair<int, int> b) {
if (a.second != b.second)
return a.second < b.second;
else
return a.first > b.first;
}
};
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> q, p;
string s[110];
pair<int, int> tm;
pair<int, int> tmm;
int rank_(int i) {
if (s[tm.first][i] == 'G') {
if (s[tmm.first][i] == 'C') {
tm.second++;
q.push(tm);
q.push(tmm);
} else if (s[tmm.first][i] == 'G') {
q.push(tm);
q.push(tmm);
} else {
tmm.second++;
q.push(tm);
q.push(tmm);
}
} else if (s[tm.first][i] == 'C') {
if (s[tmm.first][i] == 'P') {
tm.second++;
q.push(tm);
q.push(tmm);
} else if (s[tmm.first][i] == 'C') {
q.push(tm);
q.push(tmm);
} else {
tmm.second++;
q.push(tm);
q.push(tmm);
}
} else if (s[tm.first][i] == 'P') {
if (s[tmm.first][i] == 'G') {
tm.second++;
q.push(tm);
q.push(tmm);
} else if (s[tmm.first][i] == 'P') {
q.push(tm);
q.push(tmm);
} else {
tmm.second++;
q.push(tm);
q.push(tmm);
}
}
return 0;
}
int rank_1(int i) {
if (s[tm.first][i] == 'G') {
if (s[tmm.first][i] == 'C') {
tm.second++;
p.push(tm);
p.push(tmm);
} else if (s[tmm.first][i] == 'G') {
p.push(tm);
p.push(tmm);
} else {
tmm.second++;
p.push(tm);
p.push(tmm);
}
} else if (s[tm.first][i] == 'C') {
if (s[tmm.first][i] == 'P') {
tm.second++;
p.push(tm);
p.push(tmm);;
} else if (s[tmm.first][i] == 'C') {
p.push(tm);
p.push(tmm);
} else {
tmm.second++;
p.push(tm);
p.push(tmm);
}
} else if (s[tm.first][i] == 'P') {
if (s[tmm.first][i] == 'G') {
tm.second++;
p.push(tm);
p.push(tmm);
} else if (s[tmm.first][i] == 'P') {
p.push(tm);
p.push(tmm);
} else {
tmm.second++;
p.push(tm);
p.push(tmm);
}
}
return 0;
}
int a[100010];
int main(void) {
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 0; i < 2 * n; i++) {
cin >> s[i];
pair<int, int> x;
x.first = i;
x.second = 0;
q.push(x);
}
for (int i = 0; i < m; i++) {
int tmp = n;
while (tmp--) {
if (i & 1) {
tm = p.top();
p.pop();
tmm = p.top();
p.pop();
rank_(i);
} else {
tm = q.top();
q.pop();
tmm = q.top();
q.pop();
rank_1(i);
}
}
}
if (!(m & 1))
while (!q.empty()) {
pair<int, int> tmmm = q.top();
cout << ++tmmm.first << endl;
q.pop();
}
else
while (!p.empty()) {
pair<int, int> tmmm = p.top();
cout << ++tmmm.first << endl;
p.pop();
}
}
数论(逆元)和(同余方程组(线性))等公式推完再补