来自地狱的喵 @ 2019-05-19 20:22:51
#include<bits/stdc++.h>
using namespace std;
const int MANX=5000;
int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int n,s,a,b,xi[MANX],yi[MANX],i,num=0;
cin>>n>>s;
cin>>a>>b;
for(i=0;i<n;i++)
cin>>xi[i]>>yi[i];
for(i=0;i<n;i++)
{
if(s<=0)
break;
if((a+b)>=xi[i])
num++;
s=s-yi[i];
}
cout<<num<<endl;
return 0;
}
by Celestial_Scarlet @ 2019-05-19 20:24:49
@淡泊名利 要排序
by 老彩笔 @ 2019-05-19 20:55:25
把每个苹果的重量从小到大排序
by 来自地狱的喵 @ 2019-05-20 17:42:27
#include<bits/stdc++.h>
using namespace std;
const int MANX=5000;
int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int n,s,a,b,xi[MANX],yi[MANX],i,num=0,temp,temp1;
cin>>n>>s;
cin>>a>>b;
for(i=0;i<n;i++)
cin>>xi[i]>>yi[i];
for(i=n-1;i>=0;i--)
{
temp=0;
temp1=0;
if(xi[i]<xi[i-1])
{
temp=xi[i-1];
xi[i-1]=xi[i];
xi[i]=temp;
temp1=yi[i-1];
yi[i-1]=yi[i];
yi[i]=temp1;
}
}
for(i=0;i<n;i++)
{
if(s<=0)
break;
if((a+b)>=xi[i])
num++;
s-=yi[i];
}
cout<<num<<endl;
return 0;
}
(无奈)依然20
by 来自地狱的喵 @ 2019-05-21 16:59:51
@hdyzgsj @baoyu 重构了一遍,终于40了。。。 求挑毛病
#include<bits/stdc++.h>
using namespace std;
const int MANX=5000;
int main()
{
int n,s,a,b,xi[MANX],yi[MANX],i,temp,temp1,num=0;
cin>>n>>s;
cin>>a>>b;
for(i=0;i<n;i++)
cin>>xi[i]>>yi[i];
for(i=0;i<n;i++)
{
if(yi[i]>yi[i+1])
{
temp=0;
temp1=0;
temp=yi[i+1];
yi[i+1]=yi[i];
yi[i]=temp;
temp1=xi[i+1];
xi[i+1]=xi[i];
xi[i]=temp1;
}
}
for(i=0;i<n;i++)
{
if(s<=0)
break;
if(xi[i]<=a+b)
num++;
s-=yi[i];
}
cout<<num;
return 0;
}
求大佬指点
(我要AC 555)
by 老彩笔 @ 2019-05-21 17:23:47
用结构体再用sort排序试一下吧
struct apple{
int h,stren;
}ap[5005];
bool cmp(apple a,apple b)
{
return a.stren<b.stren;
}
…………
sort(ap+1,ap+n+1,cmp);
…………
by 老彩笔 @ 2019-05-21 17:23:57
@淡泊名利
by 老彩笔 @ 2019-05-21 17:51:23
@淡泊名利
或者用个冒泡排序
for(int j=1;j<=n;j++)
for(i=j+1;i<=n;i++)
{
if(yi[i]<yi[j])
{
swap(yi[i],yi[j]);
swap(xi[i],xi[j]);
}
}
下面这里可能会多算
for(i=0;i<n;i++)
{
if(s<=0)
break;
if(xi[i]<=a+b)
num++;
s-=yi[i];
}
改成这样试一下把
for(i=1;i<=n;i++)
{
if(xi[i]<=a+b&&s>=yi[i])
{
num++;
s-=yi[i];
}
}
by 老彩笔 @ 2019-05-21 17:52:13
https://www.luogu.org/recordnew/show/19244576
by Hams_rain @ 2019-07-17 21:04:14
这道题可是贪心啊