Educational Codeforces Round 95 题解

背景

T1出了大锅导致unrated了……

最后排名在70左右,如果不算橙名及以上的那些,那就排到第10左右了,小号错失一波上分的好机会QAQ……

A

算一下需要的总木棍数,然后就能算出交易次数,要注意精度问题,需要用long double。

代码如下:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define maxn 300010
#define ll long long

int T,n,m,k;

int main()
{
    
    
	scanf("%d",&T);while(T--)
	{
    
    
		scanf("%d %d %d",&n,&m,&k);
		ll ans=1ll*m*k+1ll*k;
		long double p=(long double)(ans-1)/(long double)(n-1);
		printf("%lld\n",(ll)ceil(p)+k);
	}
}

B

显然把能动的那些从大到小排序,大的尽量堆在前面,这样可以使每个前缀都尽可能的大。

代码如下:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define maxn 300010
#define ll long long

int T,n,a[maxn],l[maxn];
vector<int> b;
bool cmp(int x,int y){
    
    return x>y;}

int main()
{
    
    
	scanf("%d",&T);while(T--)
	{
    
    
		scanf("%d",&n);b.clear();
		for(int i=1;i<=n;i++)scanf("%d",&a[i]);
		for(int i=1;i<=n;i++){
    
    
			scanf("%d",&l[i]);
			if(!l[i])b.push_back(a[i]);
		}
		sort(b.begin(),b.end(),cmp);
		int st=0;
		for(int i=1;i<=n;i++){
    
    
			if(!l[i])a[i]=b[st++];
			printf("%d ",a[i]);
		}
		printf("\n");
	}
}

C

dp一下即可,令 f [ i ] [ 0 / 1 ] f[i][0/1] f[i][0/1] 表示干掉了前 i i i boss \text{boss} boss,此时是谁操作。

代码如下:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define maxn 300010
#define ll long long

int T,n,a[maxn],f[maxn][2];//0表示我操作,1表示朋友操作

int main()
{
    
    
	scanf("%d",&T);while(T--)
	{
    
    
		scanf("%d",&n);
		for(int i=1;i<=n;i++)scanf("%d",&a[i]),f[i][0]=f[i][1]=1e9;
		f[0][0]=0;f[0][1]=1e9;
		f[1][1]=a[1];
		for(int i=2;i<=n;i++){
    
    
			f[i][0]=min(f[i-1][1],f[i-2][1]);
			f[i][1]=min(f[i-1][0]+a[i],f[i-2][0]+a[i]+a[i-1]);
		}
		printf("%d\n",min(f[n][0],f[n][1]));
	}
}

D

显然最后保留相邻的最远的一对垃圾堆,其他垃圾都要堆过来,用 s e t set set 啥的维护一下就好了。

代码如下;

#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
#define maxn 300010
#define ll long long
#define it set<int>::iterator
#define it2 map<int,int>::iterator

int T,n,q;
set<int> s;//维护所有垃圾
map<int,int> ans;//维护相邻垃圾的坐标差
void add(int x){
    
    ans[x]++;}
void del(int x){
    
    ans[x]--;if(!ans[x])ans.erase(x);}
void getans(){
    
    
	if(s.size()<=2)printf("0\n");
	else{
    
    
		it p=s.end(),p1=s.begin();
		it2 p2=ans.end();p--;p2--;
		printf("%d\n",*p-*p1-p2->first);
	}
}

