目录
1.Problem
2.Input
3.Output
4.Examples
4.1input
4.2output
5.Code
6.Conclusion
Nezzar designs a brand new game "Hidden Permutations" and shares it with his best friend, Nanako.
At the beginning of the game, Nanako and Nezzar both know integers nn and mm. The game goes in the following way:
However, Nezzar accidentally knows Nanako's unordered pairs and decides to take advantage of them. Please help Nezzar find out two permutations pp and qq such that the score is maximized.
The first line contains a single integer tt (1≤t≤5⋅1051≤t≤5⋅105) — the number of test cases.
The first line of each test case contains two integers n,mn,m (1≤n≤5⋅105,0≤m≤min(n(n−1)2,5⋅105)1≤n≤5⋅105,0≤m≤min(n(n−1)2,5⋅105)).
Then mm lines follow, ii-th of them contains two integers li,rili,ri (1≤li,ri≤n1≤li,ri≤n, li≠rili≠ri), describing the ii-th unordered pair Nanako chooses. It is guaranteed that all mm unordered pairs are distinct.
It is guaranteed that the sum of nn for all test cases does not exceed 5⋅1055⋅105, and the sum of mm for all test cases does not exceed 5⋅1055⋅105.
For each test case, print two permutations p1,p2,…,pnp1,p2,…,pn and q1,q2,…,qnq1,q2,…,qn such that the score Nezzar gets is maximized.
3
4 2
1 2
3 4
6 4
1 2
1 3
3 5
3 6
2 1
1 2
1 2 3 4
3 4 1 2
2 3 4 1 6 5
1 4 3 2 5 6
1 2
1 2
#include
using namespace std;
typedef long long ll;
ll readint() {
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main() {
ll n = readint();
ll t = readint();
char s[100005];
scanf("%s", s + 1);
t -= 1 << (s[n] - 'a');
t += 1 << (s[n - 1] - 'a');
vector cnt(26, 0);
for (int i = 1; i <= n - 2; i++)
cnt[s[i] - 'a']++;
for (int i = 0; i <= 25; i++)
t += 1ll * cnt[i] * (1 << i);
if (t < 0 || t & 1)
return printf("No\n"), 0;
t /= 2;
for (int i = 0; i <= 60; i++) {
if (t & 1) {
if (cnt[i] == 0)
return printf("No\n"), 0;
cnt[i]--;
}
cnt[i + 1] += cnt[i] / 2;
t /= 2;
}
printf("Yes\n");
return 0;
}
这段代码的主要作用是判断是否能通过一系列操作,将给定数组s的某些元素值变换,使得最终数组满足特定条件。具体来说:
1.从标准输入读取两个整数 n 和 t,以及一个字符串 s。
2.对 t 进行一系列运算,包括减去 1 << (s[n] - 'a') 和加上 1 << (s[n - 1] - 'a')。
3.统计数组 s 中除了最后两个元素之外的字符频次,存储在 cnt 数组中。
4.根据字符频次和位运算,调整 t 的值。
5.判断是否满足一些条件,如果不满足则输出 "No" 并结束程序,否则继续执行。
6.进行一系列位运算和数组操作,最终判断是否能达到目标值 t,如果可以则输出 "Yes",否则输出 "No"。总体来说,这段代码实现了一个特定的逻辑,通过对输入进行一系列操作,判断最终是否满足特定条件。