蓝桥算法训练营—普及组—2023年2.7日打卡

_谦言万语 2023-02-07 22:39:23

T1

//01背包
#include <iostream>
using namespace std;
int f[100005];
int main()
{
    int n,V;
    cin>>V>>n;
    int v;
    for(int i=1; i<=n; i++)
    {
        cin>>v;
        for(int j=V; j>=v; j--)
        if(f[j]<f[j-v]+v)
        f[j]=f[j-v]+v;
    }
    cout<<V-f[V];
    return 0;
}

T2

//二分答案(至少,往左找)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N=1e5+100;
int h[N],w[N];
int n,k;

bool check(int x)
{
    int res=0;
    for(int i=0; i<n; i++)
    {
        res+=(LL)(h[i]/x)*(w[i]/x);
    }
    if(res>=k) return true; 
    else return false;
}

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0; i<n; i++) scanf("%d%d",&h[i],&w[i]);
    
    int l=0, r=1e5+10;
    while(l<r)
    {
        int mid=(l+r+1)>>1;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    cout<<l;
    return 0;
}

T3

//二分(至多,往右找)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int a[N];
int n,c,maxn;

bool check(int x)
{
    int y=a[0], res=1;
    for(int i=1; i<n; i++)
    {
        if(a[i]-y>=x) res++, y=a[i];
    }
    return res>=c;
}

int main()
{
    cin>>n>>c;
    for(int i=0; i<n; i++) 
    {
        cin>>a[i];
        maxn=max(maxn,a[i]);
    }
    sort(a,a+n);
    int l=0, r=maxn;
    while(l<r)
    {
        int mid=(l+r+1)/2;
        if(check(mid)) l=mid;
        else r=mid-1;
    }
    cout<<l;
    return 0;
}

 

T4

//dfs暴搜
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000009
using namespace std;
typedef long long LL;
LL s,n,k;
LL a[30];

bool is_prime(int x) {
	for(int i=2; i<=sqrt(x); i++)
		if(x%i==0) return false;
	return true;
}

void dfs(int sum,int from,int cur) {
	if(cur==k) {
		if(is_prime(sum)) s++;
		return;
	}
	for(int i=from; i<n; i++)
		dfs(sum+a[i],i+1,cur+1);
}

int main() {
	cin>>n>>k;
	for(int i=0; i<n; i++)
		cin>>a[i];
	dfs(0,0,0);
	cout<<s;
	return 0;
}

 

...全文
15 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,782

社区成员

发帖
与我相关
我的任务
社区描述
和众多高校算法内卷分子,一起学习和交流算法那。浓郁的算法交流氛围,拒绝躺平,有效内卷。加入我们,私信我拉你入核心内卷群。
算法数据结构leetcode 个人社区
社区管理员
  • 执 梗
  • Dream-Y.ocean
  • ღCauchyོꦿ࿐
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

 刷题!

试试用AI创作助手写篇文章吧