int main()
{
    
    
//	scanf("%d",&T);while(T--)
	{
    
    
		scanf("%d %d",&n,&q);s.clear();ans.clear();
		for(int i=1,x;i<=n;i++)scanf("%d",&x),s.insert(x);
		int last=0;for(int i:s){
    
    
			if(last)ans[i-last]++;
			last=i;
		}
		getans();
		for(int i=1;i<=q;i++){
    
    
			int id,x;
			scanf("%d %d",&id,&x);
			if(id==0){
    
    
				it p=s.lower_bound(x),p1=p;
				bool v=false,v1=false;
				if(p!=s.begin())p--,del(x-*p),v=true;
				if(p1!=s.end())p1++,del(*p1-x),v1=true;
				if(v&&v1)add(*p1-*p);
				s.erase(x);
			}else{
    
    
				s.insert(x);
				it p=s.lower_bound(x),p1=p;
				bool v=false,v1=false;
				if(p!=s.begin())p--,add(x-*p),v=true;
				if(p1!=s.end())p1++,add(*p1-x),v1=true;
				if(v&&v1)del(*p1-*p);
			}
			getans();
		}
	}
}

E

对于每个盾牌,怪物分成两类:攻击力大于等于其防御值的,攻击力小于其防御值的。

a a a 为第一类怪物数量, b b b 为第二类怪物数量,盾牌耐久为 p p p

考虑一只第一类怪物能造成伤害的情况数,如果 a ≤ p a\leq p ap 则为 0 0 0,否则为:
C a + b a C a − 1 p p ! b ! ( a − p ) ! C_{a+b}^a C_{a-1}^p p! b! (a-p)! Ca+baCa1pp!b!(ap)!

C a + b a C_{a+b}^a Ca+ba 是在序列中给两类怪物分配位置, C a − 1 p C_{a-1}^p Ca1p 是从除了自己以外的第一类怪物中找 p p p 个放在自己前面,这样自己才能造成伤害,剩下的阶乘就是表示不在乎相对位置的怪物随便排列。

考虑一只第二类怪物能造成伤害的情况数,如果 a < p a<p a<p 则为 0 0 0,否则为:
C a + b b − 1 ( b − 1 ) ! a ! ( a − p + 1 ) C_{a+b}^{b-1}(b-1)!a!(a-p+1) Ca+bb1(b1)!a!(ap+1)

C a + b b − 1 C_{a+b}^{b-1} Ca+bb1 表示给除了自己以外的第二类怪物分配位置,而自己前面一定要有至少 p p p 个第一类怪物,所以剩下的 a + 1 a+1 a+1 个位置里面只有 a + 1 − p a+1-p a+1p 个位置能放,然后阶乘依然表示不在乎相对顺序的怪物随便放。

注意上面求出来的是方案数,要乘上伤害再除以总方案数才是期望造成的伤害。

代码如下:

#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
#define maxn 300010
#define mod 998244353

int n,m,d[maxn];
struct node{
    
    int x,y,pos;}a[maxn];
bool cmp(node x,node y){
    
    return x.y<y.y;}
int fac[maxn],inv[maxn],inv_fac[maxn];
void work(){
    
    
	fac[0]=inv_fac[0]=inv[1]=1;
	for(int i=1;i<=n;i++)fac[i]=1ll*fac[i-1]*i%mod;
	for(int i=2;i<=n;i++)inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
	for(int i=1;i<=n;i++)inv_fac[i]=1ll*inv_fac[i-1]*inv[i]%mod;
}
int C(int x,int y){
    
    return 1ll*fac[x]*inv_fac[y]%mod*inv_fac[x-y]%mod;}
int sum1=0,sum2=0,ans,Ans[maxn];

int main()
{
    
    
	scanf("%d %d",&n,&m);work();
	for(int i=1;i<=n;i++)scanf("%d",&d[i]),sum1=(sum1+d[i])%mod;
	sort(d+1,d+n+1);
	for(int i=1;i<=m;i++)scanf("%d %d",&a[i].x,&a[i].y),a[i].pos=i;
	sort(a+1,a+m+1,cmp);
	int now=0;
	for(int i=1;i<=m;i++){
    
    
		while(d[now+1]<a[i].y)now++,sum2=(sum2+d[now])%mod;
		ans=0;
		if(a[i].x<=n-now)ans=(ans+1ll*C(n,now-1)*fac[now-1]%mod*fac[n-now]%mod*(n-now-a[i].x+1)%mod*sum2%mod)%mod;
		if(a[i].x<n-now)ans=(ans+1ll*C(n,now)*C(n-now-1,a[i].x)%mod*fac[a[i].x]%mod*fac[n-now-a[i].x]%mod*fac[now]%mod*(sum1-sum2+mod)%mod)%mod;
		Ans[a[i].pos]=1ll*ans*inv_fac[n]%mod;
	}
	for(int i=1;i<=m;i++)printf("%d\n",Ans[i]);
}

