zoukankan      html  css  js  c++  java
  • 11.19 考试

    T1
    O(n)时间复杂度求第k小的值;
    stl函数即可

    暴力解法:一半通排,一半sort;

    #include<bits/stdc++.h>
    #define LL long long
    using namespace std;
    const int N=1e7+7;
    const int M=2e7;
    const int p=1e9;
    LL n,k,x,y,cnt,num;
    LL a[N],b[N],c[M+7];
    void mod(LL &x,int y){
    	LL tt=x/y;
    	x-=tt*y;
    }
    int main(){
    	freopen("sort.in","r",stdin);
    	freopen("sort.out","w",stdout);
    	scanf("%lld%lld%lld%lld",&n,&k,&x,&y);
    	a[1]=x;
    //	if(n<=1000){
    //		for(int i=1;i<=n;i++){
    //			a[i]=(a[i-1]*y+x)%p;
    //		}
    //		sort(a+1,a+n+1);
    //		cout<<a[k];
    //		return 0;
    //	}
    //	if(a[1]>M) b[++cnt]=a[1];
    //	else c[a[1]]++;
    	for(int i=2;i<=n;i++){
    		a[i]=a[i-1]*y+x;
    		mod(a[i],p);
    //		if(a[i]>M) b[++cnt]=a[i];
    //		else c[a[i]]++;
    	}
    	nth_element(a+1,a+k,a+n+1);
    	cout<<a[k]<<"
    ";
    //	num=n-cnt;
    //	if(k<=num){
    //		LL sum=0;
    //		for(int i=0;i<=M;i++){
    //			sum+=c[i];
    //			if(sum>=k){
    //				cout<<i;
    //				break;
    //			}
    //		}
    //	}else{
    //		k-=num;
    //		sort(b+1,b+cnt+1);
    //		cout<<b[k];
    //	}
    	fclose(stdin);
    	fclose(stdout);
    	return 0;
    }
    /*
    5 3
    2 9
    
    100 55
    123982 123123
    
    9999949 400000
    6587534 6654739
    */
    

    T2
    小林想在桃园装上一批路灯。小林计算了一下,如果整个桃园都按要求装
    上路灯的话,总共需要n盏路灯,由于安装位置的不同,第i盏路灯
    的功率为a[i]瓦。小林给这次路灯安装设置的可用总功率是m瓦,超过总功
    率电路就会跳闸。小林希望在电路不会跳闸的前提下,尽可能把总功率用
    完,请你帮小林计算一下,最多能使用多少功率

    折半搜索+双指针优化;

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #define LL long long
    using namespace std;
    const int N=50;
    const int M=1e7+7;
    int n,m,cnt,num,mid;
    LL ans;
    int a[N];
    int b[M],c[M];
    void dfs1(int k,LL sum){
    	if(sum>m) return;
    	if(k==mid+1){
    		b[++cnt]=sum;
    		ans=max(ans,sum);
    		return;
    	}
    	dfs1(k+1,sum+a[k]);
    	dfs1(k+1,sum);
    }
    void dfs2(int k,LL sum){
    	if(sum>m) return;
    	if(k==n+1){
    		c[++num]=sum;
    		ans=max(ans,sum);
    		return;
    	}
    	dfs2(k+1,sum+a[k]);
    	dfs2(k+1,sum);
    }
    int main(){
    	freopen("light.in","r",stdin);
    	freopen("light.out","w",stdout);
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=n;i++){
    		scanf("%d",&a[i]);
    	}
    	mid=n/2;
    	dfs1(1,0);
    	dfs2(mid+1,0);
    	sort(b+1,b+cnt+1);
    	sort(c+1,c+num+1);
    	int r=num;
    	for(int i=1;i<=cnt;i++){
    		while(r>1&&1LL*b[i]+c[r]>m) r--;
    		if(1LL*b[i]+c[r]<=m) ans=max(ans,1LL*b[i]+c[r]);
    	}
    	cout<<ans;
    	fclose(stdin);
    	fclose(stdout);
    	return 0;
    }
    /*
    5 9
    1 3 1 3 3
    */
    ```T1
    O(n)时间复杂度求第k小的值;
    stl函数即可
    
    暴力解法:一半通排,一半sort;
    
    ```cpp
    #include<bits/stdc++.h>
    #define LL long long
    using namespace std;
    const int N=1e7+7;
    const int M=2e7;
    const int p=1e9;
    LL n,k,x,y,cnt,num;
    LL a[N],b[N],c[M+7];
    void mod(LL &x,int y){
    	LL tt=x/y;
    	x-=tt*y;
    }
    int main(){
    	freopen("sort.in","r",stdin);
    	freopen("sort.out","w",stdout);
    	scanf("%lld%lld%lld%lld",&n,&k,&x,&y);
    	a[1]=x;
    //	if(n<=1000){
    //		for(int i=1;i<=n;i++){
    //			a[i]=(a[i-1]*y+x)%p;
    //		}
    //		sort(a+1,a+n+1);
    //		cout<<a[k];
    //		return 0;
    //	}
    //	if(a[1]>M) b[++cnt]=a[1];
    //	else c[a[1]]++;
    	for(int i=2;i<=n;i++){
    		a[i]=a[i-1]*y+x;
    		mod(a[i],p);
    //		if(a[i]>M) b[++cnt]=a[i];
    //		else c[a[i]]++;
    	}
    	nth_element(a+1,a+k,a+n+1);
    	cout<<a[k]<<"
    ";
    //	num=n-cnt;
    //	if(k<=num){
    //		LL sum=0;
    //		for(int i=0;i<=M;i++){
    //			sum+=c[i];
    //			if(sum>=k){
    //				cout<<i;
    //				break;
    //			}
    //		}
    //	}else{
    //		k-=num;
    //		sort(b+1,b+cnt+1);
    //		cout<<b[k];
    //	}
    	fclose(stdin);
    	fclose(stdout);
    	return 0;
    }
    /*
    5 3
    2 9
    
    100 55
    123982 123123
    
    9999949 400000
    6587534 6654739
    */
    

    T2
    小林想在桃园装上一批路灯。小林计算了一下,如果整个桃园都按要求装
    上路灯的话,总共需要n盏路灯,由于安装位置的不同,第i盏路灯
    的功率为a[i]瓦。小林给这次路灯安装设置的可用总功率是m瓦,超过总功
    率电路就会跳闸。小林希望在电路不会跳闸的前提下,尽可能把总功率用
    完,请你帮小林计算一下,最多能使用多少功率

    折半搜索+双指针优化;

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #define LL long long
    using namespace std;
    const int N=50;
    const int M=1e7+7;
    int n,m,cnt,num,mid;
    LL ans;
    int a[N];
    int b[M],c[M];
    void dfs1(int k,LL sum){
    	if(sum>m) return;
    	if(k==mid+1){
    		b[++cnt]=sum;
    		ans=max(ans,sum);
    		return;
    	}
    	dfs1(k+1,sum+a[k]);
    	dfs1(k+1,sum);
    }
    void dfs2(int k,LL sum){
    	if(sum>m) return;
    	if(k==n+1){
    		c[++num]=sum;
    		ans=max(ans,sum);
    		return;
    	}
    	dfs2(k+1,sum+a[k]);
    	dfs2(k+1,sum);
    }
    int main(){
    	freopen("light.in","r",stdin);
    	freopen("light.out","w",stdout);
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=n;i++){
    		scanf("%d",&a[i]);
    	}
    	mid=n/2;
    	dfs1(1,0);
    	dfs2(mid+1,0);
    	sort(b+1,b+cnt+1);
    	sort(c+1,c+num+1);
    	int r=num;
    	for(int i=1;i<=cnt;i++){
    		while(r>1&&1LL*b[i]+c[r]>m) r--;
    		if(1LL*b[i]+c[r]<=m) ans=max(ans,1LL*b[i]+c[r]);
    	}
    	cout<<ans;
    	fclose(stdin);
    	fclose(stdout);
    	return 0;
    }
    /*
    5 9
    1 3 1 3 3
    */
    
  • 相关阅读:
    思考:如何保证服务稳定性?
    svn:Item is out of date解决办法
    MAC OS 10.15 Lucene 源码分析环境搭建
    防止数据重复提交的6种方法(超简单)!
    6种快速统计代码执行时间的方法,真香!
    漫画:Integer 竟然有 6 种比较方式?
    IDEA 不为人知的 5 个骚技巧!真香!
    自由职业半年之后,我又滚回职场了...
    为什么建议你使用枚举?
    ESP8266
  • 原文地址:https://www.cnblogs.com/Aswert/p/14014615.html
Copyright © 2011-2022 走看看