biubiubiiu @ 2017-12-12 21:02:50
#include <cstdio>
#define scanf scanf_s
void swap(int&, int&);
int main()
{
int n, s, a, b, i, j, cnt = 0;
int x[5000] = { 0 }, y[5000] = { 0 };
scanf("%d%d%d%d", &n, &s, &a, &b);
for (i = 0; i < n; ++i)
scanf("%d%d", x + i, y + i);
for (i = 0; i < n - 1; ++i){
for (j = i + 1; j < n; ++j){
if (y[i] > y[j])
{
swap(y[i], y[j]);
swap(x[i], x[j]);
}
}
}
i = 0;
while (s >= 0 && i < n){
if (x[i++] <= a + b && s - y[i] >= 0)
{
s -= y[i];
cnt++;
}
}
printf("%d", cnt);
getchar(); getchar();
return 0;
}
void swap(int& a, int& b) {
int tmp = a;
a = b;
b = tmp;
}
by Hugo_Minion @ 2017-12-12 21:23:11
你的有点复杂,换个简单的思路可以很轻松
几个月前写的,仅供参考,变量名乱起的,别在意
#include<iostream>
#include<algorithm>
using namespace std;
int n,s,a,b,x[5005],y[5005],high,ans=0,tmp[5005],ko=0,i;
bool vis[5005];
int main(){
cin>>n>>s>>a>>b;
for(i=1;i<=n;i++)
cin>>x[i]>>y[i];
high=a+b;
for(i=1;i<=n;i++){
if(x[i]<=high){
ko++;
tmp[ko]=y[i];
}
}
sort(tmp+1,tmp+ko+1);
for(i=1; i<=n; i++){
ans+=tmp[i];
if(ans>s)
break;
}
cout<<i-1;
return 0;
}