NOIP2014题解

Day 1 T1:完全模拟....

            T2:枚举中点+完全平方优化;

            T3:完全背包的思想;


Day 2 T1: 枚举...

           T2:反图bfs+正图spfa;

           T3:只会做暴力30分;




//Day 1 T1;
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
const int M=305;
int n, na,nb;
int a[M];
int b[M];
int map[6][6];
void init()
{
	scanf("%d %d %d", &n, &na, &nb);
	memset(map, 0, sizeof(map));
	for(int i=0; i<na; i++) scanf("%d", &a[i]);
	for(int i=0; i<nb; i++) scanf("%d", &b[i]); 
	map[0][0]=0;map[0][1]=0;map[0][2]=1;map[0][3]=1;map[0][4]=0;
	map[1][1]=0;map[1][2]=0;map[1][3]=1;map[1][4]=0;
	map[2][2]=0;map[2][3]=0;map[2][4]=1;
	map[3][3]=0;map[3][4]=1;map[4][4]=0;
	for(int i=0; i<=4; i++)
	 for(int j=0; j<=4; j++)
	 {
	 	if (map[i][j]==0 && i!=j)
	 	{
	 		map[j][i]=1;
	 	}
	 	else if (map[i][j]==1)
	 	{
	 		map[j][i]=0;
	 	}
	 }
}
void solve()
{
	int ta=-1;
	int tb=-1;
	int ansa=0;
	int ansb=0;
	for(int i=0; i<n; i++)
	{
		if (ta+1>na-1) ta=-1;
		if (tb+1>nb-1) tb=-1;
		ansa+=map[a[++ta]][b[++tb]];
		ansb+=map[b[tb]][a[ta]];
	}
	printf("%d %d\n", ansa, ansb);
}
int main()
{
	init();
	solve();

	return 0;
}

//Day 1 T2;
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
const int M=200005;
const int mod=10007;
struct linklist
{
	int s;
	linklist *next;
};
int n;
int val[M];
linklist t[M];
int map[M];
int lx[M];
int ans[M*4];
int js=0;
void debug()
{
	for(int i=1; i<=n; i++)
	{
		printf("%d ", i);
		for(linklist *j=t[i].next; j; j=j->next)
		printf("%d ", j->s);
		printf("\n");
	}
}
void insert(linklist &x, int y)
{
	linklist *t=new(linklist);
	t->s=y;
	t->next=x.next;
	x.next=t;
}
void solve()
{
    int maxx=0;
    long long sum=0;	
	int temp[M];
	for(int i=1; i<=n; i++)
	{
		int tot=0;
		if (lx[i]>=2)
		{
		   for(linklist*j=t[i].next; j; j=j->next) temp[++tot]=val[j->s];
		   sort(temp+1, temp+tot+1);
		   if (temp[tot]*temp[tot-1]>maxx) maxx=temp[tot]*temp[tot-1];
		   long long s=0;
		   long long t=0;
		   for(int j=1; j<=tot; j++)
		   {
		   	  s+=temp[j];
		   	  t+=temp[j]*temp[j];
		   }
		   sum+=(s*s-t);
	    }
    }
	sum=sum%mod;
	printf("%d %lld\n", maxx, sum);
}
void init()
{
	scanf("%d", &n);
	for(int i=1; i<=n-1; i++){
		int x, y;
		scanf("%d %d", &x, &y);
		insert(t[x], y);
		lx[x]++;
		insert(t[y], x);
		lx[y]++;
	}
	for(int i=1; i<=n; i++)
	{
		scanf("%d", &val[i]);
	}
}
int main()
{
	init();
	solve();
	return 0;
}

//Day 1 T3;
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int INF=0xfffffff;
const int N=10005;
const int M=1005;
struct tunnel
{
	int s;
	int x;
	int y;
};
tunnel p[N];

int n, m, k;
int f[N][M];
int up[N];
int down[N];
int pass=0;
bool flag=true;
void debug()
{
	for(int i=0; i<=n; i++)
	{
	   for(int j=0; j<=m; j++)
	   printf("%d ", f[i][j]);
	   printf("\n");
    }
    printf("--------------------------\n");
}
void do_up(int i)
{
	 for(int j=1; j<=m; j++)
	 if (j-up[i]>0) f[i][j]=min(f[i-1][j-up[i]]+1, f[i][j-up[i]]+1);
}
void do_down(int i)
{
	for(int j=1; j<=m; j++)
	if (j+down[i]<=m) f[i][j]=min(f[i][j], f[i-1][j+down[i]]);
}
void up_bound(int i)
{
	for(int j=1; j<=m; j++)
	{
	   if (j+up[i]>=m) f[i][m]=min(f[i][m],f[i-1][j]+1);
    }
    for(int j=1; j<=m; j++)
    {
    	if (j+up[i]>=m) f[i][m]=min(f[i][m], f[i][j]+1);
    }
    
}
void push_tunnel(int i)
{
	if (p[i].s)
	{
		for(int j=0; j<=p[i].x; j++) f[i][j]=INF;
		for(int j=m; j>=p[i].y; j--) f[i][j]=INF;
	}
}
bool check(int i)
{
	for(int j=1; j<=m; j++)
	if (f[i][j]<INF) {
		if (p[i].s) pass=p[i].s;
		//printf("%d ",pass);
	  return true;
    }
	flag=0;
	return false;
}
void solve()
{
	for(int i=1; i<=n; i++)
    {
    	do_up(i);	
    	do_down(i);
		up_bound(i);
		push_tunnel(i);
        if (!check(i)) return;
    }
    int ans=INF;
    for(int i=1; i<=m; i++) ans=min(ans, f[n][i]);
    printf("1\n%d\n", ans);
} 
bool cmp(tunnel a, tunnel b)
{
	return a.s<b.s;
}
void init()
{
	memset(p, 0, sizeof(p));
	scanf("%d %d %d", &n, &m, &k);
	for(int i=1; i<=n; i++) scanf("%d %d", &up[i], &down[i]);
	for(int i=1; i<=k; i++)
	{
		int s, up, down;
		scanf("%d %d %d", &s, &up, &down);
		p[s].x=up;
		p[s].s=1;
		p[s].y=down;
	}
	int tot=0;
	for(int i=1; i<=n; i++) if (p[i].s) p[i].s=++tot;
    for(int i=0; i<=n; i++) for(int j=0; j<=m; j++) f[i][j]=INF;
    for(int i=1; i<=m; i++) f[0][i]=0;
} 
int main()
{
	init();
	solve();
	//debug();
	if (!flag) printf("0\n%d\n", pass);
	return 0;
}

