ACM PKU 1828 Monkeys' Pride

http://acm.pku.edu.cn/JudgeOnline/problem?id=1828
看了discuss,很多同学对题意理解有误(刚开始我也理解错了)
主要是这句:If a monkey lives at the point (x0, y0), he can be the king only if there is no monkey living at such point (x, y) that x>=x0 and y>=y0

成为猴王的条件是,没有任何的一个猴子的x坐标和y坐标都大于它,而不是说猴王的x和y都要最大.
ok,算法也出来了,简单地说: 先对x快速排序,然后统计y  , 时间效率是O(n^lgn)
具体细节要自己体会,这题挺经典的.另外快速排序的方法,虽然我也不是第一次用到了,但是仍然不熟练,到网上查了语法再做的.
#include"stdio.h"
#include
"stdlib.h"

 
 typedef 
struct{
    
int x;
     
int y;
 }
node[50001];
 


 
int cmp(const void *pl, const void *pr){   ///按照x从小到大排序
    node *p1 = (node*)pl; 
     node 
*p2 = (node*)pr;
     
if(p1->== p2->x)           
         
return p1->- p2->y;
    
return p1->- p2->x;
 }

 

void main(){
     
int num,i,total,maxy;
     
while(scanf("%d",&num) && num){
         
for(i=0;i<num;i++)
             scanf(
"%d%d",&nodes[i].x,&nodes[i].y);
         qsort(nodes, num, 
sizeof(node), &cmp); //按照x从小到大排序
       total=1;           //最后一个猴子的x最大,所以至少有一个猴王. 往前扫描,如果出现某个猴子的y大于当前最大y,total+1
         maxy=nodes[num-1].y;
        
for(i=num-2;i>=0;i--){
             
if(maxy<nodes[i].y){
                maxy
=nodes[i].y;
                 total
++;
             }

         }

         printf(
"%d\n",total);
     }

     
return;
 }



另外,在PKU上编译器效率的问题:

同样的程序,我测试了3次.
include的时候,如果用iostream,在  C++编译器下测试,Memory是476K ,时间280MS
换成 stdio.h + stdlib.h ,在C编译器下Memory是464K ,时间171MS
如果是stdio.h + stdlib.h在C++的编译器下测试呢?Memory是464K ,时间155MS

也就是说,同样的测试数据,要达到最好的效率,应该用纯C的方式写程序,并选择C++编译器judge程序.

posted on 2007-09-21 01:14 流牛ζ木马 阅读(1698) 评论(8)  编辑 收藏 引用

评论

# re: ACM PKU 1828 Monkeys' Pride 2008-12-02 18:12 aa

这题题目改了吧。。。你这代码过不了。  回复  更多评论   

# re: ACM PKU 1828 Monkeys' Pride 2009-08-01 09:33 幻风

明显过不了吧?
4
3 1
3 2
3 0
2 2
  回复  更多评论   

# re: ACM PKU 1828 Monkeys' Pride 2010-07-09 00:15 WallacePatti29

This is what I was exploring for a while! Thank you for this topic around college! Once someone state that In union there is might. Our high qualified team can help you in writing <a href="http://essaysexperts.com/">term paper</a>.  回复  更多评论   

# re: ACM PKU 1828 Monkeys' Pride 2010-07-09 16:43 dissertation writing service

Your well done article about this post comes side by side with the student dissertation. Hence, you must perform for dissertation service.   回复  更多评论   

# re: ACM PKU 1828 Monkeys' Pride 2010-07-09 17:29 thesis

Eventually, We have found best article just about this good post? We suggest to search the buy thesis or purchase french dissertation, just because this helps in getting the best grade if you have buy dissertation.   回复  更多评论   

# re: ACM PKU 1828 Monkeys' Pride 2010-09-06 14:54 resume writing service

The clients rely on our
resume service cause they are very responsible! This corporation performs resume writing to fit the precise field of science you wish.  回复  更多评论   

# re: ACM PKU 1828 Monkeys' Pride 2010-10-07 14:55 buying essays

To have good grades, some students have to decide if they are willing to accomplish the custom essay paper online or buy an essay paper of the best upper-class.   回复  更多评论   

# re: ACM PKU 1828 Monkeys' Pride 2012-01-13 07:40 book reports

University students would not have complications with their wars essays creating, because the papers writing corporations are able to sell essay of high quality.   回复  更多评论   

# re: ACM PKU 1828 Monkeys' Pride 2012-04-17 19:04 buy essay

Some time before I faced a lot of complications with essays writing. Nevertheles, my friend suggested to buy essays online. Thus, at this moment I have my A+.   回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2007年9月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

公告

MY Email/MSN :mars1021@163.com QQ : 27402040 流牛ζ木马

常用链接

留言簿(6)

随笔档案

相册

搜索

最新随笔

最新评论

阅读排行榜

评论排行榜