2023牛客寒假算法基础训练营5

A-小沙の好客_2023牛客寒假算法基础集训营5 (nowcoder.com)

解题思路:前缀和+二分

#include<bits/stdc++.h>
#define ll long long  
#define f(i,j,n) for(int i=j;i<n;i++)
#define PI acos(-1.0)
using namespace std;
const int INF=0x3f3f3f3f;
const int Mod=1e9+7;
const int N=1e6+6;
ll n,q,a[N],k,x,l,b[N];
int main () {
    ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>q;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++){
        b[i]=b[i-1]+a[i];
    }
    while(q--){
        cin>>k>>x;
        l=upper_bound(a+1,a+1+n,x)-a;
        cout<<b[l-1]-b[max(l-k-1,0ll)]<<"\n";
    }
	return 0;
}

B-小沙の博弈_2023牛客寒假算法基础集训营5 (nowcoder.com)

解题思路:偶数平局,奇数小雅赢

#include<bits/stdc++.h>
#define ll long long  
#define f(i,j,n) for(int i=j;i<n;i++)
#define PI acos(-1.0)
using namespace std;
const int INF=0x3f3f3f3f;
const int Mod=1e9+7;
const int N=1e6+6;
int n;
int main () {
    ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
    if(n%2==0){
        cout<<"win-win!";
    }
    else cout<<"Yaya-win!";
	return 0;
}

H-小沙の店铺_2023牛客寒假算法基础集训营5 (nowcoder.com)

解题思路:模拟题意即可,没有套路很简单

#include<bits/stdc++.h>
#define ll long long  
#define f(i,j,n) for(int i=j;i<n;i++)
#define PI acos(-1.0)
using namespace std;
const int INF=0x3f3f3f3f;
const int Mod=1e9+7;
const int N=1e6+6;
ll x,y,k,n,t,sum,q,p;
int main () {
    ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>x>>y>>k>>n>>t;
    for(int i=n;i>=1;i--){
        sum+=x*i;
        p+=i;
        if(p>=k){
            x+=(p/k)*y;
            p%=k;
        }
        if(sum>=t){
            q=i;
            break;
        }
    }
    if(sum>=t)cout<<n-q+1;
    else cout<<"-1";
	return 0;
}

K-小沙の抱团 easy_2023牛客寒假算法基础集训营5 (nowcoder.com)

解题思路:赛时推出了间隔规律,在代码下面注释

#include<bits/stdc++.h>
#define ll long long  
#define f(i,j,n) for(int i=j;i<n;i++)
#define PI acos(-1.0)
using namespace std;
const int INF=0x3f3f3f3f;
const int Mod=1e9+7;
const int N=1e6+6;
ll n,x,i;
int main () {
    ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
    if(n==1||n==2)cout<<0;
    else{
        x=2;
        i=1;
        while(x+1<n){
            i++;
            x*=2;
        }
        cout<<i;
    }
	return 0;
}
//1 0
//2 0
//3 1
//4 2
//5 2
//6 3
//7 3
//8 3
//9 3
//10 4
//11 4
//12 4
//13 4
//14 4
//15 4
//16 4
//17 4

方法二:n大于2 时,可以每次将n变成n/2+1

#include<bits/stdc++.h>
#define ll long long  
#define f(i,j,n) for(int i=j;i<n;i++)
#define PI acos(-1.0)
using namespace std;
const int INF=0x3f3f3f3f;
const int Mod=1e9+7;
const int N=1e6+6;
ll n,i;
int main () {
    ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
    while(n>2){
        n=n/2+1;
        i++;
    }
    cout<<i;
	return 0;
}

L-小沙の抱团 hard_2023牛客寒假算法基础集训营5 (nowcoder.com)

解题思路:一个dp题,由于n只会变小,只需从大到小遍历求出最小人数所需的最小代价即可

赛时和赛后补题区别还是很大的,赛时怎么都过不去,补题的时候重新看一下思路清晰很多

#include<bits/stdc++.h>
#define ll long long  
#define f(i,j,n) for(int i=j;i<n;i++)
#define PI acos(-1.0)
using namespace std;
const int INF = 0x3f3f3f3f;
const int Mod = 1e9 + 7;
const int N = 1e6 + 6;
int n, m, s;
ll a[N];
ll b[505], x[505], z[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i <= n; i++) {
        a[i] = INF;
    }
    s = n;
    z[n] = 1;
    for (int i = 0; i < m; i++) {
        cin >> b[i] >> x[i];
    }
    a[n] = 0;
    for(int i=n;i>2;i--){
        if(a[i]!=INF){
            for(int j=0;j<m;j++){
                if(i>=x[j]){
                    a[i-i%x[j]]=min(a[i-i%x[j]],a[i]+b[j]);
                    s=min(s,(int)(i-i%x[j]));
                }
            }
        }
    }
    cout << a[s];
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值