2018.12.15【NOIP提高组】模拟B组

本文精选了三道算法竞赛题目,包括收集卡片、基因变异和abcd问题,详细解析了每道题目的解题思路和代码实现,涵盖了字符串处理、广度优先搜索和背包问题等核心算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JZOJ 100046 收集卡片

题目

询问一段字母种类最多的最短区间


分析

扫描每一个结尾,找到最适合的开头,统计最短长度


代码

#include <cstdio>
#include <cctype>
#define rr register
using namespace std;
char s[500001];
int n,cnt[151],sum=1,ans=2147473647,ans1=1,head=1;
signed main(){
	scanf("%d",&n);
	rr char c=getchar();
	while (!isalpha(c)) c=getchar(); cnt[s[1]=c]=1;
	for (rr int i=2;i<=n;++i){
		if (++cnt[s[i]=getchar()]==1) ++sum;
		while (head<i&&cnt[s[head]]>1){
		    if (!(--cnt[s[head]])) --sum;
		    ++head;
	    }
		if (ans1<sum) ans1=sum,ans=i-head+1;
		else if (ans1==sum) ans=ans>(i-head+1)?(i-head+1):ans;
	}
	printf("%d",ans);
	return 0;
}

JZOJ 100047 基因变异

题目

给定一个数字 x x x n n n个指定的数,可以在某一个二进制位取反(也就是异或 2 t 2^t 2t),可以异或指定的数,问最少需要多少次操作才能变成 y y y,( T ≤ 100000 , n ≤ 20 T\leq 100000,n\leq 20 T100000,n20


分析

一看询问的数量就知道必须快速查询,然后想了半天都没想好,后来发现可以从0开始广搜找对于每个数最少的次数,预处理后 O ( 1 ) O(1) O(1)查询。查询的时候可以这样想,若异或的数是 a n s ans ans,那么 x x x xor a n s ans ans xor y y y= x x x xor y y y xor a n s ans ans,所以说 x x x xor y y y即为需要查询的数
时间复杂度 O ( 2 20 + T ) O(2^{20}+T) O(220+T)


代码

#include <cstdio>
#include <algorithm>
#include <cctype>
#include <queue>
#define rr register
using namespace std;
int a[21],cnt[1050001],n,t;
inline signed iut(){
	rr int ans=0; rr char c=getchar();
	while (!isdigit(c)) c=getchar();
	while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
	return ans;
}
inline void print(int ans){
	if (ans>9) print(ans/10);
	putchar(ans%10+48);
}
signed main(){
	n=iut(); t=iut(); rr queue<int>q; q.push(0);
	for (rr int i=1;i<=n;++i) a[i]=iut();
	sort(a+1,a+1+n); n=unique(a+1,a+1+n)-a-1;//剪枝
	while (q.size()){
		int x=q.front(); q.pop();
		for (rr int i=1;i<=n;++i)
		if (!cnt[x^a[i]]&&x!=a[i])
		cnt[x^a[i]]=cnt[x]+1,q.push(x^a[i]);//指定的数
		for (rr int i=0;i<20;++i)
		if (!cnt[x^(1<<i)]&&x!=(1<<i))
		cnt[x^(1<<i)]=cnt[x]+1,q.push(x^(1<<i));//取反某一二进制位
	}
	while (t--){
		rr int x=iut(),y=iut();
		print(cnt[x^y]); putchar(10);
	}
	return 0;
}

JZOJ 100044 abcd

题目

有4个长度为 n n n的数组 a , b , c , d a,b,c,d a,b,c,d,现在需要选择 n n n个数构成数组 e e e,使 a i ≤ e i ≤ b i a_i\leq e_i\leq b_i aieibi,且 ∑ i = 1 n e [ i ] × c [ i ] = 0 \sum_{i=1}^ne[i]\times c[i]=0 i=1ne[i]×c[i]=0,最后使 ∑ i = 1 n e [ i ] × d [ i ] \sum_{i=1}^ne[i]\times d[i] i=1ne[i]×d[i]最大


分析

这道题目可以转换为选择 a i a_i ai b i b_i bi个物品,单个费用为 c i c_i ci,价值为 d i d_i di,并使价值最大,说到这里其实就是一个多重背包,不过可以直接取 a i a_i ai个,然后把 b i − a i b_i-a_i biai进行多重背包,但是单纯的多重背包依然会超时,单调队列又太麻烦,所以说可以用二进制优化,把它转换成01背包


代码

#include <cstdio>
#include <cctype>
#include <cstring>
#define rr register
#define max(a,b) ((a)>(b))?(a):(b)
using namespace std;
int w[4001],v[4001],f[50001],n,m,ans;
inline signed iut(){
	rr int ans=0,f=1; rr char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') f=-f,c=getchar();
	while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
	return ans*f;
}
signed main(){
	memset(f,0xcf,sizeof(f)); f[0]=0;
	for (rr int t=iut();t;--t){
		rr int a=iut(),b=iut(),c=iut(),d=iut();
		ans+=a*d,m-=a*c,b-=a;//容量在多重背包的时候用费用补回去
		for (rr int i=1;i<=b;i<<=1)
			w[++n]=i*c,v[n]=i*d,b-=i;//二进制拆分
		if (b) w[++n]=b*c,v[n]=b*d;	
	}
	for (rr int i=1;i<=n;++i)//01背包
	    for (rr int j=m;j>=w[i];--j)
	        f[j]=max(f[j],f[j-w[i]]+v[i]);
	printf("%d",f[m]+ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值