CF edu 168 题解
A
直接找两个相同的字符往里面塞个不同的字符即可。
点击查看代码
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
打 cf 不要用 umap!!!
记住,rating 是身外之物。
该冲正解时冲正解!
Problem:
算法:
思路:
*/
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define re register
#define ll long long
#define forl(i,a,b) for(re ll i=a;i<=b;i++)
#define forr(i,a,b) for(re ll i=a;i>=b;i--)
#define forll(i,a,b,c) for(re ll i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(re ll i=a;i>=b;i-=c)
#define mid ((l+r)>>1)
#define lowbit(x) (x&-x)
#define pb push_back
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);init();
#define endl '\n'
#define QwQ return 0;
#define db long double
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define x first
#define y second
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
#define maxqueue priority_queue<ll>
#define minqueue priority_queue<ll,vector<ll>,greater<ll>>
void Max(ll&x,ll y){x=max(x,y);}
void Min(ll&x,ll y){x=min(x,y);}
ll _t_;
void _clear(){}
void solve()
{
_clear();
string s;
cin>>s;
ll n=s.size();
s=' '+s;
forl(i,1,n-1)
{
if(s[i]==s[i+1])
{
if(s[i]=='a')
{
forl(j,1,i)
cout<<s[j];
cout<<'b';
forl(j,i+1,n)
cout<<s[j];
cout<<endl;
}
else
{
forl(j,1,i)
cout<<s[j];
cout<<'a';
forl(j,i+1,n)
cout<<s[j];
cout<<endl;
}
return ;
}
}
forl(i,1,n)
cout<<s[i];
if(s[n]=='a')
cout<<'b';
else
cout<<'a';
cout<<endl;
}
void init(){}
int main()
{
IOS;
_t_=1;
cin>>_t_;
while(_t_--)
solve();
QwQ;
}
B
直接暴力判断用前缀和维护即可。
点击查看代码
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
打 cf 不要用 umap!!!
记住,rating 是身外之物。
该冲正解时冲正解!
Problem:
算法:
思路:
*/
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define re register
#define ll long long
#define forl(i,a,b) for(re ll i=a;i<=b;i++)
#define forr(i,a,b) for(re ll i=a;i>=b;i--)
#define forll(i,a,b,c) for(re ll i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(re ll i=a;i>=b;i-=c)
#define mid ((l+r)>>1)
#define lowbit(x) (x&-x)
#define pb push_back
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);init();
#define endl '\n'
#define QwQ return 0;
#define db long double
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define x first
#define y second
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
#define maxqueue priority_queue<ll>
#define minqueue priority_queue<ll,vector<ll>,greater<ll>>
void Max(ll&x,ll y){x=max(x,y);}
void Min(ll&x,ll y){x=min(x,y);}
ll _t_;
void _clear(){}
ll n;
ll pd;
string s[10];
ll sum[200010],sum2[200010];
ll sum3[200010],sum4[200010];
void solve()
{
pd=1;
_clear();
cin>>n>>s[1]>>s[2];
forl(i,0,n+2)
sum[i]=sum2[i]=sum3[i]=sum4[i]=0;
s[1]=' '+s[1];
s[2]=' '+s[2];
forl(j,1,2)
forl(i,1,n)
pd&=s[j][i]=='x';
forl(i,1,n)
sum[i]|=s[1][i]=='.',sum2[i]=sum[i];
forl(i,1,n)
sum[i]|=sum[i-1];
forr(i,n,1)
sum2[i]|=sum2[i+1];
forl(i,1,n)
sum3[i]|=s[2][i]=='.',sum4[i]=sum3[i];
forl(i,1,n)
sum3[i]|=sum3[i-1];
forr(i,n,1)
sum4[i]|=sum4[i+1];
if(pd==1)
{
cout<<0<<endl;
return ;
}
ll ans=0;
forl(i,1,n)
if(s[2][i-1]=='x' && s[2][i]=='.' && s[2][i+1]=='x' && (sum[i-1] || sum3[i-2]) && (sum2[i+1] || sum4[i+2]))
ans++;
forl(i,1,n)
if(s[1][i-1]=='x' && s[1][i]=='.' && s[1][i+1]=='x' && (sum[i-2] || sum3[i-1]) && (sum2[i+2] || sum4[i+1]))
ans++;
cout<<ans<<endl;
}
void init(){}
int main()
{
IOS;
_t_=1;
cin>>_t_;
while(_t_--)
solve();
QwQ;
}
C
右括号合法情况下能放就放,剩余情况放左括号即可。
点击查看代码
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
打 cf 不要用 umap!!!
记住,rating 是身外之物。
该冲正解时冲正解!
Problem:
算法:
思路:
*/
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define re register
#define ll long long
#define forl(i,a,b) for(re ll i=a;i<=b;i++)
#define forr(i,a,b) for(re ll i=a;i>=b;i--)
#define forll(i,a,b,c) for(re ll i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(re ll i=a;i>=b;i-=c)
#define mid ((l+r)>>1)
#define lowbit(x) (x&-x)
#define pb push_back
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);init();
#define endl '\n'
#define QwQ return 0;
#define db long double
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define x first
#define y second
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
#define maxqueue priority_queue<ll>
#define minqueue priority_queue<ll,vector<ll>,greater<ll>>
void Max(ll&x,ll y){x=max(x,y);}
void Min(ll&x,ll y){x=min(x,y);}
ll _t_;
void _clear(){}
ll n;
ll pd;
string s[10];
ll sum[200010],sum2[200010];
ll sum3[200010],sum4[200010];
void solve()
{
pd=1;
_clear();
cin>>n>>s[1]>>s[2];
forl(i,0,n+2)
sum[i]=sum2[i]=sum3[i]=sum4[i]=0;
s[1]=' '+s[1];
s[2]=' '+s[2];
forl(j,1,2)
forl(i,1,n)
pd&=s[j][i]=='x';
forl(i,1,n)
sum[i]|=s[1][i]=='.',sum2[i]=sum[i];
forl(i,1,n)
sum[i]|=sum[i-1];
forr(i,n,1)
sum2[i]|=sum2[i+1];
forl(i,1,n)
sum3[i]|=s[2][i]=='.',sum4[i]=sum3[i];
forl(i,1,n)
sum3[i]|=sum3[i-1];
forr(i,n,1)
sum4[i]|=sum4[i+1];
if(pd==1)
{
cout<<0<<endl;
return ;
}
ll ans=0;
forl(i,1,n)
if(s[2][i-1]=='x' && s[2][i]=='.' && s[2][i+1]=='x' && (sum[i-1] || sum3[i-2]) && (sum2[i+1] || sum4[i+2]))
ans++;
forl(i,1,n)
if(s[1][i-1]=='x' && s[1][i]=='.' && s[1][i+1]=='x' && (sum[i-2] || sum3[i-1]) && (sum2[i+2] || sum4[i+1]))
ans++;
cout<<ans<<endl;
}
void init(){}
int main()
{
IOS;
_t_=1;
cin>>_t_;
while(_t_--)
solve();
QwQ;
}
D
题目链接
CF1997D Maximize the Root (codeforces)
CF1997D Maximize the Root (luogu)
解题思路
二分答案。
可以逐一计算每个节点在节点一最终的权值为 \(Mid\) 时的所需要的付出的代价,可以通过 dfs 来进行代价的下传。
代价的转移比较显然。
时间复杂度 \(O(n \log V)\)。其中 \(V\) 为答案的上界。
注意下传代价时可能会爆 long long
,记得及时剪枝。
参考代码
点击查看代码
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
打 cf 不要用 umap!!!
记住,rating 是身外之物。
该冲正解时冲正解!
Problem:
算法:
思路:
*/
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define re register
#define ll long long
#define forl(i,a,b) for(re ll i=a;i<=b;i++)
#define forr(i,a,b) for(re ll i=a;i>=b;i--)
#define forll(i,a,b,c) for(re ll i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(re ll i=a;i>=b;i-=c)
#define mid ((l+r)>>1)
#define lowbit(x) (x&-x)
#define pb push_back
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);init();
#define endl '\n'
#define QwQ return 0;
#define db long double
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define x first
#define y second
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
#define maxqueue priority_queue<ll>
#define minqueue priority_queue<ll,vector<ll>,greater<ll>>
void Max(ll&x,ll y){x=max(x,y);}
void Min(ll&x,ll y){x=min(x,y);}
ll _t_;
void _clear(){}
ll n;
ll a[200010];
vector<ll>G[200010];
ll L,R;
ll fl;
ll x,y;
void dfs(ll x,ll fa,ll need)
{
if(need>1e17)
{
fl=0;
return ;
}
ll pd=0;
for(auto i:G[x])
if(i!=fa)
pd=1,dfs(i,x,need+max(0ll,need-a[x]));
// cout<<x<<' '<<fa<<' '<<need;
if(pd==0 && a[x]<need)
{
// need=max(need-a[x],0ll);
fl=0;
}
}
bool check(ll x)
{
fl=1;
for(auto i:G[1])
dfs(i,1,x-a[1]);
return fl;
}
void solve()
{
_clear();
cin>>n;
forl(i,0,n+2)
G[i].clear();
forl(i,1,n)
cin>>a[i];
forl(i,2,n)
cin>>x,G[x].pb(i),G[i].pb(x);
L=0,R=1e10;
while(L<R)
{
ll Mid=(L+R+1)/2;
if(check(Mid))
L=Mid;
else
R=Mid-1;
}
cout<<L<<endl;
}
void init(){}
int main()
{
IOS;
_t_=1;
cin>>_t_;
while(_t_--)
solve();
QwQ;
}
E
upd:原来代码 fst 了,现已修改了代码和思路。
题目链接
解题思路
根号分治。
设阙值为 \(B\),我们可以将所有小于等于 \(B\) 的 \(k\) 的全部答案都预处理出来,这个部分可以使用 bitset 来进行维护从而降低空间复杂度,反之可以通过预处理 \(sum_{i,j}\) 表示前 \(i\) 个人战斗力大于等于 \(j\) 的人的个数,然后就可以进行二分答案了。
这里第一个预处理 \(B\) 取 \(1200\),第二个预处理 \(B\) 取 \(560\) 可以通过此题。
时间复杂度 \(O(n \sqrt{n} \log n)\)。
参考代码
点击查看代码
// LUOGU_RID: 170011394
#include<bits/stdc++.h>
using namespace std;//
//#define map unordered_map
#define re register
#define ll int
#define forl(i,a,b) for(re ll i=a;i<=b;i++)
#define forr(i,a,b) for(re ll i=a;i>=b;i--)
#define forll(i,a,b,c) for(re ll i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(re ll i=a;i>=b;i-=c)
#define mid ((l+r)>>1)
#define lowbit(x) (x&-x)
#define pb push_back
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);init();
//#define endl '\n'
#define QwQ return 0;
#define db long double
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define x first
#define y second
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
#define maxqueue priority_queue<ll>
#define minqueue priority_queue<ll,vector<ll>,greater<ll>>
namespace Fread
{
const int SIZE = 1 << 16;
char buf[SIZE], *S, *T;
inline char getchar() { if (S == T) { T = (S = buf) + fread(buf, 1, SIZE, stdin); if (S == T) return '\n'; } return *S++; }
}
using namespace Fread;
namespace Fwrite
{
const int SIZE = 1 << 16;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() { fwrite(buf, 1, S - buf, stdout); S = buf; }
inline void putchar(char c) { *S++ = c; if (S == T) flush(); }
struct NTR { ~NTR() { flush(); } } ztr;
}
using namespace Fwrite;
#define getchar Fread::getchar
#define putchar Fwrite::putchar
namespace Fastio
{
struct Reader
{
template <typename T> Reader& operator >> (T &x)
{
x = 0;
short f = 1;
char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f *= -1; c = getchar(); }
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
x *= f;
return *this;
}
Reader& operator >> (double &x)
{
x = 0;
double t = 0;
short f = 1, s = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '.') { if (c == '-') f *= -1; c = getchar(); }
while (c >= '0' && c <= '9' && c != '.') x = x * 10 + (c ^ 48), c = getchar();
if (c == '.') c = getchar();
else { x *= f; return *this; }
while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s++, c = getchar();
while (s--) t /= 10.0;
x = (x + t) * f;
return *this;
}
Reader& operator >> (long double &x)
{
x = 0;
long double t = 0;
short f = 1, s = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '.') { if (c == '-') f *= -1; c = getchar(); }
while (c >= '0' && c <= '9' && c != '.') x = x * 10 + (c ^ 48), c = getchar();
if (c == '.') c = getchar();
else { x *= f; return *this; }
while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s++, c = getchar();
while (s--) t /= 10.0;
x = (x + t) * f;
return *this;
}
Reader& operator >> (__float128 &x)
{
x = 0;
__float128 t = 0;
short f = 1, s = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '.') { if (c == '-') f *= -1; c = getchar(); }
while (c >= '0' && c <= '9' && c != '.') x = x * 10 + (c ^ 48), c = getchar();
if (c == '.') c = getchar();
else { x *= f; return *this; }
while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s++, c = getchar();
while (s--) t /= 10.0;
x = (x + t) * f;
return *this;
}
Reader& operator >> (char &c)
{
c = getchar();
while (c == ' ' || c == '\n' || c == '\r') c = getchar();
return *this;
}
Reader& operator >> (char *str)
{
int len = 0;
char c = getchar();
while (c == ' ' || c == '\n' || c == '\r') c = getchar();
while (c != ' ' && c != '\n' && c != '\r') str[len++] = c, c = getchar();
str[len] = '\0';
return *this;
}
Reader& operator >> (string &str)
{
str.clear();
char c = getchar();
while (c == ' ' || c == '\n' || c == '\r') c = getchar();
while (c != ' ' && c != '\n' && c != '\r') str.push_back(c), c = getchar();
return *this;
}
Reader() {}
} cin;
const char endl = '\n';
struct Writer
{
const int Setprecision = 6;
typedef int mxdouble;
template <typename T> Writer& operator << (T x)
{
if (x == 0) { putchar('0'); return *this; }
if (x < 0) putchar('-'), x = -x;
static short sta[40];
short top = 0;
while (x > 0) sta[++top] = x % 10, x /= 10;
while (top > 0) putchar(sta[top] + '0'), top--;
return *this;
}
Writer& operator << (double x)
{
if (x < 0) putchar('-'), x = -x;
mxdouble _ = x;
x -= (double)_;
static short sta[40];
short top = 0;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
if (top == 0) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
putchar('.');
for (int i = 0; i < Setprecision; i++) x *= 10;
_ = x;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
for (int i = 0; i < Setprecision - top; i++) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
return *this;
}
Writer& operator << (long double x)
{
if (x < 0) putchar('-'), x = -x;
mxdouble _ = x;
x -= (long double)_;
static short sta[40];
short top = 0;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
if (top == 0) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
putchar('.');
for (int i = 0; i < Setprecision; i++) x *= 10;
_ = x;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
for (int i = 0; i < Setprecision - top; i++) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
return *this;
}
Writer& operator << (__float128 x)
{
if (x < 0) putchar('-'), x = -x;
mxdouble _ = x;
x -= (__float128)_;
static short sta[40];
short top = 0;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
if (top == 0) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
putchar('.');
for (int i = 0; i < Setprecision; i++) x *= 10;
_ = x;
while (_ > 0) sta[++top] = _ % 10, _ /= 10;
for (int i = 0; i < Setprecision - top; i++) putchar('0');
while (top > 0) putchar(sta[top] + '0'), top--;
return *this;
}
Writer& operator << (char c) { putchar(c); return *this; }
Writer& operator << (char *str)
{
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer& operator << (const char *str)
{
int cur = 0;
while (str[cur]) putchar(str[cur++]);
return *this;
}
Writer& operator << (string str)
{
int st = 0, ed = str.size();
while (st < ed) putchar(str[st++]);
return *this;
}
Writer() {}
} cout;
}
using namespace Fastio;
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
void Max(ll&x,ll y){x=max(x,y);}
void Min(ll&x,ll y){x=min(x,y);}
ll _t_;
void _clear(){}
ll n,q,sq,sq2;
ll a[200010];
//ll vis[200010][320];
bitset<1210>vis[200010];
ll sum[200010][560];
ll maxn[200010],minn[200010],Sum[200010];
ll times;
map<ll,map<ll,ll>>mp;
ll get(ll l,ll r,ll x)
{
ll su=0;
forl(i,l,r)
su+=a[i]>=x,times++;
return su;
}
ll lst;
ll x,y;
void solve()
{
_clear();
cin>>n>>q;
sq=558,sq2=1200;
// sq=sqrt(n);
forl(i,1,n)
{
maxn[i]=1e18;
cin>>a[i];
forl(j,0,sq+1)
{
sum[i][j]=sum[i-1][j];
if(a[i]>=j)
sum[i][j]++;
times++;
}
}
forl(i,1,n)
Sum[i]=Sum[i-1]+(a[i]==200000);
forl(i,1,sq2)
{
ll sum=1,sum2=0;
forl(j,1,n)
{
times++;
if(sum>a[j])
continue;
vis[j][i]=1;
sum2++;
if(sum2==i)
{
sum2=0;
sum++;
}
}
}
while(q--)
{
cin>>x>>y;
if(a[x]==2e5)
{
cfy;
continue;
}
if(a[x]==1 && Sum[x]>y)
{
cfn;
continue;
}
if(maxn[x]<=y)
{
cfy;
continue;
}
if(minn[x]>=y)
{
cfn;
continue;
}
if(a[x]>=(x+y-1)/y)
{
cfy;
lst=1;
continue;
}
if(mp[x][y])
{
if(mp[x][y]==1){
cfy;
}
else
cfn;
continue;
}
if(y<=1200)
{
printcf(vis[x][y]);
continue;
}
ll now=1,sum2=1;
forl(i,1,n)
{
ll L=now+y-1,R=n,fl=1;
while(L<R)
{
ll Mid=(L+R)/2;
if(sum[Mid][sum2]-sum[now-1][sum2]<y)
L=Mid+1;
else
R=Mid;
if(L>=x)
{
if(a[x]>=sum2)
mp[x][y]=1,Min(maxn[x],y);
else
mp[x][y]=-1,Max(minn[x],y);
printcf(a[x]>=sum2);
fl=0;
break;
}
}
if(!fl)
break;
sum2++;
now=L+1;
if(L>=x)
{
if(a[x]>=sum2-1)
mp[x][y]=1,Min(maxn[x],y);
else
mp[x][y]=-1,Max(minn[x],y);
printcf(a[x]>=sum2-1);
break;
}
}
}
}
void init(){}
int main()
{
// IOS;
_t_=1;
// cin>>_t_;
while(_t_--)
solve();
QwQ;
}