TEST2018.4.21 (Noip2015 improvement group Day1)

TEST2018.4.21 (Noip2015 improvement group Day1)

Offer benefits to the students of the class of 2019 lizw0520zzh! ! !

Title description (thanks to Luo Gu's father):   The magical magic square    information    transmission


T1 Magic Square:

The simulation questions are very simple, just simulate according to the description of the question, but there are a lot of judgment conditions, just be careful.

Use pos[i][0] to represent the number of rows of i, use pos[i][1] to represent the number of columns of i (this is easier to judge), mapp[][] records the magic square (mapp prevents the same name as map) .

Code (I actually tuned it for a long time at first?!):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=45;
int folder [N] [N];
int pos[N*N][2];
int n;

void print() {
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=n;j++)
			printf("%d ",mapp[i][j]);
		printf("\n");
	}
}

int main() {
	//freopen("magic.in","r",stdin);
	//freopen("magic.out","w",stdout);
	scanf("%d",&n);
	pos [1] [0] = 1; pos [1] [1] = (n+1)/2; mapp [pos [1] [0]] [pos [1] [1]] = 1;
	for(int i=2;i<=n*n;i++) {
		     if(pos[i-1][0]==1&&pos[i-1][1]!=n) pos[i][0]=n,pos[i][1]=pos[i-1][1]+1,mapp[pos[i][0]][pos[i][1]]=i;
		else if(pos[i-1][1]==n&&pos[i-1][0]!=1) pos[i][1]=1,pos[i][0]=pos[i-1][0]-1,mapp[pos[i][0]][pos[i][1]]=i;
		else if(pos[i-1][0]==1&&pos[i-1][1]==n) pos[i][1]=n,pos[i][0]=pos[i-1][0]+1,mapp[pos[i][0]][pos[i][1]]=i;
		else if(pos[i-1][0]!=1&&pos[i-1][1]!=n) {
			 if(!mapp[pos[i-1][0]-1][pos[i-1][1]+1]) pos[i][0]=pos[i-1][0]-1,pos[i][1]=pos[i-1][1]+1;
			 else pos[i][0]=pos[i-1][0]+1,pos[i][1]=pos[i-1][1];
			 mapp[pos[i][0]][pos[i][1]]=i;
		}
		//print();
	}
	print();
	return 0;
}


T2 information transfer:

The 2015 questions are really watery.

The core is to find the length of the smallest ring.

There are many ways to do this question. My idea is to delete the points with an in-degree of 0, and then run it directly through dfs. Other methods you can Baidu.

Friendly link: Shuiyou gay week's blog (standard practice)     By the way, Rancy is his girlfriend.

Code (the simplest universe):

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int N=200000+233;
int n,in[N],a[N];
bool fish [N];

bool del() {
	bool re=false;
	for(int i=1;i<=n;i++)
		if(!in[i]&&!vis[i])
			re=true,in[a[i]]--,a[i]=-1,vis[i]=true;
	return re;
}

int dfs(int now,const int o) {
	if(now==o&&vis[o]) return 0;
	vis[now]=true;
	return dfs(a[now],o)+1;
}

int main() {
	//freopen("message.in","r",stdin);
	//freopen("message.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++) {
		scanf("%d",a+i);
		in[a[i]]++;
	}
	int ans=N+10;
	while(del());
	for(int i=1;i<=n;i++)
		if (! vis [i]) ans = min (ans, dfs (i, i));
	printf("%d",ans);
	return 0;
}

Everyone is free to try and find out how to do it.


T3 Fighting Landlord:

I lost the landlord and won my life (the She Dao next door who won the landlord and lost my life has already died).

It is a simulation question (Noip2015 simulation questions are really many)

You can seriously think about the optimal strategy, and then the dfs straight will be fine. You can Baidu for the specific method.

I forgot to initialize ans with multiple sets of data and I cried and fainted in the toilet. . . my AK!

Code (in absolute conscience):

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define maxx(a,b) a>b?a:b
#define minn(a,b) a<b?a:b
using namespace std;
int T,n,y[20],years;

void init() {
	memset(a,0,sizeof(a));
	for(int num,opt,i=1;i<=n;i++) {
		scanf("%d%d",&num,&opt);
		if(num==0) a[14]++;
		if(num==1) a[12]++;
		if(num==2) a[13]++;
		if(num>=3) a[num-2]++;
	}
}

void solve(int x) {
	if(x>ans) return;
	int s1,s2,s3,s4;
	s1=s2=s3=s4=0;
	for(int i=1;i<=14;i++) {
		if(a[i]==1) s1++;
		if(a[i]==2) s2++;
	}
	for(int i=1;i<=14;i++) {
		if(a[i]==4) {
			s4++;
			if(s1>=2) s1-=2;
			else if(s2>=2) s2-=2;
			else if(s2>=1) s2-=1;
		}
	}
	for(int i=1;i<=14;i++) {
		if(a[i]==3) {
			s3++;
			if(s1>=1) s1-=1;
			else if(s2>=1) s2-=1;
		}
	}
	yrs=min(yrs,x+s1+s2+s3+s4);
	for(int i=1;i<=8;i++) {
		int j;
		for(j=i;j<=12;j++) {
			a[j]--;
			if(a[j]<0) break;
			if(j-i>=4) solve(x+1);
		}
		if(j==13) j--;
		while(j>=i) a[j--]++;
	}
	for(int i=1;i<=10;i++) {
		int j;
		for(j=i;j<=12;j++) {
			a[j]-=2;
			if(a[j]<0) break;
			if(j-i>=2) solve(x+1);
		}
		if(j==13) j--;
		while(j>=i) a[j--]+=2;
	}
	for(int i=1;i<=11;i++) {
		int j;
		for(j=i;j<=12;j++) {
			a[j]-=3;
			if(a[j]<0) break;
			if(j-i>=1) solve(x+1);
		}
		if(j==13) j--;
		while(j>=i) a[j--]+=3;
	}
}

int main() {
	//freopen("landlords.in","r",stdin);
	//freopen("landlords.out","w",stdout);
	scanf("%d%d",&T,&n);
	while(T--) {
		years=23;
		init();
		solve(0);
		printf("%d\n",ans);
	}
	return 0;
}
Finally, I wish everyone an early AK!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324801764&siteId=291194637