Codeforces Round #732 (Div. 2)
A. AquaMoon and Two Arrays
A题比较简单,显然总和守恒,只需要总和相等就一定有解;否则,无解。至于输出操作,只需要暴力遍历每一位即可。不多赘述
//#include<bits/stdc++.h> ----万能头----
#include <iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cmath>
#include<queue>
#include<list>
#include<map>
#include<set>
//#include<unordered_map>
#include<stack>
#include<time.h>
using namespace std;
const int mod = 1e9+7;
const int MOD = 998244353;
const int N = 2e3+10;
const int maxn = 1e5+5;
const long long INF = 1e18;
#define REP(i,n) for (int i = 1; i <= n; ++i)
#define REPA(i,j,n) for(lli = j; i <= n; ++i)
#define RREP(i,n) for(ll i = n; i >= 1; --i)
#define REPR(i,j,n) for(ll i = n; i >= j; --i)
#define lll __int128
#define Accpect return 0;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,ll> pdi;
typedef pair<double,PII> DII;
typedef pair<int,PII> PPI;
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (!isdigit(c)) {
if (c == '-')f = -1;
c = getchar();
}
while (isdigit(c))x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * f;
}
int t;
ll n, m, k;
int a[101],b[101];
int c[101];
void solve(){
cin >> n;
REP(i,n)cin >> a[i];
REP(i,n)cin >> b[i];
ll res =0 ;
REP(i,n){
res += a[i] - b[i];
c[i] = a[i] - b[i];
}
if(res != 0){
cout << -1 << '\n';
return;
}
vector<PII> q;
for(int i = 1; i <= n; ++i){
if(c[i] < 0){
for(int j = i + 1; c[i] && j <= n; ++j){
if(c[j] > 0){
if(c[j] + c[i] >= 0){
for(int k = 1; k <= abs(c[i]); ++k)q.push_back({j,i});
c[j] += c[i];
c[i] = 0;
break;
} else {
for(int k = 1; k <= abs(c[j]); ++k)q.push_back({j,i});
c[i] += c[j];
c[j] = 0;
}
}
}
} else if(c[i] > 0){
for(int j = i + 1; j <= n; ++j){
if(c[j] < 0){
if(c[j] + c[i] >= 0){
for(int k = 1; k <= abs(c[j]); ++k)q.push_back({i,j});
c[i] += c[j];
c[j] = 0;
} else {
for(int k = 1; k <= abs(c[i]); ++k)q.push_back({i,j});
c[j] += c[i];
c[i] = 0;
}
}
}
}
}
cout << q.size() << '\n';
for(int i = 0 ; i < q.size(); ++i) cout << q[i].first << " " << q[i].second << '\n';
}
signed main(){
//ios::sync_with_stdio(0);
//cin.tie(0);
//cout.tie(0);
#ifdef ACM
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
t = 1;
//srand((unsigned)time(NULL));
cin >> t;
for(int i = 1; i <= t; ++i) {
solve();
}
fclose(stdin);
fclose(stdout);
//-------------
Accpect;
//-------------
}
B. AquaMoon and Stolen String
B题也较为简单,总共有n(n为奇数)个原串,从中偷走一个,剩余的n-1个串两两随机配对,并存在交换相同位上字母的情况,但是显然,每一位上总共出现的字母数量和对应总类是恒定的,那么,我们只需要各自遍历一遍原本的和后来,记录每个位上的情况,做差值,剩下的每一位上存在的字母便是偷走的串的每一位。
//#include<bits/stdc++.h> ----万能头----
#include <iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cmath>
#include<queue>
#include<list>
#include<map>
#include<set>
//#include<unordered_map>
#include<stack>
#include<time.h>
using namespace std;
const int mod = 1e9+7;
const int MOD = 998244353;
const int N = 1e5+10;
const int maxn = 1e5+5;
const long long INF = 1e18;
#define REP(i,n) for (int i = 1; i <= n; ++i)
#define REPA(i,j,n) for(lli = j; i <= n; ++i)
#define RREP(i,n) for(ll i = n; i >= 1; --i)
#define REPR(i,j,n) for(ll i = n; i >= j; --i)
#define lll __int128
#define Accpect return 0;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,ll> pdi;
typedef pair<double,PII> DII;
typedef pair<int,PII> PPI;
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (!isdigit(c)) {
if (c == '-')f = -1;
c = getchar();
}
while (isdigit(c))x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * f;
}
int t;
ll n, m, k;
ll num[26][N];
string s[N];
string res[N];
ll num2[26][N];
bool ck(){
for(int i = 0; i < 26; ++i)if(num2[i] != num[i])return 0;
return 1;
}
void solve(){
cin >> n >> m;
REP(i,n) cin >> s[i];
REP(i,n-1) cin >> res[i];
memset(num,0,sizeof num);
memset(num2,0,sizeof num2);
for(int i = 1; i <= n; ++i){
for(int j = 0; j < m; ++j)num[s[i][j]-'a'][j]++;
}
for(int i = 1; i < n; ++i){
for(int j = 0; j < m; ++j)num2[res[i][j]-'a'][j]++;
}
for(int i = 0 ; i < 26; ++i)
for(int j = 0; j < m; ++j) num[i][j] = num[i][j] - num2[i][j];
for(int i = 1; i <= n; ++i){
for(int j = 0; j < m; ++j){
if(num[s[i][j]-'a'][j]){
if(j == m-1){
cout << s[i] << '\n';
return;
}
}
else break;
}
}
}
signed main(){
//ios::sync_with_stdio(0);
//cin.tie(0);
//cout.tie(0);
#ifdef ACM
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
t = 1;
//srand((unsigned)time(NULL));
cin >> t;
for(int i = 1; i <= t; ++i) {
solve();
}
fclose(stdin);
fclose(stdout);
//-------------
Accpect;
//-------------
}
C. AquaMoon and Strange Sort
C题需要认真思考一下。由题意显然,我们只有再保证每一位都经过偶数次操作到达它本该在的位置,才能保证最后所有位都朝向右侧。我们思考一下,对于每一个数,假设它当前位是 i i i,目标位是 j j j,并且它最后一定会在目标的相邻位走向目标位,在整个过程中,它会交换若干次,我们设为 k k k,那么思考每一次交换对当前数的影响,首先,朝向改变,其次,距离改变 ( + 1 / − 1 ) (+1/-1) (+1/−1)。而距离改变意味着它到终点必要的交换次数会改变,即本来是 j − i j-i j−i现在变为 j − i ( + 1 / − 1 ) j - i (+ 1/- 1) j−i(+1/−1),思考到最后一步,进行了 k − 1 k-1 k−1次,那么剩下所需距离的带来的奇偶性变化次数等价于 j − i + ( k − 1 ) j-i+(k-1) j−i+(k−1)(所有+1与-1影响一致),而当前的朝向所带来的奇偶性变化次数可以用 k − 1 k-1 k−1表示,那么在走完最后一步带来后的奇偶性变化等价于 j − i j-i j−i。那么一切都显然了。
//#include<bits/stdc++.h> ----万能头----
#include <iostream>
#include<stdio.h>
#include<cstdio>8
#include<string>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cmath>
#include<queue>
#include<list>
#include<map>
#include<set>
//#include<unordered_map>
#include<stack>
#include<time.h>
using namespace std;
const int mod = 1e9+7;
const int MOD = 998244353;
const int N = 2e5+10;
const int maxn = 1e5+5;
const long long INF = 1e18;
#define REP(i,n) for (int i = 1; i <= n; ++i)
#define REPA(i,j,n) for(lli = j; i <= n; ++i)
#define RREP(i,n) for(ll i = n; i >= 1; --i)
#define REPR(i,j,n) for(ll i = n; i >= j; --i)
#define lll __int128
#define Accpect return 0;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,ll> pdi;
typedef pair<double,PII> DII;
typedef pair<int,PII> PPI;
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (!isdigit(c)) {
if (c == '-')f = -1;
c = getchar();
}
while (isdigit(c))x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * f;
}
int t;
ll n, m, k;
ll a[N];
ll f[2][N];
void solve(){
cin >> n;
REP(i,n)cin >> a[i],f[0][a[i]] = f[1][a[i]] = 0;
REP(i,n)++f[i&1][a[i]];
sort(a+1,a+1+n);
REP(i,n)--f[i&1][a[i]];
bool ok = 1;
REP(i,n){
if(!ok)break;
if(f[1][a[i]] || f[0][a[i]]) ok = 0;
}
if(ok)cout << "YES\n";
else cout << "NO\n";
}
signed main(){
//ios::sync_with_stdio(0);
//cin.tie(0);
//cout.tie(0);
#ifdef ACM
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
t = 1;
//srand((unsigned)time(NULL));
cin >> t;
for(int i = 1; i <= t; ++i) {
solve();
}
fclose(stdin);
fclose(stdout);
//-------------
Accpect;
//-------------
}
D. AquaMoon and Chess
这是一道组合数学.
首先题目给我们的两种变化方式是对称的,不难注意到,只有两个
1
1
1连在一起时,才能转移,并且永远以两个1的方式一起转移。如果我们把两个相邻的1看成一个整体记为
p
p
p,那么任意到达的状态永远与当前的状态中
p
,
0
p,0
p,0的数量一致。接着思考每一个
p
p
p可以到达的位置,如果走向的那一侧为0,则可以移动,如果是偶数个1,就等于多个
p
p
p在这个区间内任意走动,如果出现奇数个1,那么在向走向方向移动的时候,一定会额外拉个1当替罪羊,然后那个1走出去,这样
p
p
p可以达到任何位置,如果这个序列中存在
p
p
p,并且即使过程中
p
p
p的成员会更换。不难注意到,当前仅当有
p
p
p经过特定奇数个1时,单独1的位置才发生变化,且与
p
p
p唯一对应,即每一种
p
p
p的状态都有确定的单独1的状态,可以认为,单独1和0在
p
p
p的移动过程中是相对固定的 (这样思考,对于一个特定的单独的1,在某个状态下,其左右分别需要若干个
p
p
p,那么就需要p的转移,转移带来的奇偶性变化是恒定的),即,存在奇数个1的连续体的数量也是固定的,那么整个问题转化为
p
p
p的一个放置,由于
p
p
p的每个状态都有唯一的单独1的状态与它对应,那么问题又可以转化为0的放置,既然单独的1跟着
p
p
p变化,我们只需要知道0和
p
p
p的数量即可,设分别为
m
,
n
m,n
m,n,则答案为
(
m
+
n
m
)
\tbinom{m+n}{m}
(mm+n),即总共由
n
+
m
n+m
n+m个位置,选
m
m
m个为0.
//#include<bits/stdc++.h> ----万能头----
#include <iostream>
#include<stdio.h>
#include<cstdio>
#include<string>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cmath>
#include<queue>
#include<list>
#include<map>
#include<set>
//#include<unordered_map>
#include<stack>
#include<time.h>
using namespace std;
const int mod = 1e9+7;
const int MOD = 998244353;
const int N = 2e5+10;
const int maxn = 1e5+5;
const long long INF = 1e18;
#define REP(i,n) for (ll i = 1; i <= n; ++i)
#define REPA(i,j,n) for(ll i = j; i <= n; ++i)
#define RREP(i,n) for(ll i = n; i >= 1; --i)
#define REPR(i,j,n) for(ll i = n; i >= j; --i)
#define lll __int128
#define Accpect return 0;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,ll> pdi;
typedef pair<double,PII> DII;
typedef pair<int,PII> PPI;
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (!isdigit(c)) {
if (c == '-')f = -1;
c = getchar();
}
while (isdigit(c))x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * f;
}
int t;
ll n, m, k;
char s[N];
bool st[N];
ll f[N];
void init(){
f[0] = 1;
for(ll i = 1; i < N; ++i)f[i] = f[i-1] * i % MOD;
}
ll fast(ll a, ll b){
ll res = 1;
while(b){
if(b & 1)res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res % MOD;
}
ll get_com(ll a, ll b){
return f[a]*fast(f[b]*f[a-b]%MOD,MOD-2)%MOD;
}
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i)st[i] = 0;
s[0] = '0';
cin >> s + 1;
ll cnt = 0; m = 0;
for(int i = 1; i <= n; ++i){
if(s[i] == s[i-1] && s[i] == '1' && !st[i-1]){
st[i-1] = st[i] = 1;
++cnt;
}
if(s[i] == '0') ++m;
}
cout << get_com(m+cnt,cnt) << '\n';
}
signed main(){
//ios::sync_with_stdio(0);
//cin.tie(0);
//cout.tie(0);
#ifdef ACM
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
t = 1;
//srand((unsigned)time(NULL));
cin >> t;
init();
for(int i = 1; i <= t; ++i) {
solve();
}
fclose(stdin);
fclose(stdout);
//-------------
Accpect;
//-------------
}