2025暑期算法竞赛学习笔记(二)
前言
2025.7.2
2023-2024 ICPC German Collegiate Programming Contest (GCPC 2023)
GCPC2023链接(qoj版)
1.G. German Conference for Public Counting
签到,不述。
2.L. Loop Invariant
- 把括号序列用栈拆成一段一段。如果段数
>=2 ,且存在不同的段时,可以构造;否则不能。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1000006;
string st;
int haxi[N];
vector <string> vec;
stack <int> s;
int js(string ss) {
int t=0;
for (int i=0;i<(int)ss.size();i++) {
t=(t*433+ss[i])%998244353;
}
return t;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>st;
string t="";
for (int i=0;i<(int)st.size();i++) {
t+=st[i];
if (st[i]=='(') {
s.push(i);
}
else {
int u=s.top();
s.pop();
if (s.empty()) {
vec.push_back(t);
t="";
}
}
}
if (!s.empty()) {
cout<<"no"<<endl;
return 0;
}
int cnt=(int)vec.size();
for (int i=0;i<(int)vec.size();i++) {
haxi[i+1]=js(vec[i]);
}
sort(haxi+1,haxi+cnt+1);
if (cnt==1) {
cout<<"no"<<endl;
return 0;
}
if (haxi[1]==haxi[cnt]) {
cout<<"no"<<endl;
return 0;
}
string ans="";
for (int i=1;i<cnt;i++) ans+=vec[i];
ans+=vec[0];
cout<<ans<<endl;
return 0;
}
3.B. Balloon Darts
-
抽屉原理可得:对于前
k+1 个点,想把他们分为k 条直线的话,至少有两个点是在同一个直线上的。 -
从这个点出发,暴力即可。复杂度为
O\left( C\mkern2mu\genfrac{}{}{0pt}{}{4}{2}C\mkern2mu\genfrac{}{}{0pt}{}{3}{2}C\mkern2mu\genfrac{}{}{0pt}{}{2}{2} \cdot n \right) ,可行。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1000006;
vector <int> b;
struct qwq{
int x,y;
}a[N];
int n,tf;
void dfs(vector<int> s,int x) {
if ((int)s.size()<=x) {
tf=1;
return ;
}
if (x==0) return ;
for (int i=1;i<=x+1;i++) {
for (int j=1;j<=i-1;j++) {
vector <int> now;
now.clear();
for (int p=0;p<(int)s.size();p++) {
int k=s[p];
if (k==s[i-1]||k==s[j-1]) continue;
if ((a[s[i-1]].y-a[s[j-1]].y)*(a[s[i-1]].x-a[k].x)!=(a[s[i-1]].y-a[k].y)*(a[s[i-1]].x-a[s[j-1]].x)) {
now.push_back(k);
}
}
dfs(now,x-1);
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
tf=0;
cin>>n;
for (int i=1;i<=n;i++) {
cin>>a[i].x>>a[i].y;
b.push_back(i);
}
dfs(b,3);
if (tf) cout<<"possible"<<endl;
else cout<<"impossible"<<endl;
return 0;
}
4.D. DnD Dice
直接暴力DP即可。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=200005;
const int t[]={0,4,6,8,12,20};
int n,cnt,a[N],b[N];
struct qwq{
double p,num;
}s[N];
double pre[N];
bool cmp(qwq x,qwq y) {
return x.p>y.p;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
for (int i=1;i<=5;i++) cin>>a[i];
for (int i=1;i<=5;i++) {
for (int j=1;j<=a[i];j++) {
b[++cnt]=t[i];
}
}
for (int i=1;i<=cnt;i++) {
for (int t=0;t<=1000;t++) s[t].p=0;
if (i==1) pre[0]=1;
for (int t=0;t<=1000;t++) {
for (int k=1;k<=b[i];k++) {
s[t+k].p+=pre[t]*1/(b[i]*1.0);
s[t+k].num=t+k;
}
}
for (int t=0;t<=1000;t++) pre[t]=s[t].p;
}
sort(s+1,s+1000+1,cmp);
for (int i=1;i<=1000;i++) {
if (s[i].p!=0) {
cout<<(int)s[i].num<<" ";
}
else break;
}
return 0;
}
5.C. Cosmic Commute
跑两次最短路即可,不述。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1000006;
int head[N],dis1[N],dis2[N],vis[N];
int inf,n,m,k,tot,w[N];
struct qwq{
int hext,to,w;
}e[N<<1];
void add(int x,int y) {
e[++tot].hext=head[x];
e[tot].to=y;
e[tot].w=1;
head[x]=tot;
}
priority_queue <pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
void dijk1(int s) {
for (int i=1;i<=n;i++) dis1[i]=inf,vis[i]=0;
dis1[s]=0;
q.push({(int)0,s});
while (!q.empty()) {
int u=q.top().second; q.pop();
if (vis[u]) continue;
vis[u]=1;
for (int i=head[u];i;i=e[i].hext) {
int v=e[i].to;
if (dis1[v]>dis1[u]+e[i].w) {
dis1[v]=dis1[u]+e[i].w;
q.push({dis1[v],v});
}
}
}
return ;
}
void dijk2(int s) {
for (int i=1;i<=n;i++) dis2[i]=inf,vis[i]=0;
dis2[s]=0;
q.push({(int)0,s});
while (!q.empty()) {
int u=q.top().second; q.pop();
if (vis[u]) continue;
vis[u]=1;
for (int i=head[u];i;i=e[i].hext) {
int v=e[i].to;
if (dis2[v]>dis2[u]+e[i].w) {
dis2[v]=dis2[u]+e[i].w;
q.push({dis2[v],v});
}
}
}
return ;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
inf=1e9+7; inf*=inf;
cin>>n>>m>>k;
for (int i=1;i<=k;i++) cin>>w[i];
for (int i=1;i<=m;i++) {
int x,y; cin>>x>>y;
add(x,y); add(y,x);
}
dijk1(1); dijk2(n);
int sum=0;
for (int i=1;i<=k;i++) {
sum+=dis2[w[i]];
}
int minn=dis1[n]*(k-1),kk=0;
for (int i=1;i<=k;i++) {
int t1=dis1[w[i]];
int t2=dis2[w[i]];
int t=t1*(k-1)+(sum-t2);
if (t<minn) {
minn=t;
kk=i;
}
}
if (kk==0) {
cout<<(minn/(k-1))<<"/1"<<endl;
return 0;
}
int t1=dis1[w[kk]];
int t2=dis2[w[kk]];
int c=(k-1)*t1+(sum-t2);
int d=k-1;
int g=__gcd(c,d);
c/=g,d/=g;
cout<<c<<"/"<<d<<endl;
return 0;
}
6.K. Kaldorian Knights
组合数学题。
VP列式子时一开始只考虑了
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1000006;
const int MOD=1e9+7;
int n,m,a[N],fac[N],b[N],dp[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>m;
for (int i=1;i<=m;i++) cin>>a[i];
fac[0]=1; for (int i=1;i<=n;i++) fac[i]=fac[i-1]*i%MOD;
for (int i=1;i<=m;i++) b[i]=b[i-1]+a[i];
for (int i=1;i<=m;i++) {
dp[i]=fac[b[i]];
for (int j=1;j<=i-1;j++) {
dp[i]=(dp[i]+MOD-fac[b[i]-b[j]]*dp[j]%MOD)%MOD;
}
}
int ans=fac[n];
for (int i=1;i<=m;i++) {
ans=(ans+MOD-fac[n-b[i]]*dp[i]%MOD)%MOD;
}
cout<<ans<<endl;
return 0;
}
7.F. Freestyle Masonry
几行代码的事,太神奇了……
还是要加一句,lyx是神orz!
-
-
一开始一直想着比如维护最后两行之类的方法,实际应该关注的是
h 相邻列的差值……
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1000006;
int n,H,h[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>H;
for (int i=1;i<=n;i++) cin>>h[i];
int pre=0;
for (int i=1;i<=n;i++) {
if (h[i]+pre>H) {
cout<<"impossible\n";
return 0;
}
if ((H-h[i]-pre)&1) pre++;
else pre=max(pre-1,(int)0);
}
if (pre) {
cout<<"impossible\n";
return 0;
}
cout<<"possible\n";
return 0;
}
8.H. Highway Combinatorics
-
可以观察到,对于一个2行
n 列的全空地,方案数即为fibo(n) 。 -
然后我们就要选出一些斐波那契数来,使得他们相乘为
n , 且他们作为斐波那契数的下标的和<=200 -
我们可以想到,把
n 拆成两个数,因为存一个 1e9+7 的map显然在时间空间上都不合理。这两个数分别可以为 2e6 (但是赛时一开始嘴的是1e5,实际不够)。然后我们用类似随机化选一下,然后合并。 -
————————以上是赛时想法
-
然后我们用随机化即可,随便构造
2e6 个集合(里面包含了下标和<=100 的一堆数),把它们合并起来。 -
注意
//!!!处,构造0 这一答案的时候,
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2000006;
const int MOD=1e9+7;
map<int,vector<int> > mp;
struct qwq{
int val,num;
}b[N];
int n;
int mi(int A,int B) {
int t=1;
while (B) {
if (B&1) t=t*A%MOD;
A=A*A%MOD;
B>>=1;
}
return t;
}
void pri(int k) {
vector <int> t=mp[k];
for (int i=0;i<(int)t.size();i++) {
for (int j=1;j<=t[i];j++) {
cout<<".";
}
cout<<"#";
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
mt19937 myrand(time(nullptr));
cin>>n;
if (n==0) {
cout<<".##"<<endl<<"##."<<endl; //!!!
return 0;
}
b[0].val=1,b[1].val=1;
for (int i=2;i<=100;i++) b[i].val=(b[i-1].val+b[i-2].val)%MOD;
for (int i=1;i<=100;i++) b[i].num=i;
int p=(int)2e6;
for (int ci=1;ci<=p;ci++) {
if (ci%10==0) shuffle(b+1,b+96+1,myrand);
int len=1,res=1;
vector<int> t; t.clear();
while (len<=95) {
int i=myrand()%95+1;
if (len+b[i].num>98&&b[i].num<=10) break;
if (len+b[i].num>98) continue;
t.push_back(b[i].num);
res=(res*b[i].val)%MOD;
len+=b[i].num+1;
}
mp[res]=t;
}
for (auto tem:mp) {
int u=tem.first;
int v=n*mi(u,MOD-2)%MOD;
if (mp.count(v)){
pri(u);
pri(v);
cout<<endl;
pri(u);
pri(v);
cout<<endl;
return 0;
}
}
return 0;
}
2025.7.3
The 2023 ICPC Asia Yokohama Regional Contest
1.B: # 7922. Rank Promotion
-
经过数学推导,可以得出,当
val[s-1]<=val[i] 时,可以更新。 -
更新条件的第二点:" The player was already at the current rank before starting the s-th quiz. " 其实在告诉我们,如果
i-1 的排名其实已经比s 之前的高了,那么这个更新就没什么意义了。所以我们知道,rank 是随时间递增的(其实用common sense也知道)(一开始用树状数组进行DP,浪费了很多机时,就是因为这一点/ll)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1000006;
const int m=500002;
int dp[N],a[N],b[N],val[N],tree[N],vall[N];
int n,c,p,q;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>c>>p>>q;
string st; cin>>st;
for (int i=1;i<=n;i++) {
if (st[i-1]=='Y') a[i]=1;
}
for (int i=1;i<=n;i++) b[i]=b[i-1]+a[i];
for (int i=1;i<=n;i++) val[i]=q*b[i]-p*i;
int inf=1e9+7,now=0,pre=0;
int minn=inf*inf;
if (n==1&&c==1&&p&&st[0]=='N') {
cout<<0<<endl;
return 0;
}
if (c==1) minn=val[1];
for (int i=1;i<=n;i++) {
if (val[i]>=minn) {
now++;
pre=i;
minn=inf*inf;
}
if (i+1-c>=pre) minn=min(minn,val[i+1-c]);
}
cout<<now<<endl;
return 0;
}
2.G: # 7927. Fortune Telling
-
这是队友推的式子:
-
-
一开始以为这个会TLE/MLE,后来发现,其实在一个固定的
x 下,可能的i 值有限,因此记忆化可过。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=300005;
const int MOD=998244353;
int n,inv[N];
unordered_map <int,int> dp[N];
int mi(int A,int B) {
int t=1;
while (B) {
if (B&1) t=t*A%MOD;
A=A*A%MOD;
B>>=1;
}
return t;
}
int js(int x,int i) {
if (x<=6) return inv[x];
if (dp[x].count(i)) return dp[x][i];
int res=0;
for (int t=1;t<=6;t++) {
if (i%6==t%6) continue;
int xx=x-(x-t)/6-1;
int ii;
if (i>=t) ii=i-(i-t)/6-1;
else ii=i;
int tmp=inv[6]*js(xx,ii)%MOD;
res=(res+tmp)%MOD;
}
dp[x][i]=res;
return dp[x][i];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for (int i=1;i<=6;i++) inv[i]=mi(i,MOD-2);
for (int i=1;i<=n;i++) {
cout<<js(n,i)<<endl;
}
return 0;
}
3.D: # 7924. Nested Repetition Compression
跑 区间DP 即可。
#include <bits/stdc++.h>
#define int long long
using namespace std;
string st,dp[233][233];
int n;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>st;
n=(int)st.size();
st=" "+st;
for (int l=1;l<=n;l++) {
for (int len=1;l+len-1<=n;len++) {
dp[l][l+len-1]=st.substr(l,len);
}
}
for (int len=2;len<=n;len++) {
for (int l=1;l+len-1<=n;l++) {
int r=l+len-1;
for (int k=l;k<=r-1;k++) {
if ((int)dp[l][k].size()+(int)dp[k+1][r].size()<(int)dp[l][r].size()) {
dp[l][r]=dp[l][k]+dp[k+1][r];
}
}
for (int k=2;k<=9;k++) {
if (len%k) continue;
if (st.substr(l,len/k*(k-1))==st.substr(l+len/k,len/k*(k-1))) {
if (3+(int)dp[l][l+len/k-1].size()<(int)dp[l][r].size()) {
dp[l][r]=(char(k+48));
dp[l][r]+="(";
dp[l][r]+=dp[l][l+len/k-1];
dp[l][r]+=")";
}
}
}
}
}
cout<<dp[1][n]<<endl;
return 0;
}
4.J: # 7930. Do It Yourself?
贪心……
要点是,想到这个贪心能过并且敢写emmm
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=500005;
int p[N],fa[N],f[N],dp[N],head[N];
int n,tot,ans;
struct qwq{
int hext,to;
}e[N<<1];
priority_queue <pair<int,int> > q[N];
void add(int x,int y) {
e[++tot].hext=head[x];
e[tot].to=y;
head[x]=tot;
}
void dfs(int u) {
p[u]=1;
for (int i=head[u];i;i=e[i].hext) {
int v=e[i].to;
dfs(v); dp[u]+=dp[v];
if ((int)q[v].size()>(int)q[u].size()) swap(q[u],q[v]);
while (!q[v].empty()) {
q[u].push(q[v].top());q[v].pop();
}
}
dp[u]+=f[u];
while (!q[u].empty()) {
pair<int,int>x=q[u].top();
int v=x.second;
int w=f[u]*(2*p[u]+1);
if (x.first<=w) break;
q[u].pop();
dp[u]-=x.first; dp[u]+=w;
p[v]--; p[u]++;
if (p[v]) q[u].push({f[v]*(p[v]*p[v]-(p[v]-1)*(p[v]-1)),v});
}
q[u].push({f[u]*(p[u]*p[u]-(p[u]-1)*(p[u]-1)),u});
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for (int i=2;i<=n;i++) {
cin>>fa[i];
add(fa[i],i);
}
for (int i=1;i<=n;i++) {
cin>>f[i];
}
dfs(1);
cout<<dp[1]<<'\n';
return 0;
}
5.F: # 7926. Color Inversion on a Huge Chessboard
-
我们可以知道,对于每一行,它的状态要么是第一行,要么是第一行的全取反;对列同理。
-
统计第一行和第一列的区域数,相乘即可。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=500005;
int n,q,ansx,ansy,ans,a[N],b[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>q;
for (int i=1;i<=n;i++) {
a[i]=b[i]=((i+1)&1);
}
ansx=n; ansy=n;
for (int qwq=1;qwq<=q;qwq++) {
string st; int x;
cin>>st>>x;
if (n==1) { //!!!
cout<<1<<'\n';
continue;
}
if (st[0]=='R') {
if (x==1) {
if (b[x]==b[x+1]) ansy++;
else ansy--;
}
else if (x==n) {
if (b[x]==b[x-1]) ansy++;
else ansy--;
}
else {
if (b[x-1]==b[x+1]) {
if (b[x]==b[x-1]) ansy+=2;
else ansy-=2;
}
}
b[x]=1-b[x];
}
else {
if (x==1) {
if (a[x]==a[x+1]) ansx++;
else ansx--;
}
else if (x==n) {
if (a[x]==a[x-1]) ansx++;
else ansx--;
}
else {
if (a[x-1]==a[x+1]) {
if (a[x]==a[x-1]) ansx+=2;
else ansx-=2;
}
}
a[x]=1-a[x];
}
ans=ansx*ansy;
cout<<ans<<'\n';
}
return 0;
}
6.I: # 7929. Liquid Distribution
官方tutorial
看了题解才想到,这题真的是太巧妙了……
- 记得考虑
i=0 的情况。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=200005;
int n,m;
struct qwq{
int a,b;
}x[N],y[N],p[N],q[N];
bool cmp(qwq xx,qwq yy) {
return xx.a*yy.b>yy.a*xx.b;
//xx.a/xx.b>yy.a/yy.b
}
signed main(){
cin>>n>>m;
for (int i=1;i<=n;i++) cin>>x[i].a;
for (int i=1;i<=n;i++) cin>>x[i].b;
for (int i=1;i<=m;i++) cin>>y[i].a;
for (int i=1;i<=m;i++) cin>>y[i].b;
sort(x+1,x+n+1,cmp);
sort(y+1,y+m+1,cmp);
for (int i=1;i<=n;i++) {
p[i].a=p[i-1].a+x[i].a;
p[i].b=p[i-1].b+x[i].b;
}
for (int i=1;i<=m;i++) {
q[i].a=q[i-1].a+y[i].a;
q[i].b=q[i-1].b+y[i].b;
}
bool tf=1;
for (int i=0;i<=n-1;i++) { //i=0
int xp=p[i].a,yp=p[i].b;
int xq=p[i+1].a,yq=p[i+1].b;
for (int j=1;j<=m;j++) {
int xr=q[j].a,yr=q[j].b;
if ((xq-xp)*(yr-yp)<(yq-yp)*(xr-xp)) {
tf=0;
}
}
}
if (tf) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
2025.7.4
P1407 [国家集训队] 稳定婚姻
大概是六年级的时候,随机跳题跳到了这道,题目背景相当有趣,就记了好久。
(从相册找到了原版题目背景 嘻嘻qwq)
- 夫妻关系:“女->男”,情人关系:“男->女”,跑一遍 tarjan ,判断每对夫妻是否在同一个强联通分量里即可。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=100005;
int n,m,tot,cnt,ans,totcol;
int col[N],low[N],dfn[N],head[N],num[N];
bool vis[N];
stack <int> s;
vector <int> p[N];
struct qwq{int hext,to,from;}e[N];
unordered_map <string,int> mp;
void add(int x,int y) {
e[++tot].hext=head[x];
e[tot].from=x;
e[tot].to=y;
head[x]=tot;
}
void paint(int x) {
s.pop();
col[x]=totcol;
p[totcol].push_back(x);
num[totcol]++;
vis[x]=0;
}
void tarjan(int x) {
dfn[x]=low[x]=++cnt;
s.push(x);
vis[x]=1;
for (int i=head[x];i;i=e[i].hext) {
int v=e[i].to;
if (!dfn[v]) {
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if (vis[v]) low[x]=min(low[x],low[v]);//dfn[v] is also right
}
if (low[x]==dfn[x]) {
++totcol;
while (s.top()!=x) {
int t=s.top(); paint(t);
}
paint(x);
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n; int p=0;
for (int i=1;i<=n;i++) {
string st1,st2; cin>>st1>>st2;
int x,y;
if (mp.count(st1)) x=mp[st1];
else x=++p,mp[st1]=p;
if (mp.count(st2)) y=mp[st2];
else y=++p,mp[st2]=p;
add(x,y);
}
cin>>m;
for (int i=1;i<=m;i++) {
string st1,st2; cin>>st1>>st2;
int x,y;
if (mp.count(st1)) x=mp[st1];
else x=++p,mp[st1]=p;
if (mp.count(st2)) y=mp[st2];
else y=++p,mp[st2]=p;
add(y,x);
}
for (int i=1;i<=p;i++) {
if (!dfn[i]) {
tarjan(i);
}
}
for (int i=1;i<=n;i++) {
if (col[e[i].from]==col[e[i].to]) {
cout<<"Unsafe"<<endl;
}
else cout<<"Safe"<<endl;
}
return 0;
}
2025.7.5
Codeforces Round 1035 (Div. 2)
A. Add or XOR
赛时 WA 了好几发,最后人红温了就去写了一个 dijk ……
要点:
//!!!处的运算符优先级问题,警钟敲烂!
#include <bits/stdc++.h>
#define int long long
using namespace std;
int T;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>T;
while (T--) {
int a,b,x,y;
cin>>a>>b>>x>>y;
if (a>b) {
if ((a xor 1)==b) {
cout<<y<<'\n';
}
else cout<<-1<<'\n';
continue;
}
if (a==b) {
cout<<0<<'\n';
continue;
}
//a<b
if (a+1==b) {
if ((a xor 1)==b) { //!!!
cout<<min(x,y)<<'\n';
}
else {
cout<<x<<'\n';
}
continue;
}
int ans=0;
for (int i=a;i<=b-1;i++) {
if (i+1==(i xor 1)) {
ans+=min(x,y);
}
else ans+=x;
}
cout<<ans<<'\n';
}
return 0;
}
B. Line Segments
#include <bits/stdc++.h>
#define int long long
using namespace std;
int T,n,px,py,qx,qy;
const int N=200005;
double a[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>T;
while (T--) {
cin>>n;
cin>>px>>py>>qx>>qy;;
for (int i=1;i<=n;i++) cin>>a[i];
double s=sqrt((qy-py)*(qy-py)+(qx-px)*(qx-px));
a[n+1]=s;
double sum=0;
sort(a+1,a+n+2);
for (int i=1;i<=n;i++) sum+=a[i];
if (a[n+1]>sum) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
return 0;
}
C. A Good Problem
一道构造题。
#include <bits/stdc++.h>
#define int long long
using namespace std;
int T,n,l,r,K,ans;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>T;
while (T--) {
cin>>n>>l>>r>>K;
if (l==0) {
cout<<0<<'\n';
continue;
}
if (n&1) {
cout<<l<<'\n';
continue;
}
if (n==2) {
cout<<-1<<'\n';
continue;
}
ans=0;
int k=-1,tf=0;
for (int i=62;i>=0;i--) {
int t=(1ll<<i);
if (t&l) {
break;
}
if ((t&r)||tf) k=i,tf=1;
}
if (k==-1) cout<<-1<<'\n';
else {
if (K<=n-2) cout<<l<<'\n';
else cout<<((int)(1ll<<k))<<'\n';
}
}
return 0;
}
2025.7.7
QOJ-9731 icpc2024杭州:Fuzzy Ranking
-
大学A比大学B在某排名中厉害,就连一条单向边。同时,我们知道,如果两所大学在同一个强连通分量,他们就是模糊对。
-
我们可以知道,在每个排行榜中,那些
col相同的大学,必然相邻。 -
前缀和统计答案即可,这个挺巧妙的,复习的时候可以再想想。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=200005;
int T,n,k,q,m,tot,cnt,ans,totcol;
int col[N],low[N],dfn[N],head[N],num[N];
bool vis[N];
stack <int> s;
vector <int> p[N];
vector <int> b[N],L[N],R[N],sum[N];
struct qwq{int hext,to;}e[N];
void add(int x,int y) {
e[++tot].hext=head[x];
e[tot].to=y;
head[x]=tot;
}
void paint(int x) {
s.pop();
col[x]=totcol;
p[totcol].push_back(x);
num[totcol]++;
vis[x]=0;
}
void tarjan(int x) {
dfn[x]=low[x]=++cnt;
s.push(x);
vis[x]=1;
for (int i=head[x];i;i=e[i].hext) {
int v=e[i].to;
if (!dfn[v]) {
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if (vis[v]) low[x]=min(low[x],low[v]);//dfn[v] is also right
}
if (low[x]==dfn[x]) {
++totcol;
while (s.top()!=x) {
int t=s.top(); paint(t);
}
paint(x);
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>T;
while (T--) {
cin>>n>>k>>q;
tot=totcol=cnt=0; for (int i=1;i<=n*k;i++) head[i]=0;
for (int i=1;i<=n;i++) col[i]=low[i]=dfn[i]=num[i]=vis[i]=0;
for (int i=1;i<=k;i++) {
b[i].clear();sum[i].clear();
L[i].clear();R[i].clear();
for (int j=0;j<=n+1;j++) {
b[i].push_back(0);
L[i].push_back(0);
R[i].push_back(0);
sum[i].push_back(0);
}
}
for (int i=1;i<=k;i++) {
int prex; cin>>prex; b[i][1]=prex;
for (int j=2;j<=n;j++) {
int x; cin>>x; b[i][j]=x;
add(prex,x);
prex=x;
}
}
for (int i=1;i<=n;i++) {
if (!dfn[i]) tarjan(i);
}
for (int i=1;i<=k;i++) {
for (int j=1;j<=n;j++) {
b[i][j]=col[b[i][j]];
}
}
for (int i=1;i<=k;i++) {
int t=0; L[i][1]=0;
for (int j=2;j<=n;j++) {
if (b[i][j]==b[i][j-1]) t++;
else t=0;
L[i][j]=t;
}
for (int j=1;j<=n;j++) {
sum[i][j]=sum[i][j-1]+L[i][j];
}
}
int pre=0;
while (q--) {
int id,l,r; cin>>id>>l>>r;
id=(id+pre)%k+1,l=(l+pre)%n+1,r=(r+pre)%n+1;
int ans=sum[id][r]-sum[id][l-1];
ans=(ans-L[id][l]*min(r-l+1,(num[b[id][l]]-L[id][l])));
pre=ans;
cout<<ans<<'\n';
}
}
return 0;
}