OneFrance @ 2024-07-26 13:59:25
#include <iostream>
using namespace std;
int n, s,a, b;
int main(){
cin >> n >> s;
cin >> a >> b;
int ap[n][2];
for(int i=0;i<n;i++){
cin>>ap[i][0]>>ap[i][1];
}
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(ap[j][0]>ap[j+1][0]){
swap(ap[j],ap[j + 1]);
}
}
}
int count=0;
for (int i=0;i<n;i++){
if (ap[i][0]<=a+b&&s>=ap[i][1]){
count++;
s-=ap[i][1];
} else if(ap[i][0]<=a+b&&ap[i][0]>a&&s>=ap[i][1]){
count++;
s-=ap[i][1];
} else if(ap[i][0]>a+b&&ap[i][0]<=a+2*b&&s>=ap[i][1]){
count++;
s-=ap[i][1];
}
}
cout<<count+1;
return 0;
}
by OneFrance @ 2024-07-26 13:59:57
是只有...
by zixindexiaoan @ 2024-07-26 14:37:27
本蒟蒻实在是太菜了,根本没看懂你写的代码
(实际上是for循环里面的)
给你看看本蒟蒻的思路(浅浅加了一点注释,不知道你看不看得懂,主要本蒟蒻太菜了)
#include<bits/stdc++.h>//万能头文件
using namespace std;
int n,s,a,b,x,y,cnt = 0;
struct node
{
int xi,yi;
}apple[50005];//定个结构体方便点
bool cmp(node x,node y)
{
return x.yi < y.yi;
}
int main(){
cin >> n >> s >> a >> b;
for(int i = 1;i <= n;i++)
{
cin >> x >> y;
if(x <= a + b)//把能够到的进去
{
cnt++;
apple[cnt].xi = x;
apple[cnt].yi = y;
}
}
sort(apple + 1,apple + cnt + 1,cmp);//直接sort排序省事
int ans = 0;
for(int i = 1;i <= cnt;i++)
{
if(s >= apple[i].yi)//体力够就摘
{
ans++;
s -= apple[i].yi;
}
}
cout << ans << endl;
return 0;
}
啊! 本蒟蒻太菜了,连@都不会,希望你看得见
by postpone @ 2024-07-29 13:35:47
@OneFrance 这题贪心一下就行了,感觉你理解错题目意思了
int n, s, a, b;
cin >> n >> s >> a >> b;
vector<int> t;
for (int i = 1; i <= n; i++) {
int h, tmps;
cin >> h >> tmps;
if (h <= a + b)
t.push_back(tmps);
}
sort(t.begin(), t.end(), greater<>());
int cnt = 0;
while (!t.empty()) {
if (s - t.back() < 0) {
break;
}
s -= t.back();
t.pop_back();
cnt++;
}
cout << cnt;
by OneFrance @ 2024-07-30 09:39:57
@postpone 阿巴阿巴,懂了