F

要满足 x 1 y 1 = x 2 y 2 x_1y_1=x_2y_2 x1y1=x2y2,相当于 x 2 x 1 = y 1 y 2 = a b \dfrac {x_2} {x_1}=\dfrac {y_1} {y_2}=\dfrac a b x1x2=y2y1=ba a , b a,b a,b 是设出来的一个东西,并且满足 a > b a>b a>b

还要满足 l ≤ x 1 y 1 ≤ r l\leq x_1y_1\leq r lx1y1r,即 ⌈ l x 1 ⌉ ≤ y 1 ≤ ⌊ r x 1 ⌋ \lceil \dfrac l {x_1} \rceil \leq y_1\leq \lfloor \dfrac r {x_1} \rfloor x1ly1x1r,随着 x 1 x_1 x1 的减小这个区间的左右端点都会变大,上限是 m m m,记这个区间为 [ L , R ] [L,R] [L,R]

先枚举 x 1 x_1 x1,然后考虑枚举 x 1 x_1 x1 的一个因子 b b b,问题变成:找到一个 y 1 ∈ [ L , R ] y_1\in[L,R] y1[L,R],并且拥有一个大于 b b b 的因子 a a a,并且 x 2 = x 1 a b ≤ n x_2=x_1\dfrac a b\leq n x2=x1ban。假如我们把 [ L , R ] [L,R] [L,R] 内的所有数的所有因子放到权值线段树里面,那么就是要找到大于 b b b 的一个最小的 a a a,然后找到这个 a a a 对应的 y 1 y_1 y1,有了 x 1 , y 1 , a , b x_1,y_1,a,b x1,y1,a,b,就可以求出 x 2 , y 2 x_2,y_2 x2,y2,最后判一下 x 2 x_2 x2 是否小于等于 n n n,如果是就找到一组可行解了。

[ 1 , n ] [1,n] [1,n] 的总因子数为 n ln ⁡ n n\ln n nlnn,所以总复杂度大约就是 n log ⁡ 2 n n\log^2 n nlog2n。(虽然题解也是这个复杂度并且也用到了线段树,但是蒟蒻并没有看懂题解在说什么,这个做法是蒟蒻瞎yy的qwq……)

代码如下:

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define maxn 200010
#define ll long long

int n,m;
ll L,R;
vector<int> fac[maxn];
void work(){
    
    
	for(int i=1;i<=maxn-10;i++){
    
    
		for(int j=i;j<=maxn-10;j+=i)
		fac[j].push_back(i);
	}
}
struct par{
    
    int x,y;};
#define zuo ch[0]
#define you ch[1]
struct node{
    
    
	int l,r,mid,c,num;node *ch[2];
	node(int x,int y):l(x),r(y),mid(l+r>>1),c(0){
    
    
		if(x<y){
    
    
			zuo=new node(l,mid);
			you=new node(mid+1,r);
		}else zuo=you=NULL;
	}
	void change(int x,int y,int z){
    
    
		c+=y;if(l==r){
    
    if(y==1)num=z;return;}
		ch[x>=mid+1]->change(x,y,z);
	}
	int getsum(int x,int y){
    
    
		if(l==x&&r==y)return c;
		if(y<=mid)return zuo->getsum(x,y);
		else if(x>=mid+1)return you->getsum(x,y);
		else return zuo->getsum(x,mid)+you->getsum(mid+1,y);
	}
	par ask(int x){
    
    //找到大于等于x的最小的a
		if(!c)return (par){
    
    0,0};
		if(l==r)return (par){
    
    l,num};
		if(x<=mid&&zuo->getsum(x,mid))return zuo->ask(x);
		return you->ask(max(mid+1,x));
	}
}*root=NULL;
struct ANS{
    
