Simulation 2019CSP-S

T1

description

Given a positive integer S, it asks you to select a plurality of mutually different positive integers such that their sum is not greater than S, and
and the number of each factor (not including itself) and the maximum sum.

input

Enter a number of S.

output

The output of a number ans

Sample

sample input 1
4
1.5 sample output 1
3
1.6 sample input 2
6
1.7 sample output 2
6

data range

Data for 30%, S ≤ 10.
To 100% of the data, S ≤ 1000.

Thinking

Typical knapsack problem

Code

#include <bits/stdc++.h>
#define int long long
using namespace std;
int s;
int sum[1005];
inline int read(){
	int cnt=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')	f=-f;c=getchar();}
	while(isdigit(c)){cnt=(cnt<<3)+(cnt<<1)+(c^48);c=getchar();}
	return cnt*f;
}
signed main(){
	freopen("sum.in","r",stdin);
	freopen("sum.out","w",stdout);
	s=read();
	for(int i=1;i<=s;i++){
		for(int j=1;j<=i/2;j++){//任何一个因数都不会超过i/2
			if((i%j)==0){
				sum[i]+=j;//预处理出因数和
			}
		}
	}
	for(int i=1;i<=s;++i){//枚举当前
		for(int j=1;j<=i/2;++j){
			sum[i]=max(sum[i],sum[j]+sum[i-j]);//sum类似于前缀和
		}
	}
	printf("%lld",sum[s]);
	return 0;
}

T2T2

Thinking: + monotonic binary optimization queue

Monotone queue is a queue having a generally monotonic, there are two kinds of monotonous increase and monotonous decrease, in general, the head of the queue is the queue maximum queue whole or minimum
implementation: If the queue is empty, the A [i] from the team tail enqueued
if the queue is not empty, than the a [i] elements are ejected from the large end of the team, then a [i] is enqueued
when the queue is not empty and a [i] is greater than the tail, directly from the team end of the A [i] enqueue

Code

sum: minus prefix and the average value
check in the enumeration

  1. Note that i is an enumeration of the right end point
  2. i L i-L and i R i-R enumeration point where the left end of the range
  3. q in e what single queue maintained by minimal sequence prefix and ( s u m (sum value ) )
    note When monotonous queue maintenance applies only to consider only the minimum and maximum values, pop queue status are not any impact on the answer (decision monotonic)
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+50;
int n,L,R,a[N];
double mid;
inline int read()
{
    char ch = getchar();int i = 0, f = 1;
    while (!isdigit(ch)){if (ch == '-')f = -1;ch = getchar();}
    while (isdigit(ch)){i = (i << 1) + (i << 3) + ch - '0';ch =getchar();}
    return i * f;
}
bool check(double v){
	static double sum[N],que[N];
	static int head,tail,pos[N];
	head=1;tail=0;
	for(int i=1;i<=n;i++){
		sum[i]=sum[i-1]+1.0*a[i]-v;
	}
	for(int i=L;i<=n;i++){
		while(head<=tail&&que[tail]>=sum[i-L]){
			tail--;//如果单调队列最末大于现在值,不断出队以维护单调性
		}
		while(head<=tail&&pos[head]+R<i){//pos[head]<i-R此时已经不可能作为左端点,出队
			head++;
		}
		que[++tail]=sum[i-L];
		pos[tail]=i-L;
		if(sum[i]-que[head]>=0){
			return true;
		}
	}
	return false;
}
int main(){
	n=read(),L=read(),R=read();
	for(int i=1;i<=n;++i){
		a[i]=read();
	}
	double l=0,r=1e6;
	for(int i=1;i<=100;i++){
		mid=(l+r)/2;
		if(check(mid)){
			l=mid;
		}
		else	r=mid;
	}
	printf("%.4lf\n",mid);
	return 0;
}

T3

Topic [Wallace]Title Description

Thinking

When determining whether to join an edge, if both ends of this edge had been connected, we need to know whether the block has a link ring. If none, then this edge can be added, and if the block via the crossover ring at both ends are obtained whether the new block has Unicom ring.
With disjoint-set to maintain connectivity, additional maintenance Unicom block to whether there ring.

  1. Both the ends of the same tree: arbitration ring; the ring: nonunion; no ring: Double
  2. Both the ends of the two trees: arbitration ring; two rings are: discontinuous; balance: even

Code

There are no ring array record book
fa maintenance disjoint-set
number of edges connected tot ( ( Trees but also with an edge, it is not n 1 ) n-1)
and the set of check information, and when combined loop

#include <bits/stdc++.h>
using namespace std;
int fa[500050],book[500050];

struct node{
	int x,y,w;
}a[500050];
bool cmp(const node &a,const node &b){
	return a.w<b.w;
}
inline char nc(){//超级快读
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int _read(){
    char ch=nc();int sum=0;
    while(!(ch>='0'&&ch<='9'))ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum;
}
int getf(int x){
	return fa[x]==x?x:fa[x]=getf(fa[x]);
}
int main(){
	int n,m,i,x,y;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;++i)  fa[i]=i;
	for(i=1;i<=m;++i)  scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
	sort(a+1,a+m+1,cmp);//按边权排序
	long long tot=0,ans=0;
	for(int i=1;i<=m;i++){
		x=getf(a[i].x),y=getf(a[i].y);
		if(x==y&&!book[x]){
			book[x]=1;tot++;ans+=a[i].w;
		}
		else{
			if(book[x]&&book[y])	continue;
			fa[x]=y;book[y]=book[x]|book[y];
			tot++;ans+=a[i].w;
		}
	}
	if(tot!=n){
		printf("No"); 
	}
	else{
		printf("%lld",ans);
	}
	return 0;
	
}
Published 37 original articles · won praise 11 · views 1949

Guess you like

Origin blog.csdn.net/weixin_42750325/article/details/102470476