NOIP2018 DAY1

T1

  1. 2013年原题,考场上用了10min,100
  2. 就是单调上升区间的最大值减最小值,第一个单调上升区间例外,他的贡献是最大值
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;
ll read()
{
	char ch=' ';ll x=0,f=1;
	while(ch<'0'||ch>'9')
	{
		ch=getchar();if(ch=='-') f=-1;
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int main()
{
	//freopen("road.in","r",stdin);
	//freopen("road.out","w",stdout);
	ll n;
	n=read();
	ll ans=read();
	ll last=ans;
	for(int i=1;i<n;i++)
	{
		int x=read();
		if(x>last)
		{
			ans+=x-last;
		}
		last=x;
	}
	printf("%lld\n",ans);
	return 0;
}

T2

  1. 考场上发现了最终答案序列一定是给定序列的子集,被筛掉的数一定是由 a x 1 + b x 2 + c x 3 + d x 4 a*x1+b*x2+c*x3+d*x4 筛掉,所以就写了个BFS
  2. 正解就是dp,完全背包
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
inline int read(){
	char ch=' ';int f=1;int x=0;
	while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	return x*f;
}
const int M=25100;
int vis[M];
int a[110];
int main()
{
	int T=read();
	while(T--)
	{
		int n=read();
		int i,j;
		for(i=1;i<=n;i++)
		{
			a[i]=read();
		}
		sort(a+1,a+1+n);
		n=unique(a+1,a+1+n)-a-1;
		memset(vis,0,sizeof(vis));
		vis[0]=true;
		int ans=0;
		for(i=1;i<=n;i++)
		{
			if(!vis[a[i]])
			{
				ans++;
				for(j=0;j<=25000;j++)
				{
					if(j+a[i]<=25000&&vis[j])
					{
						vis[j+a[i]]=true;
					}
				}
			}
		}
		cout<<ans<<endl;
	}
}


考场BFS 80

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
using namespace std;
int read()
{
	char ch=' ';int x=0,f=1;
	while(ch<'0'||ch>'9')
	{
		ch=getchar();if(ch=='-') f=-1;
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
const int N=110;
const int M=25100;
int a[N];
int b[N],cnt;
bool vis[M];
bool inque[M];
int q[M],h,t;
int maxx=0;
void bfs()
{
	for(int i=1;i<=cnt;i++)
	{
		maxx=max(maxx,b[i]);
		q[++t]=b[i];
		vis[b[i]]=true;
		inque[b[i]]=true;
	}
	while(h<=t)
	{
		int x=q[h];
		int k=t;
		for(int i=h;i<=k;i++)
		{
			int tmp=x+q[i];
			if(tmp<=maxx)
			{
				if(vis[tmp]==true)
				{
					cnt--;
					vis[tmp]=false;
				}
				if(!inque[tmp])
				{
					q[++t]=tmp;
					inque[tmp]=true;
				}
			}
		}
		h++;
	}
}
void init()
{
	cnt=0;
	memset(vis,0,sizeof(vis));
	memset(inque,0,sizeof(inque));
	h=1;t=0;
	maxx=0;
}
int main()
{
	//freopen("money.in","r",stdin);
	//freopen("money.out","w",stdout);
	int T=read();int i,j;
	while(T--)
	{
		int n=read();
		init();
		for(i=1;i<=n;i++)
		a[i]=read();
		sort(a+1,a+1+n);
		for(i=1;i<=n;i++)
		{
			bool flag=true;
			for(j=1;j<i;j++)
			{
				if(a[i]%a[j]==0)
				{
					flag=false;
					break;
				}
			}
			if(flag)
			{
				b[++cnt]=a[i];
			}
		}
		bfs();
		printf("%d\n",cnt);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42110318/article/details/86675716
今日推荐