20分求助

P1478 陶陶摘苹果(升级版)

zheng0201 @ 2023-10-15 19:58:38

代码如下

#include<bits/stdc++.h>
using namespace std;

int n;
long long a[200000],tong[20000000],ans=0;

int main(){
    //freopen("submaximum.in","r",stdin);
    //freopen("submaximum.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(a[i]==a[j]){
                continue;
            }
            tong[a[i]%a[j]]=1;
            tong[a[j]%a[i]]=1;
        }
    }
    for(int i=20000000;i>=1;i--){
        if(tong[i]==1){
            //cout<<i<<endl;
            ans++;
        }
        if(tong[i]==1 && ans==2){
            cout<<i<<endl;
        }
        //cout<<ans<<endl;
    }
    if(ans<2){
        cout<<"-1";
    }
    return 0;
}

by CHWe668 @ 2023-10-20 18:57:41

半天没看懂……

#include <bits/stdc++.h>
using namespace std;
struct st{
    long long height,each_energy;
};
st s[5005];
bool cmp(st x,st y) {return x.each_energy<y.each_energy;}
long long n,ch,b,energy,cnt=1,sum=0;
int main(){
//  freopen("picking.in","r",stdin);
//  freopen("picking.out","w",stdout);
    cin >> n >> energy >> ch >> b;
    if(n==0) {cout<<0;return 0;}
    for(long long i=1;i<=n;++i){
        cin >> s[i].height;
        cin >> s[i].each_energy;
    }
    sort(s+1,s+n+1,cmp);
    while(energy>=0){
        if(s[cnt].height<=(ch+b)){
            energy-=s[cnt].each_energy;
            sum++;
        }
        cnt++;
    }
    cout << sum-1;
}

AC


by CHWe668 @ 2023-10-20 18:58:35

写一个sort排序就可以了


by tiansuo1145 @ 2023-10-21 21:23:03

自己看

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <cctype>
#include <iomanip>

using namespace std;

struct Apple{
    int x;
    int y;
};

int n, s, a, b, cnt;
Apple arr[5005];

bool cmp(Apple a, Apple b){
    if(a.y != b.y){
        return a.y < b.y;
    }

    return a.x < b.x;
}

int main(){
    cin>>n>>s>>a>>b;

    for(int i=1; i<=n; i++){
        cin>>arr[i].x>>arr[i].y;
    }

    sort(arr+1, arr+n+1, cmp);

    for(int i=1; i<=n && s; i++){
        if(s>=arr[i].y && a+b>=arr[i].x){
            cnt++;
            s -= arr[i].y;
        }
    }

    cout<<cnt;

    return 0;
}

|