    int x_1,y_1,x_2,y_2;}ans[maxn];

int main()
{
    
    
	scanf("%d %d %lld %lld",&n,&m,&L,&R);work();
	int l=1,r=0;root=new node(1,m);
	for(int x_1=n;x_1>=1;x_1--){
    
    
		ll lto=(ll)ceil(1.0*L/x_1),rto=(ll)floor(1.0*R/x_1);
		while(r<m&&r<rto){
    
    
			for(int i:fac[r+1])root->change(i,1,r+1);
			r++;
		}
		while(l<=m&&l<lto){
    
    
			for(int i:fac[l])root->change(i,-1,l);
			l++;
		}
		ans[x_1].x_1=-1;
		for(int b:fac[x_1])if(b+1<=m){
    
    //注意这里,a要大于b但不能大于m
			par p=root->ask(b+1);//这里的+1将“找到大于的”变成了“找到大于等于的”
			if(!p.x)continue;
			int a=p.x,y_1=p.y,x_2=1ll*a*x_1/b,y_2=1ll*y_1*b/a;
			if(1ll*a*x_1/b<=n){
    
    ans[x_1]=(ANS){
    
    x_1,y_1,x_2,y_2};break;}
		}
	}
	for(int i=1;i<=n;i++)if(ans[i].x_1==-1)printf("-1\n");
	else printf("%d %d %d %d\n",ans[i].x_1,ans[i].y_1,ans[i].x_2,ans[i].y_2);
}

G

考虑维护一个 o c c [ i ] occ[i] occ[i] 表示当前前缀中数字 i i i 的出现次数模 3 3 3 的值,如果一个区间 [ l , r ] [l,r] [l,r] 是合法的,那么 1 1 1 ~ l − 1 l-1 l1 这个前缀的 o c c occ occ 数组和 1 1 1 ~ r r r 这个前缀的 o c c occ occ 数组肯定一样,这个可以用哈希来判断。

但是对于一个 r r r,并不是所有的拥有相同 o c c occ occ l − 1 l-1 l1 都合法,有些数可能出现了 6 6 6 次或以上,这个可以通过维护一个左端点 s t st st 来解决, s t st st 表示 l − 1 l-1 l1 最左能取到什么位置,再往左取就会有某个数出现超过 3 3 3 次,然后每次 s t st st 右移的时候把原位置的 h a s h hash hash 值删掉即可。

代码如下:

#include <cstdio>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
#define maxn 500010
#define ll long long

int n,occ[maxn];
ll val[maxn],ha[maxn],ans=0;
map<ll,int> mp;//存每个哈希值的出现次数
queue<int> q[maxn];

int main()
{
    
    
	scanf("%d",&n);srand(9904);
	for(int i=1;i<=n;i++)val[i]=((ll)rand()<<30)+(rand()<<16)+rand();
	int st=0;mp[0ll]++;
	for(int i=1,x;i<=n;i++){
    
    
		scanf("%d",&x);
		if(q[x].size()==3){
    
    
			int to=q[x].front();q[x].pop();
			while(st<to)mp[ha[st++]]--;
		}
		q[x].push(i);
		ha[i]=ha[i-1]-occ[x]*val[x];
		occ[x]=(occ[x]+1)%3;
		ha[i]+=occ[x]*val[x];
		ans+=mp[ha[i]];mp[ha[i]]++;
	}
	printf("%lld",ans);
}

猜你喜欢

转载自blog.csdn.net/a_forever_dream/article/details/108599981
今日推荐