2018.9.11模拟赛

算是模拟赛Day2吧

T1 动态仙人掌(其实只是个吓唬人的名字)

正解:排序+贪心

其实很简单但是被题目吓到了就没有写

血亏啊血亏

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define maxn 300005
using namespace std;
int n;
double ans;

inline int rd(){
    int X=0;int w=0;char ch=0;
    while(!isdigit(ch))w|=ch=='-',ch=getchar();
    while( isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}

struct qwq{
	double p,h;
	bool operator <(const qwq &x) const {
		return p-h<x.p-x.h||(p-h==x.p-x.h&&p+h<x.p+x.h);
	}
}a[maxn];

int main(){
	freopen("dinosaur.in","r",stdin);
	freopen("dinosaur.out","w",stdout);
	n=rd();
	for(int i=1;i<=n;i++) a[i].p=rd(),a[i].h=rd();
	sort(a+1,a+n+1);
	double prex=-1,prey=-1;
	for(int i=1;i<=n;i++){
		double x=a[i].p-a[i].h,y=a[i].p+a[i].h;
		if(x<0) return cout<<-1,0;
		if(x>=prey){
			ans=max(ans,(prey-prex)/2.0);
			prex=x, prey=y;
		}
		prey=max(prey,y);
	}
	ans=max(ans,(prey-prex)/2.0);
	printf("%.1lf",ans);
	return 0;
}

样例输入
样例输入 1

8

1 2

1 3

2 4

2 5

5 6

5 7

3 8

4

2 5 4 3

5 3 8 8

5 4 6 7

4 8 6 7
样例输入 2

15

2 1

3 1

4 2 

5 3

6 2

7 2

8 5

9 3

10 6

11 5

12 7

13 11

14 1

15 1

5

1 2 3 4

4 7 1 9

2 3 7 9

2 6 7 8

2 1 6 8

 


样例输出
样例输出 1

YES
NO

YES

NO
样例输出 2

YES

NO

YES

YES

YES
数据规模
对于 30%的数据 1<=n,q<=3000

对于另外 20%的数据 第 i 条边连接第 i 个巢穴和第 i+个巢穴

对于 100%的数据 1<=n,q<=100000

 

裸LCA···考场AC

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define maxn 100005
using namespace std;
int n,q,cnt,head[maxn],c[maxn],dep[maxn],f[maxn][20];

inline int rd(){
    int X=0;int w=0;char ch=0;
    while(!isdigit(ch))w|=ch=='-',ch=getchar();
    while( isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}

struct EDGE{
	int to,nxt;
}edge[maxn<<1];

inline void add(int x,int y){
	edge[++cnt].to=y; edge[cnt].nxt=head[x]; head[x]=cnt;
}

inline void dfs(int u,int fa){
	for(int i=head[u];i;i=edge[i].nxt){
		int v=edge[i].to; if(v==fa) continue;
		dep[v]=dep[u]+1; f[v][0]=u;
		for(int j=1;j<=18;j++)
			f[v][j]=f[f[v][j-1]][j-1];
		dfs(v,u);
	} return;
}

inline int LCA(int x,int y){
	if(dep[x]<dep[y]) swap(x,y);
	for(int j=18;j>=0;j--)
		if(dep[f[x][j]]>=dep[y]) x=f[x][j];
	if(x==y) return x;
	for(int j=18;j>=0;j--){
		if(f[x][j]!=f[y][j])
			x=f[x][j],y=f[y][j];
	}
	return f[x][0];
}

int main(){
	freopen("inter.in","r",stdin);
	freopen("inter.out","w",stdout);
	n=rd();
	for(int i=1;i<n;i++){
		int x=rd(),y=rd();
		add(x,y); add(y,x);
	}
	dep[1]=1;
	dfs(1,0);
	q=rd();
	while(q--){
		int a=rd(),b=rd(),c=rd(),d=rd();
		int x=LCA(a,b),y=LCA(c,d);
		if((LCA(a,y)==y || LCA(b,y)==y) && (LCA(c,x)==x || LCA(d,x)==x)){
			puts("YES");
		}
		else puts("NO");
	}
	return 0;
}

附第一个输入样例:

4
6 6 1 144 
6 2 144 144 
4 2 144 4 
12 12 4 4 

输出:

1
2 3 1 4
1 2 4 3
4 1 3 2
3 4 2 1

 

考场上看到想,这不就是dfs嘛!于是写了个纯纯的暴力:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<cmath>
#include<vector>
#define maxn 100005
using namespace std;
int n,a[15][15],ans[15][15],cnt,mul[100],num,bel[15][15],tmp[15][15];
bool used[15][15][2],vis[15][15];//0行 1列

inline int rd(){
    int X=0;int w=0;char ch=0;
    while(!isdigit(ch))w|=ch=='-',ch=getchar();
    while( isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}

inline void pre(int row,int lin){
	int x=bel[row][lin]; vis[row][lin]=1;
	if(lin>1 && a[row][lin-1]==a[row][lin]) {
		bel[row][lin-1]=x;
		if(!vis[row][lin-1]) pre(row,lin-1);
	}
	if(lin<n && a[row][lin+1]==a[row][lin]) {
		bel[row][lin+1]=x;
		if(!vis[row][lin+1]) pre(row,lin+1);
	}
	if(row>1 && a[row-1][lin]==a[row][lin]) {
		bel[row-1][lin]=x;	
		if(!vis[row-1][lin]) pre(row-1,lin);
	}
	if(row<n && a[row+1][lin]==a[row][lin]) {
		bel[row+1][lin]=x;
		if(!vis[row+1][lin]) pre(row+1,lin);
	} return;
}

void print(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			ans[i][j]=tmp[i][j];
}

inline void dfs(int row,int lin){
	if(row==n && lin==n+1){
		bool flg=true;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
//				cout<<mul[bel[i][j]]<<" ";
				if(mul[bel[i][j]]!=a[i][j]) {
					flg=false; break;
				}
			}
		}
			
		if(flg) cnt++;
		if(cnt==1 && flg) print();
		return;
	}
	for(int i=1;i<=n;i++)
		if(!used[row][i][0] && !used[lin][i][1] && a[row][lin]%i==0 && mul[bel[row][lin]]*i<=a[row][lin]){
			used[row][i][0]=1; used[lin][i][1]=1;
			int tx,ty;
			if(lin<n && row<=n) tx=row,ty=lin+1;
			else if(lin>=n && row<n) tx=row+1,ty=1;
			else tx=row,ty=lin+1;
			tmp[row][lin]=i; mul[bel[row][lin]]*=i;
//			cout<<row<<" "<<lin<<" "<<mul[bel[row][lin]]<<endl;
			dfs(tx,ty);
			tmp[row][lin]=0; mul[bel[row][lin]]/=i;
			used[row][i][0]=0; used[lin][i][1]=0;
		}
	return;
}

int main(){
	freopen("kenken.in","r",stdin);
	freopen("kenken.out","w",stdout);
	n=rd();
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			a[i][j]=rd();
		}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(!vis[i][j]){
				bel[i][j]=++num; pre(i,j);
			}
	for(int i=1;i<=num;i++) mul[i]=1;
	dfs(1,1);
	printf("%d\n",cnt);
	if(cnt>0)
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++) printf("%d ",ans[i][j]);
			printf("\n");
		}
	return 0;
}

