NOIP2015补坑完毕

day1t1略

day1t2

不知道为什么洛谷上AC了而我觉得是有反例的

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iomanip>
#define LL long long
#define PAU putchar(' ')
#define ENT putchar('\n')
#define fo(_i,_a,_b) for (int _i=_a;_i<=_b;_i++)
#define down(_i,_a,_b) for (int _i=_a;_i>=_b;_i--)
#define efo(_i,_a) for (int _i=last[_a];_i!=0;_i=e[_i].next)
#define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
#define end system("pause")
using namespace std;
LL read()
{
         LL f=1,d=0;char s=getchar();
         while (s<'0'||s>'9'){if (s=='-') f=-1;s=getchar();}
         while (s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
         return f*d;
}
LL readln()
{
       LL f=1,d=0;char s=getchar();
       while (s<'0'||s>'9'){if (s=='-') f=-1;s=getchar();}
       while (s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
       while (s!='\n') s=getchar();
       return f*d;
}
inline void write(LL x)
{
    if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    int len=0,buf[20];while(x)buf[len++]=x%10,x/=10;
    for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
}
inline void writeln(LL x){write(x);ENT;}
#define N 200005
int a[N],from[N],v[N];
int n,m,t,anss;

void dfs(int x,int T)
{
	if (v[x]&&v[x]!=T) return;
	if (v[x]&&v[x]==T) 
	{
		t=x;
		return;
	}
	v[x]=T;
	dfs(a[x],T);
	if (t!=0&&x!=t) anss++;
	if (x==t) t=0;
}

int main()
{
	n=read();
	fo(i,1,n) 
	{
		a[i]=read();
		from[a[i]]++;
	}
	memset(v,0,sizeof(v));
	int zn=0,ans=1000000;
	fo(i,1,n)
	if (from[i]==0)
	{
		anss=0;t=0;
		dfs(i,++zn);
		if (anss!=0) ans=min(ans,anss);
	}
	cout<<ans+1<<endl;
	return 0;
}

day1t3 DFS 

终于有时间了,自己补坑,程序在判断3带1的时候顺便把炸弹做了【虽然根本没什么用还浪费时间

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iomanip>
#define LL long long
#define PAU putchar(' ')
#define ENT putchar('\n')
#define fo(_i,_a,_b) for (int _i=_a;_i<=_b;_i++)
#define down(_i,_a,_b) for (int _i=_a;_i>=_b;_i--)
#define efo(_i,_a) for (int _i=last[_a];_i!=0;_i=e[_i].next)
#define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
#define end system("pause")
using namespace std;
LL read()
{
         LL f=1,d=0;char s=getchar();
         while (s<'0'||s>'9'){if (s=='-') f=-1;s=getchar();}
         while (s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
         return f*d;
}
LL readln()
{
       LL f=1,d=0;char s=getchar();
       while (s<'0'||s>'9'){if (s=='-') f=-1;s=getchar();}
       while (s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
       while (s!='\n') s=getchar();
       return f*d;
}
inline void write(LL x)
{
    if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    int len=0,buf[20];while(x)buf[len++]=x%10,x/=10;
    for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
}
inline void writeln(LL x){write(x);ENT;}
#define N 20
#define inf 16
int a[N];
int ans,n;

void check()
{
	fo(i,1,15)
	fo(j,1,a[i]) cout<<i<<' ';
	cout<<endl;
}

void dfs(int x,int y)
{
//	cout<<' ';check();cout<<x<<' '<<y<<' '<<ans<<endl;
	if (x>=ans) return;
	if (y==0) ans=min(ans,x);
	int p[5]={0};
	fo(i,1,15) p[a[i]]++;
	if (p[4]+p[3]>=2)
	{
		fo(i,1,11) 
		if (a[i]>=3)
		{
			int tt=1;
			fo(j,i+1,12)
			if (a[j]<3) break; else tt++;
			if (tt>=2)
			{
				int p=0;
				fo(j,i,i+tt-1) a[j]-=3,p+=3;
				dfs(x+1,y-p);
				fo(j,i,i+tt-1) a[j]+=3;
			}
		}
	}
	if (p[4]+p[3]+p[2]>=3)
	{
		fo(i,1,10) 
		if (a[i]>=2)
		{
			int tt=1;
			fo(j,i+1,12)
			if (a[j]<2) break; else tt++;
			if (tt>=3)
			{
				int p=0;
				fo(j,i,i+tt-1) a[j]-=2,p+=2;
				dfs(x+1,y-p);
				fo(j,i,i+tt-1) a[j]+=2;
			}
		}
	}
	if (p[1]+p[2]+p[3]+p[4]>=5)
	{
		fo(i,1,8)
		if (a[i])
		{
			int tt=1;
			fo(j,i+1,12)
			if (a[j]==0) break; else tt++;
			if (tt>=5)
			{
				int p=0;
				fo(j,i,i+tt-1) a[j]--,p++;
				dfs(x+1,y-p);
				fo(j,i,i+tt-1) a[j]++;
			}
		}
	}
	if (p[4])
	{
		fo(i,1,13)
		if (a[i]==4)
		{
			a[i]-=4;
			fo(j,1,14)
			if (a[j])
			{
				a[j]--;
				fo(k,j+1,15)
				if (a[k])
				{
					a[k]--;
					dfs(x+1,y-6);
					a[k]++;
				}
				a[j]++;
			}
			fo(j,1,14)
			if (a[j]>=2) 
			{
				a[j]-=2;
				fo(k,j+1,15)
				if (a[k]>=2)
				{
					a[k]-=2;
					dfs(x+1,y-8);
					a[k]+=2;
				}
				a[j]+=2;
			}
			a[i]+=4;
		}
	}
	if (p[3])
	{
		fo(i,1,13)
		if (a[i]>=3)
		{
			a[i]-=3;
			fo(j,1,15)
			{
				if (a[j])
				{
					a[j]--;
					dfs(x+1,y-4);
					a[j]++;
				}
				if (a[j]>=2)
				{
					a[j]-=2;
					dfs(x+1,y-5);
					a[j]+=2;
				}
			}
			a[i]+=3;
		}
	}
	int q=x;
	fo(i,1,15) 
	if (a[i]) q++;
	ans=min(ans,q);
}

int main()
{
	file(data);
	int t=read();n=read();
	while (t--)
	{
		memset(a,0,sizeof(a));ans=inf;
		fo(i,1,n) 
		{
			int x=read(),y=read();
			if (x>=3&&x<=13) a[x-2]++;
			if (x==1) a[12]++;
			if (x==2) a[13]++;
			if (x==0) 
			{
				if (y==1) a[14]++;
				else a[15]++;
			}
		}
		if (a[14]&&a[15])
		{
			a[14]--;a[15]--;
			dfs(1,n-2);
			a[14]++;a[15]++;
		}
		dfs(0,n);
		writeln(ans);
	}
	return 0;
}





day2t1(poj3258)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iomanip>
#define LL long long
#define PAU putchar(' ')
#define ENT putchar('\n')
#define fo(_i,_a,_b) for (int _i=_a;_i<=_b;_i++)
#define down(_i,_a,_b) for (int _i=_a;_i>=_b;_i--)
#define efo(_i,_a) for (int _i=last[_a];_i!=0;_i=e[_i].next)
#define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
#define end system("pause")
using namespace std;
LL read()
{
         LL f=1,d=0;char s=getchar();
         while (s<'0'||s>'9'){if (s=='-') f=-1;s=getchar();}
         while (s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
         return f*d;
}
LL readln()
{
       LL f=1,d=0;char s=getchar();
       while (s<'0'||s>'9'){if (s=='-') f=-1;s=getchar();}
       while (s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
       while (s!='\n') s=getchar();
       return f*d;
}
inline void write(LL x)
{
    if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    int len=0,buf[20];while(x)buf[len++]=x%10,x/=10;
    for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
}
inline void writeln(LL x){write(x);ENT;}
#define N 50005
int a[N];
int ll,n,m;

int check(int x)
{
	int sum=0,anss=0;
	fo(i,1,n)
	{
		sum+=a[i];
		if (sum<x) anss++;
		else sum=0;
	}
	return anss;
}

int main()
{
	while (cin>>ll>>n>>m)
	{
		fo(i,1,n) a[i]=read();
		sort(a+1,a+n+1);int ff=a[n];
		down(i,n,2) a[i]-=a[i-1];
		a[n+1]=ll-ff;n++;
//		fo(i,1,n) cout<<a[i]<<' ';cout<<endl;
		int l=1,r=ll+1;
		while (l<r)
		{
			int mid=(l+r)>>1;
//			cout<<l<<' '<<r<<' '<<check(mid)<<endl;
			if (check(mid)<=m) l=mid+1;
			else r=mid;
		}
		if (check(l)<=m) cout<<l<<endl;
		else cout<<l-1<<endl;
	}
	return 0;
}

day2t2今天回来看的时候才发现没有这道题,转移下就好了,然后……数组需要滚动一下

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iomanip>
#define LL long long
#define PAU putchar(' ')
#define ENT putchar('\n')
#define fo(_i,_a,_b) for (int _i=_a;_i<=_b;_i++)
#define down(_i,_a,_b) for (int _i=_a;_i>=_b;_i--)
#define efo(_i,_a) for (int _i=last[_a];_i!=0;_i=e[_i].next)
#define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
#define end system("pause")
using namespace std;
LL read()
{
         LL f=1,d=0;char s=getchar();
         while (s<'0'||s>'9'){if (s=='-') f=-1;s=getchar();}
         while (s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
         return f*d;
}
LL readln()
{
       LL f=1,d=0;char s=getchar();
       while (s<'0'||s>'9'){if (s=='-') f=-1;s=getchar();}
       while (s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
       while (s!='\n') s=getchar();
       return f*d;
}
inline void write(LL x)
{
    if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    int len=0,buf[20];while(x)buf[len++]=x%10,x/=10;
    for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
}
inline void writeln(LL x){write(x);ENT;}
#define N 1005
#define M 205
#define inf 1000000007
int f[2][N][M],sum[2][N][M];
int a[N],b[N];
int n,nn,m;

int main()
{
	n=read(),nn=read(),m=read();
	fo(i,1,n) a[i]=getchar()-'a'+1;getchar();
	fo(i,1,nn) b[i]=getchar()-'a'+1;
	f[0][0][0]=1;
	sum[0][0][0]=1;
	fo(i,0,n) sum[0][i][0]=1;
	fo(q,1,m)
	{
		int t=q&1;
		memset(f[t],0,sizeof(f[t]));
		memset(sum[t],0,sizeof(sum[t]));
		fo(i,1,n)
		{
			fo(j,1,nn)
			{
				if (a[i]==b[j])
				{
					f[t][i][j]=sum[(q+1)&1][i-1][j-1];
					if (a[i-1]==b[j-1])
					{
						f[t][i][j]+=f[t][i-1][j-1];
						f[t][i][j]%=inf;
					}
				}
				sum[t][i][j]=(sum[t][i][j]+f[t][i][j])%inf+sum[t][i-1][j];
				sum[t][i][j]%=inf;
			}
		}
	}
	LL ans=0;
	fo(i,1,n)
	{
		ans+=f[m&1][i][nn];
		ans%=inf;
	}
	writeln(ans);
	return 0;
}


day2t3(bzoj4326) 二分答案然后判判真的就好了,然后LCA最好用tarjan或者ST,避免倍增大法常数大

判的时候我们可以在他们的LCA处-2,在每个端点处+1然后类似树形DP的东西就能判断出来这些线段的交是否就是你二分出来的答案然后更新就好了但是常数略微有点大

bzoj上面似乎能帮你开栈所以不会挂,然而绝大多数oj都是不可以的,noip时的栈大的可怕完全不怕就是电脑太慢(明明CPU频率那么高)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iomanip>
#define LL long long
#define PAU putchar(' ')
#define ENT putchar('\n')
#define clr(a,b) memset(a,b,sizeof(a))
#define fo(_i,_a,_b) for (int _i=_a;_i<=_b;_i++)
#define down(_i,_a,_b) for (int _i=_a;_i>=_b;_i--)
#define efor(_i,_a) for (int _i=last[_a];_i!=0;_i=e[_i].next)
#define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
#define end system("pause")
using namespace std;
LL read()
{
         LL f=1,d=0;char s=getchar();
         while (s<'0'||s>'9'){if (s=='-') f=-1;s=getchar();}
         while (s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
         return f*d;
}
LL readln()
{
       LL f=1,d=0;char s=getchar();
       while (s<'0'||s>'9'){if (s=='-') f=-1;s=getchar();}
       while (s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
       while (s!='\n') s=getchar();
       return f*d;
}
inline void write(LL x)
{
    if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    int len=0,buf[20];while(x)buf[len++]=x%10,x/=10;
    for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
}
inline void writeln(LL x){write(x);ENT;}
#define N 300005
struct edge 
{
	int w,y,next;
}e[N*2];
int last[N]={0},tote=0;
int first[N],R[N*2],ver[N*2],dis[N],totf=0,f[N*2][20];
int u[N],v[N],w[N],C[N];
int cnt[N],tot=0;
int n,m;

void add(int u,int v,int w)
{
	int t=++tote;
	e[t].y=v;e[t].w=w;e[t].next=last[u];last[u]=t;
}

void adds(int x,int y,int z)
{
	add(x,y,z);
	add(y,x,z);
}

void dfs(int x,int dep,int pre,int key)
{
	w[x]=key;ver[++totf]=x;first[x]=totf;R[totf]=dep;dis[x]=dis[pre]+key;
	efor(i,x)
	if (e[i].y!=pre)
	{
		dfs(e[i].y,dep+1,x,e[i].w);
		ver[++totf]=x;R[totf]=dep;
	}
}

int RMQ(int l,int r)  
{
    int k=0;  
    while(1<<(k+1)<=r-l+1) k++;  
    int x=f[l][k],y=f[r-(1<<k)+1][k];  
    return R[x]<R[y]?x:y;
}  
  
int LCA(int x,int y)  
{
    int l=first[x],r=first[y];
    if (l>r) swap(l,r);
    int res=RMQ(l,r);  
    return ver[res];
}  

void DP(int n)
{
	fo(i,1,n) f[i][0]=i;
    for (int j=1;(1<<j)<=n;j++)
	{
		for (int i=1;i+(1<<j)-1<=n;i++)
        {
			int x=f[i][j-1],y=f[i+(1<<(j-1))][j-1];
            f[i][j]=R[x]<R[y]?x:y;
        }
	}
}

int ans=0,anss=0;
void judge(int x,int pre)
{
	efor(i,x)
	if (e[i].y!=pre)
	{
		judge(e[i].y,x);
		cnt[x]+=cnt[e[i].y];
	}
	if (cnt[x]==tot) anss=max(anss,w[x]);
}

bool checkmid(int x)
{
	clr(cnt,0);tot=0;
	ans=0,anss=0;
	fo(i,1,m)
	if (C[i]>x)
	{
		ans=max(ans,C[i]);
		cnt[u[i]]++;
		cnt[v[i]]++;
		cnt[LCA(u[i],v[i])]-=2;
		tot++;
	}
	judge(1,0);
	return ans-anss<=x;
}

int main()
{
	freopen("data.in","r",stdin);
	n=read(),m=read();
	fo(i,1,n-1)
	{
		int x=read(),y=read(),z=read();
		adds(x,y,z);
	}
	dfs(1,1,0,0);
	DP(2*n-1);
	int l=0,r=0;
	fo(i,1,m)
	{
		u[i]=read(),v[i]=read();
		C[i]=dis[u[i]]+dis[v[i]]-2*dis[LCA(u[i],v[i])];
//		cout<<C[i]<<' '<<LCA(u[i],v[i])<<endl;
		r=max(r,C[i]);
	}
//	cout<<endl;
	while (l<r)
	{
		int mid=(l+r)>>1;
		if (checkmid(mid)) r=mid;
		else l=mid+1;
	}
	writeln(l);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值