码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10026 贪心

时间:2017-09-08 21:46:46      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:span   ble   cin   等价   problem   c++   struct   targe   uva   

https://vjudge.net/problem/UVA-10026

   对于两个二元组(Ti,Si),(Tj,Sj), 当先执行i时耗费的价值是Ti*Sj,反之则是Tj*Si, 显然如果想要第一种情况更优得话,要满足Ti*Sj<Tj*Si, 按照这个直接排序就好了,因为要求字典序最小所以当二者等价时序号小的优先。

  

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node{int t,s,id;}P[1005];
 4 bool cmp(node A,node B)
 5 {
 6     if(A.t*B.s==A.s*B.t){
 7         return A.id<B.id;
 8     }
 9     else{
10         return A.t*B.s<A.s*B.t;
11     }
12 }
13 int main()
14 {
15     int T,N,M,i,j,k;
16     cin>>T;
17     for(int xx=1;xx<=T;++xx)
18     {
19         if(xx>1) puts("");
20         cin>>N;
21         for(i=1;i<=N;++i)
22         {
23             P[i].id=i;
24             cin>>P[i].t>>P[i].s;
25         }
26         sort(P+1,P+1+N,cmp);
27         for(i=1;i<=N;++i)
28         {
29             if(i>1) printf(" ");
30             cout<<P[i].id;
31         }
32         puts("");
33     }
34     return 0;
35 }

 

uva 10026 贪心

标签:span   ble   cin   等价   problem   c++   struct   targe   uva   

原文地址:http://www.cnblogs.com/zzqc/p/7496235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!