//Day 2 T1;
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<stack>
#include<string>
#include<queue>
#include<vector>
#include<string>
#include<functional>
using namespace std;
const int M=1000010;
int n, d;
struct node
{
	int x;
	int y;
	int val;
};
node map[M];
int f[130][130];
int ans=0;
bool cmp(node a, node b)
{
	if (a.x==b.x) return a.y<b.y;
	return a.x<b.x;
}
void solve()
{
	int co=1;
	int maxx=0;
	for(int i=0; i<=128; i++)
	  for(int j=0; j<=128; j++)
	  {
	  	 node temp;
	  	 temp.x=i;
	  	 temp.y=j;
	  	 temp.val=0;
	  	 for(int k=1; k<=n; k++)
	     {
	     	if (map[k].x>=temp.x-d && map[k].x<=temp.x+d)
	     	if (map[k].y>=temp.y-d && map[k].y<=temp.y+d)
			temp.val+=map[k].val;
	     }
	     f[i][j]=temp.val;
	  }
	for(int i=0; i<=128; i++)
	   for(int j=0; j<=128; j++)
	 {

	 	
	 	if (ans<f[i][j]) {
	 	  ans=f[i][j];
	 	  co=1;
	    }
	 	else if (ans==f[i][j])  co++;
	 }
	 printf("%d %d", co, ans);
}
void init()
{
	scanf("%d", &d);
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
	{
		scanf("%d %d %d", &map[i].x, &map[i].y, &map[i].val);
	}
	sort(map+1, map+n+1, cmp);
	memset(f, 0, sizeof(f));
}
int main()
{
	init();
	solve();
	return 0;
}

//Day 2 T2; 
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int INF=0xffffff;
const int M=100005;
struct linklist
{
	int s;
	linklist *next;
};
linklist ft[M];
linklist t[M];//It is used to saved the graph;
int link_ed[M];//It is used to check  if linked with  ed;
int n, m;
int st, ed;
int cost[M];
int ans=INF;
void debug()
{
	for(int i=1; i<=n; i++)
	{
		printf("%d ", cost[i]);
	}
}
void insert(linklist &x, int y)
{
	linklist *t=new(linklist);
	t->s=y;
	t->next=x.next;
	x.next=t;
}
void init()
{
	scanf("%d %d", &n, &m);
	for(int i=1; i<=n; i++)
	{
		t[i].next=NULL;
		t[i].s=i;
		ft[i].next=NULL;
		ft[i].s=i;
	}
	for(int i=1; i<=m; i++)
	{
		int x, y;
		scanf("%d %d", &x, &y);
		if (x==y) continue;
		insert(t[y], x);
		insert(ft[x], y);
		
	}
	scanf("%d %d", &st, &ed);
	memset(link_ed, 0, sizeof(link_ed));
}
void bfs_graph()
{
	int vis[M];
	memset(vis, 0, sizeof(vis));
	queue<int>que;
	que.push(ed);
	vis[ed]=1;
	while (!que.empty())
	{
		int v=que.front();
		que.pop();
		for(linklist *i=t[v].next; i; i=i->next)
		{
			int temp=i->s;
			if (!vis[temp])
			{
				link_ed[temp]=1;
				que.push(temp);
				vis[temp]=1;
			}
		}
	}
	link_ed[ed]=1;
}
bool check(int temp)
{
	if (!link_ed[temp]) return false;
	for(linklist *i=ft[temp].next; i; i=i->next)
	{
		int v=i->s;
		if (!link_ed[v] ) return false;
	}
	return true;
}
void spfa()
{
	queue<int>que;
	int vis[M];
	memset(vis, 0, sizeof(vis));
	for(int i=1; i<=n; i++) cost[i]=INF;
	cost[st]=0;
	que.push(st);
	while (!que.empty())
	{
		int v=que.front();
		que.pop();
		vis[v]=1;
		for(linklist *i=ft[v].next; i; i=i->next)
		{
			int temp=i->s;
			if (!vis[temp] && cost[v]+1<cost[temp] && check(temp))
			{
				que.push(temp);
				cost[temp]=cost[v]+1;
				vis[temp]=1;
			}
		}
		vis[v]=0;
	}
	ans=cost[ed];
}
void solve()
{
	bfs_graph();
	spfa();
	if (ans==INF) printf("-1");
	else printf("%d", ans);
}
int main()
{

	init();
	solve();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值