Educational Codeforces Round 102 (Rated for Div. 2)
A. Replacing Elements
看最小的俩数之和是否大于d,或者所有的数都不大于d
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 1e3 + 10;
int a[maxn];
void solve() {
int n,d;
cin>>n>>d;
for (int i = 0; i <n; ++i) {
cin>>a[i];
}
sort(a,a+n);
if(a[0]+a[1]<=d||a[n-1]<=d)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
B. String LCM
先求出总长度,然后根据公倍数之间的关系进行字符串匹配,成功则打印即可
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 1e3 + 10;
void solve() {
string s,t;
cin>>s>>t;
int sl=s.size();
int tl=t.size();
int zl=sl*tl/__gcd(sl,tl);
for (int i = 0; i < zl; ++i) {
if (s[i%sl]!=t[i%tl]) {
cout<<"-1"<<endl;
return;
}
}
for (int j = 0; j < zl/sl; ++j)
cout<<s;
cout<<endl;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
C. No More Inversions
保留前k个将后半段排列翻转即可保证逆袭对不变。
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 1e3 + 10;
void solve() {
int n,k;
cin>>n>>k;
for (int i = 1; i<2*k-n; ++i)
cout<<i<<" ";
for (int j = k; j>=2*k-n; --j)
cout<<j<<" ";
cout<<endl;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
/*
b[i]=p[a[i]]
a[i]={
i;
2k-i;
};
b[i]=p[i],i<=k
b[i]=p[2k-i],i>k
[1,2k-n]接上[k,0]
*/
D. Program
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
struct node{
int x,y,z;
};
int sum[maxn];
int tree[maxn*4],w[maxn*4],s[maxn*4];
void build(int l,int r,int t){
if (l==r){
tree[t]=sum[l];
if(sum[l]==1){
w[t]=1;
s[t]=0;
}else{
w[t]=0;
s[t]=-1;
}
}else{
int mid=(l+r)>>1;
build(l,mid,t<<1);
build(mid+1,r,t<<1|1);
w[t]=max(w[t<<1],w[t<<1|1]+tree[t<<1]);
s[t]=min(s[t<<1],s[t<<1|1]+tree[t<<1]);
tree[t]=tree[t<<1]+tree[t<<1|1];
}
}
node query(int l,int r,int t,int L,int R){
if(l>=L&&r<=R) return {w[t],s[t],tree[t]};
if (l>R||r<L) return {0,0,0};
int mid=(l+r)>>1;
node a={0,0,0},b={0,0,0},c;
if (mid>=L) a=query(l,mid,t<<1,L,R);
if (mid<R) b=query(mid+1,r,t<<1|1,L,R);
c.x=max(a.x,b.x+a.z);
c.y=min(a.y,b.y+a.z);
c.z=a.z+b.z;
return c;
}
void solve() {
int n,m;
cin>>n>>m;
for (int i = 1; i <=n; ++i) {
char c;
cin>>c;
if(c=='+') sum[i]=1;
else sum[i]=-1;
}
build(1,n,1);
for (int i = 1; i <= m; ++i) {
int l,r;
cin>>l>>r;
node a,b,c;
a=query(1,n,1,1,l-1);
b=query(1,n,1,r+1,n);
c.x=max(a.x,b.x+a.z);
c.y=min(a.y,b.y+a.z);
c.z=a.z+b.z;
cout<<c.x-c.y+1<<endl;
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}