[NOIP2015]day1题解集合

T1:

经典二分题,所以两分钟解决

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 500005;
int L,a[N];
int n,m,l=0,r=100000000,ans;
bool check( int mid ){
	int cnt=0;
	for( int i = 0,j; i <= n+1; i++ ){
		for( j = i+1; j <= n+1; j++ ){
			if( a[j] - a[i] < mid ) cnt++;
			else break;
		}
		i = j-1;
	}
	return cnt <= m;
}
int main(){
	freopen("stone.in","r",stdin);
	freopen("stone.out","w",stdout);
	scanf("%d%d%d", &L, &n, &m);
	for( int i = 1; i <= n; i++ ) scanf("%d", &a[i]); a[0] = 0; a[n+1] = L;
	r = L;
	while( l <= r ){
		int mid = (l+r)>>1;
		if( check(mid) ){  ans = mid; l = mid+1; }
		else r = mid-1;
	}
	printf("%d", ans);
	return 0;
}
T2:noip的字符串,除了DP也没什么了 ,但我调了一个小时
#include<iostream>
#include<cstring>
#include<cstdio>
const int mod = 1000000007;
using namespace std;
const int N = 1005;
const int M = 1005;
char A[N],B[M];
int n,m,K,f[2][M][M][2],ans;
int main(){
	freopen("substring.in","r",stdin);
	freopen("substring.out","w",stdout);
	scanf("%d%d%d", &n, &m, &K);
	scanf("%s", A+1); scanf("%s", B+1);
	f[1][0][0][0] = f[0][0][0][0] = 1;
	for( int i = 1; i <= n; i++ )
		for( int j = 1; j <= min(i,m); j++ )
			for( int k = 1; k <= min(K,j); k++ ){
				int now = i&1, last = (i-1)&1;
				f[now][j][k][0] = (f[last][j][k][0] + f[last][j][k][1])%mod;
				if( A[i] == B[j] ) f[now][j][k][1] = ((f[last][j-1][k-1][0]+f[last][j-1][k-1][1])%mod+f[last][j-1][k][1])%mod;
				else f[now][j][k][1] = 0;
			}
	printf("%d", (f[n&1][m][K][0]+f[n&1][m][K][1])%mod);
	return 0;
}
T3:

http://blog.csdn.net/youhavepeople/article/details/74079524

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int P = 16;
const int N = 300000 + 5;
int last[N],cnt,n,m,dep[N],fa[N],anc[N][P+5],LCA[N],dise[N],dis[N],sum[N],vv[N],B[N],A[N];
struct Edge{
	int to,v,next;
}e[N*2];
void insert( int u, int v, int w ){ e[++cnt].to = v; e[cnt].next = last[u]; last[u] = cnt; e[cnt].v = w; }
void dfs( int u, int f ){
	anc[u][0] = fa[u] = f;
	for( int p = 1; p <= P; p++ )
		anc[u][p] = anc[anc[u][p-1]][p-1];
	for( int t = last[u]; t; t = e[t].next ) {
		int v = e[t].to;
		if( v == f ) continue;
		dep[v] = dep[u] + 1; vv[v] = e[t].v; dis[v] = dis[u] + e[t].v;
		dfs( v, u );
	}
}
int lca( int u, int v ) {
	if( dep[u] < dep[v] ) swap(u,v);
	int t = dep[u] - dep[v];
	for( int p = 0; t; t>>=1,p++ )
		if( t & 1 ) u = anc[u][p];
	if( u == v ) return u;
	for( int p = P; p >= 0; p-- ) 
		if( anc[u][p] != anc[v][p] )
			u = anc[u][p], v = anc[v][p];
	return anc[u][0];
}
void pus( int x, int f ){
	for( int i = last[x]; i; i = e[i].next )
		if( e[i].to != f ){
			pus( e[i].to, x );
			sum[x] += sum[e[i].to];
		}
}
bool check( int x ){
	int cont = 0, mx = 0;
	for( register int i = 1; i <= n; i++ ) sum[i] = 0;
	for( int i = 1; i <= m; i++ )
		if( dise[i] > x ){
			cont++; sum[B[i]]++;
			sum[A[i]]++; sum[LCA[i]] -= 2;
			mx = max( mx, dise[i] - x );
		}
	pus(1,0);
	for( int i = 1; i <= n; i++ ) if ( sum[i] == cont && vv[i] >= mx ) return true;
	return false;
}
int main(){
	scanf("%d%d", &n, &m);
	for( int i = 1; i < n; i++ ){
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		insert( u, v, w ); insert( v, u, w );
	}
	dep[1] = 1;
	dfs(1,1);
	int l = 0,r = 0;
	for( int i = 1; i <= m; i++ ){
		scanf("%d%d", &A[i], &B[i]);
		LCA[i] = lca(A[i],B[i]); dise[i] = dis[A[i]] + dis[B[i]] - 2*dis[LCA[i]];
				r = max(r,dise[i]);
	}
	while( l <= r ){
		int mid = (l+r)>>1;
		if( check(mid) ) r = mid-1;
		else l = mid+1;
	}
	printf("%d", l);
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值