又长又丑···但其实暴力复杂度应该是 9^2*9!*一个挺大的常数

然后就T了,40分,不过优化优化应该是70

正解有一个非常强的剪枝:先搜乘积小的再搜乘积大的

但是写起来就比较考验码力了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#define N 15
#define re register
using namespace std;
int n,a[N][N],num,acnt,ans[N][N],tmp[N][N];
int dr[5]={0,0,1,-1};
int dl[5]={1,-1,0,0};
bool vis[N][N],used[N][N][2];

inline int rd(){
    int X=0;int w=0;char ch=0;
    while(!isdigit(ch))w|=ch=='-',ch=getchar();
    while( isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}

struct qwq{
	int ml,cnt,c[82][2];
	qwq(){
		ml=0,cnt=0; memset(c,0,sizeof c);
	}
	bool operator <(const qwq &x) const {
		return ml<x.ml;
	}
}aim[82];

inline void pre(int x,int y,int now){
	vis[x][y]=1;
	for(re int i=0;i<4;i++){
		int tx=x+dr[i],ty=y+dl[i];
		if(!vis[tx][ty] && tx>0 && tx<=n && ty>0 && ty<=n && a[tx][ty]==a[x][y]){
			aim[now].c[++aim[now].cnt][0]=tx;
			aim[now].c[aim[now].cnt][1]=ty;
			pre(tx,ty,now);
		}
	} return;
}

inline void print(){
	for(re int i=1;i<=n;i++)
			for(re int j=1;j<=n;j++)
				ans[i][j]=tmp[i][j];
}

inline void compar(){
	if(acnt==1) print();
	else{
		int flg=0;
		for(re int i=1;i<=n;i++){
			if(flg) break;
			for(re int j=1;j<=n;j++){
				if(ans[i][j]<tmp[i][j]) {flg=1;break;}
				if(ans[i][j]>tmp[i][j]) {flg=2;break;}
			}
		}
		if(flg==2) print();
	}
}

inline void dfs(int now,int t,int mul){
	if(now==num+1){
		acnt++; compar();
		return;
	}
	int val=aim[now].ml,x=aim[now].c[t][0],y=aim[now].c[t][1];
//	cout<<now<<" "<<t<<" "<<x<<" "<<y<<" "<<val<<" "<<mul<<endl;
	for(re int i=1;i<=n&&i*mul<=val;i++)
		if(!used[x][i][0] && !used[y][i][1] && val%i==0){
			if(t==aim[now].cnt && mul*i!=val) continue;
			tmp[x][y]=i; used[x][i][0]=1; used[y][i][1]=1;
			if(t==aim[now].cnt) dfs(now+1,1,1);
			else dfs(now,t+1,mul*i);
			tmp[x][y]=0; used[x][i][0]=0; used[y][i][1]=0;
		} return;
}

int main(){
	freopen("kenken.in","r",stdin);
	freopen("kenken.out","w",stdout);
	n=rd();
	for(re int i=1;i<=n;i++)
		for(re int j=1;j<=n;j++)
			a[i][j]=rd();
	for(re int i=1;i<=n;i++)
		for(re int j=1;j<=n;j++)
			if(!vis[i][j]){
				aim[++num].c[++aim[num].cnt][0]=i;
				aim[num].c[aim[num].cnt][1]=j; aim[num].ml=a[i][j];
				pre(i,j,num);
			}
	sort(aim+1,aim+num+1);
	dfs(1,1,1); 
	printf("%d\n",acnt);
	for(re int i=1;i<=n;i++){
		for(re int j=1;j<=n;j++) printf("%d ",ans[i][j]);
		printf("\n");
	} 
	return 0;
}

然而!!这玩意可以用DLXA掉!而且跑的非常非常非常非常快

qwq但是感觉noip不会用到···附个讲的好的博客

https://blog.csdn.net/MasterLuo/article/details/4575420

 

Day2 总共0+100+40=140 我好菜···

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值