Codeforces Round #829 (Div. 2)
大约 5 分钟
Codeforces Round #829 (Div. 2)
🚀:Codeforces Round #829 (Div. 2) - Codeforces
A. Technical Support
Solve:
从前往后遍历到某一点时,如果‘’比‘’多,则清空为,最后判断‘’的个数是否比‘’’的个数多即可
Code
#include<bits/stdc++.h>
#define ll long long
#define ptn putchar('\n')
using namespace std;
inline ll read() {ll x = 0, z = 1;char c = getchar();while (!isdigit(c)) {if (c == '-')z = -1;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return z * x;}
inline void writ(ll x){if(x<0) {putchar('-');x=(~x)+1;}if(x>9)writ(x/10);putchar(x-x/10*10+48);}
const int N=105;
char s[N];
void ClearDewy(){
int n=read();
cin>>s+1;
int ans=0;
for (int i = 1; i <= n; i++)
{
if(s[i]=='Q')ans++;
else if(s[i]=='A')ans--;
if(ans<0)ans=0;
}
if(ans)puts("No");
else puts("Yes");
}
int main(){
//cin.tie(nullptr)->sync_with_stdio(false);
int T=read();while (T--)
ClearDewy();
return 0;
}
B. Kevin and Permutation
Solve:
找规律构造:
- 为偶数时,最大构造为:
- 为奇数时,将最大的数字放在首位,使他不影响结果,剩下的数字按照为偶数时构造,最大构造为:
Code:
#include<bits/stdc++.h>
#define ll long long
#define ptn putchar('\n')
using namespace std;
inline ll read() {ll x = 0, z = 1;char c = getchar();while (!isdigit(c)) {if (c == '-')z = -1;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return z * x;}
inline void writ(ll x){if(x<0) {putchar('-');x=(~x)+1;}if(x>9)writ(x/10);putchar(x-x/10*10+48);}
void ClearDewy(){
int n=read();
if(n&1){
writ(n);putchar(' ');n--;
}
int m=n/2;
for (int i = m; i; i--)
{
printf("%d %d ",i,m+i);
}
ptn;
}
int main(){
//cin.tie(nullptr)->sync_with_stdio(false);
int T=read();while (T--)
ClearDewy();
return 0;
}
C1. Make Nonzero Sum (easy version)
Solve:
若数组中的数字和不为的倍数,则一定不成立
记录为数组中每个数字的和
若,将每个数字单个输出即可
若,可以证明必有连续的或(时为,时为,将连续的两个放在一起消掉即可,直到
Code:
#include<bits/stdc++.h>
#define ll long long
#define ptn putchar('\n')
using namespace std;
inline ll read() {ll x = 0, z = 1;char c = getchar();while (!isdigit(c)) {if (c == '-')z = -1;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return z * x;}
inline void writ(ll x){if(x<0) {putchar('-');x=(~x)+1;}if(x>9)writ(x/10);putchar(x-x/10*10+48);}
void ClearDewy(){
int n=read();
vector<int>a(n+1);
int sum=0;
for (int i = 1; i <= n; i++)
{
sum+=a[i]=read();
}
if(n&1){
puts("-1");return;
}
vector<pair<int,int>>v;
for (int i = 1; i <= n;i++)
{
if(i<n&&a[i]==a[i+1]&&a[i]*sum>0){
v.push_back({i,i+1});sum-=2*a[i];i++;
}
else v.push_back({i,i});
}
writ(v.size());ptn;
for(pair<int,int> &i:v){
printf("%d %d\n",i.first,i.second);
}
}
int main(){
//cin.tie(nullptr)->sync_with_stdio(false);
int T=read();while (T--)
ClearDewy();
return 0;
}
C2. Make Nonzero Sum (hard version)
Solve:
与相比多了元素为的情况,同,假设,则必有连续的或,选取后对的影响都是,操作到后再将每一个单独选取即可
Code:
#include<bits/stdc++.h>
#define ll long long
#define ptn putchar('\n')
using namespace std;
inline ll read() {ll x = 0, z = 1;char c = getchar();while (!isdigit(c)) {if (c == '-')z = -1;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return z * x;}
inline void writ(ll x){if(x<0) {putchar('-');x=(~x)+1;}if(x>9)writ(x/10);putchar(x-x/10*10+48);}
void ClearDewy(){
int n=read();
vector<int>a(n+1);
int sum=0;
for (int i = 1; i <= n; i++)
{
sum+=a[i]=read();
}
if(sum&1){
puts("-1");return;
}
vector<pair<int,int>>v;
for (int i = 1; i <= n;i++)
{
if(i<n&&(a[i]==a[i+1]||a[i]==0)&&a[i+1]*sum>0){
v.push_back({i,i+1});sum-=2*a[i+1];i++;
}
else v.push_back({i,i});
}
writ(v.size());ptn;
for(pair<int,int> &i:v){
printf("%d %d\n",i.first,i.second);
}
}
int main(){
//cin.tie(nullptr)->sync_with_stdio(false);
int T=read();while (T--)
ClearDewy();
return 0;
}
D. Factorial Divisibility
Solve:
将排列合并:
最后合并到只要个数为即可
Code:
#include<bits/stdc++.h>
#define ll long long
#define ptn putchar('\n')
using namespace std;
inline ll read() {ll x = 0, z = 1;char c = getchar();while (!isdigit(c)) {if (c == '-')z = -1;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return z * x;}
inline void writ(ll x){if(x<0) {putchar('-');x=(~x)+1;}if(x>9)writ(x/10);putchar(x-x/10*10+48);}
void ClearDewy(){
int n=read(),x=read();
vector<int>a(x+2);
for (int i = 1; i <= n; i++)
{
a[read()]++;
}
for (int i = 1; i < x; i++)
{
a[i+1]+=a[i]/(i+1);
a[i]%=i+1;
if(a[i]){
puts("No");return;
}
}
puts("Yes");
}
int main(){
//cin.tie(nullptr)->sync_with_stdio(false);
//int T=read();while (T--)
ClearDewy();
return 0;
}
E. Wish I Knew How to Sort
Solve:
简单的期望题
假设个数中有个,最终状态为:
假设最初状态前个数组中有个,则后个数中也有个
一次选择的总方案数为即,若当前前个数中还剩个,有效的方案数为
所以:
我们又知道期望:
我们还知道期望有线性,所以最后结果为:
最后别忘了取模!!!,因此了一发
Code:
#include<bits/stdc++.h>
#define ll long long
#define ptn putchar('\n')
using namespace std;
inline ll read() {ll x = 0, z = 1;char c = getchar();while (!isdigit(c)) {if (c == '-')z = -1;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return z * x;}
inline void writ(ll x){if(x<0) {putchar('-');x=(~x)+1;}if(x>9)writ(x/10);putchar(x-x/10*10+48);}
const int mod=998244353,N=1e5+5;
inline ll fp(ll x, ll y) {
ll base = 1;
while (y){
if (y&1)base =base*x%mod;
x=x*x%mod;y >>= 1;
}
return base;
}
ll inv(ll x){
return fp(x,mod-2);
}
void ClearDewy(){
int n=read();
ll invn=inv(1LL*n*(n-1)/2%mod);
vector<int>a(n+1);
int num0=0;
for (int i = 1; i <= n; i++)
{
a[i]=read();if(!a[i])num0++;
}
int c1=0;
for (int i = 1; i <= num0; i++)
{
c1+=a[i];
}
ll res=0;
for (int i = 1; i <= c1; i++)
{
res=(res+inv(1LL*i*i%mod*invn%mod))%mod;
}
writ(res);ptn;
}
int main(){
//cin.tie(nullptr)->sync_with_stdio(false);
int T=read();while (T--)
ClearDewy();
return 0;
}