Filling A Bottle(排序)

题目描述
There is a famous fable telling about a clever crow tried to drink water in a bottle and succeeded.
Now, the crow had drunk enough,so he wanted to fill the bottle with stones. Each time he just looked around and flied to the nearest stone and picked it back. He was clever enough to choose the right stone when there were two different stones has the same distance. When the bottle is full, the crow’s work is over.

输入格式
There are multiple test cases, ending by EOF. The first line of each test case is two positive integers N and V, indicating for the number of stones and the volume of the bottle.
The N lines follows. Each line has three integers xi, yi and vi, standing for the coordinate (xi, yi) and the volume of the ith stone.
( 1<=N<=100, 1<=V<=1000 -100<=xi,yi<=100, 1 <=vi<=30 )

输出格式
For each test case, if the crow can cram the bottle with the given stones, print an integer standing for the minimum fly times the crow will take, else, print “NO” (without quotation marks) in one line.

样例输入
3 15
1 1 4
2 2 5
-2 -2 28
3 20
1 1 4
2 2 5
-2 -2 6

样例输出
2
NO

#include<iostream>
#include<algorithm>
using namespace std;
const int MAX=110;
struct node{
    
    
	int dis,v;
};
bool cmp(const node &x,const node &y){
    
    
	if(x.dis!=y.dis)
		return x.dis<y.dis;
	return x.v>y.v;
}
node a[MAX];
int main(){
    
    
	int n,v;
	while(cin>>n>>v){
    
    
		int x,y;
		for(int i=0;i<n;i++){
    
    
			cin>>x>>y;
			a[i].dis=x*x+y*y;
			cin>>a[i].v;
		}
		sort(a,a+n,cmp);
		int ans=-1;
		for(int i=0;i<n;i++){
    
    
			v-=a[i].v;
			if(v<=0){
    
    
				ans=i+1;
				break;
			}
		}
		if(ans==-1)
			cout<<"NO"<<endl;
		else 
			cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_51794965/article/details/111475300