Thứ Tư, 27 tháng 4, 2022

OLYMPIC C++

#include <bits/stdc++.h>
using namespace std;
 
struct bt
{
    int a,b;
};
int n,c;
bt p[100001];
 
bool ss(bt u, bt v)
{
    if (u.a<v.a) return true;
    else return false;
}
 
int t=0;
void nhap()
{
    cin>>n>>c;
    for(int i=1;i<=n;i++)
        cin>>p[i].a>>p[i].b;
}
void qs(int l,int r)
{
    int i,j;
    bt x,t;
    x=p[(l+r)/2];
    i=l;j=r;
    while (i<=j)
    {
        while(ss(p[i],x)==true) i++;
        while (ss(x,p[j])==true) j--;
        if (i<=j)
        {
            t=p[i];p[i]=p[j];p[j]=t;
            i++;j--;
        }
    }
    if (l<j) qs(l,j);
    if (i<r) qs(i,r);
}
 
void giai()
{
    //sort(p+1,p+1+n,ss);    // dung ham sort co san trong c++
    qs(1,n);
    int d=0;
    for(int i=1;i<=n;i++)
    {
        if(c>=p[i].a)
        {
            c=c+p[i].b;
            d++;
        }
        else break;
    }
    cout<<d;
}
int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    freopen("olympic.inp","r",stdin);
    freopen("olympic.out","w",stdout);
    nhap();
    giai();
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.