Comet OJ - Contest #7

传送门

\(A\)

咕咕咕

int main(){
	for(scanf("%d",&T);T;--T){
		scanf("%d%d",&l,&r);
		if(l==r)printf("%d %d %d %d\n",l,l,l,l);
		else printf("%lld %lld %lld %lld\n",1ll*r*(r-1),1ll*l,1ll*r,1ll);
//		printf("%lld %lld %lld %lld\n",l==r?1ll*l:1ll*r*(r-1),)
	}
	return 0;
}

\(B\)

分类讨论一下就行了

int main(){
	fp(i,1,4)scanf("%d",&a[i]);
	fp(i,1,4)if(a[i]==1){
		fp(j,1,i-1)a[4+j]=a[j];
		fp(j,1,4)a[j]=a[j+i-1];
	}
	if(a[2]==2&&a[3]==3&&a[4]==4)puts("0");
	else if(a[2]==4&&a[3]==3&&a[4]==2)puts("2");
	else puts("1");
	return 0;
}

\(C\)

我可能是个\(zz\)

枚举哪些数刚好处在不合法位置,然后计算此时的贡献,大力容斥即可

//quming
#include<bits/stdc++.h>
#define R register
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
typedef long long ll;
const int N=25,M=(1<<16)+5;
ll res,fac[N];int p[N],sz[M],vis[N],Pre[N],suf[N],n,T,lim;
ll calc(R int s){
	fp(i,0,n-1)if(s>>i&1)fp(j,i+1,n-1)if((s>>j&1)&&p[i]==p[j])return false;
	fp(i,0,n-1)if(s>>i&1)vis[p[i]]=1;
	fp(i,1,n)if(vis[i]){
		Pre[i]=suf[i]=0;
		fp(j,1,i-1)if(!vis[j])Pre[i]+=i-j;
		fp(j,i+1,n)if(!vis[j])suf[i]+=j-i;
	}
	ll res=0;int sum=0,ss=sz[s];
	fp(i,1,n)if(!vis[i])fp(j,i+1,n)if(!vis[j])sum+=j-i;
	fp(i,0,n-1)fp(j,i+1,n-1){
		if((s>>i&1^1)&&(s>>j&1^1))res+=fac[n-ss-2]*sum*(j-i);
		else if(s>>i&1^1)res+=fac[n-ss-1]*suf[p[j]]*(j-i);
		else if(s>>j&1^1)res+=fac[n-ss-1]*Pre[p[i]]*(j-i);
		else if(p[j]<p[i])res+=fac[n-ss]*(j-i)*(p[i]-p[j]);
	}
	fp(i,1,n)vis[i]=0;
	return res;
}
int main(){
//	freopen("testdata.in","r",stdin);
	fp(i,1,65535)sz[i]=sz[i>>1]+(i&1);
	fac[0]=1;fp(i,1,16)fac[i]=fac[i-1]*i;
	for(scanf("%lld",&T);T;--T){
		scanf("%d",&n),res=0,lim=(1<<n);
		fp(i,0,n-1)scanf("%d",&p[i]);
//		printf("%d\n",calc(3));
		fp(i,0,lim-1)(sz[i]&1)?res-=calc(i):res+=calc(i);
		printf("%lld\n",res);
	}
	return 0;
}

\(D\)

首先由两个结论

1.\(A,B\)肯定取到某个\(x\)或者\(min(x)-1\)或者\(max(x)+1\)

2.\(A<B\)

简单来说就是不这样取的话结果会变得不优

我们先把\(x\)离散并计算每个\(x\)时取\(A\)或者取\(B\)时对应\(y_i\)的贡献之和,分别记为\(Pre_i\)\(suf_i\)

然后我们枚举\(B\)的位置\(i\),那么如果\(A\)\(j(j<i)\),答案就是\(Pre_{j}+suf_i+(p_i-p_j)^2\),其中\(p_i\)表示离散化之前\(i\)对应的\(x\)的值

斜率优化即可

//quming
#include<bits/stdc++.h>
#define R register
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
typedef long long ll;
const int N=5e5+5;
ll Pre[N],suf[N],g[N],f[N],res=1e18;
int n,h,t,m,x[N],y[N],id[N],p[N],q[N],b[N];
inline int max(R int x,R int y){return x>y?x:y;}
inline int min(R int x,R int y){return x<y?x:y;}
inline double sl(R int i,R int j){return 1.0*(g[i]-g[j])/(p[i]-p[j]);}
inline ll sqr(R int x){return 1ll*x*x;}
int main(){
//	freopen("testdata.in","r",stdin);
	scanf("%d",&n);
	fp(i,1,n)scanf("%d%d",&x[i],&y[i]),b[i]=x[i];
	sort(b+1,b+1+n),m=unique(b+1,b+1+n)-b-1;
	p[1]=b[1]-1,p[m+2]=b[m]+1;fp(i,1,m)p[i+1]=b[i];
	for(R int i=1,k;i<=n;++i){
		k=lower_bound(b+1,b+1+m,x[i])-b;
		Pre[k+1]+=max(-y[i],0),suf[k+1]+=max(y[i],0);
	}
	n=m+2;fp(i,1,n)Pre[i]+=Pre[i-1];fd(i,n,1)suf[i]+=suf[i+1];
//	fp(i,1,n)printf("%d %d %lld %lld\n",i,p[i],Pre[i],suf[i]);
	fp(i,1,n)g[i]=Pre[i-1]+sqr(p[i]);
	h=1,t=0;
	fp(i,1,n){
		while(h<t&&sl(q[t-1],q[t])>=sl(q[t-1],i))--t;
		q[++t]=i;
		while(h<t&&sl(q[h],q[h+1])<=2*p[i])++h;
		f[i]=sqr(p[i]-p[q[h]])+Pre[q[h]-1]+suf[i+1];
		cmin(res,f[i]);
//		printf("%d %d %lld\n",i,q[h],f[i]);
//		fp(j,1,i-1)cmin(res,sqr(p[i]-p[j])+Pre[j-1]+suf[i+1]);
	}
	printf("%lld\n",res);
	return 0;
}

剩下两题太恐怖了咕了

posted @ 2019-07-15 08:43  源曲明  阅读(352)  评论(0编辑  收藏  举报