zoukankan      html  css  js  c++  java
  • 多校十 hdoj4393 Throw nails

    Throw nails

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 262    Accepted Submission(s): 94


    Problem Description
    The annual school bicycle contest started. ZL is a student in this school. He is so boring because he can't ride a bike!! So he decided to interfere with the contest. He has got the players' information by previous contest video. A player can run F meters the first second, and then can run S meters every second.
    Each player has a single straight runway. And ZL will throw a nail every second end to the farthest player's runway. After the "BOOM", this player will be eliminated. If more then one players are NO.1, he always choose the player who has the smallest ID.
     
    Input
    In the first line there is an integer T (T <= 20), indicates the number of test cases.
    In each case, the first line contains one integer n (1 <= n <= 50000), which is the number of the players.
    Then n lines follow, each contains two integers Fi(0 <= Fi <= 500), Si (0 < Si <= 100) of the ith player. Fi is the way can be run in first second and Si is the speed after one second .i is the player's ID start from 1.
    Hint

    Huge input, scanf is recommended.
    Huge output, printf is recommended.
     
    Output
    For each case, the output in the first line is "Case #c:".
    c is the case number start from 1.
    The second line output n number, separated by a space. The ith number is the player's ID who will be eliminated in ith second end.
     
    Sample Input
    2 3 100 1 100 2 3 100 5 1 1 2 2 3 3 4 1 3 4
     
    Sample Output
    Case #1: 1 3 2 Case #2: 4 5 3 2 1
    Hint
    Hint The first case: 1st Second end Player1 100m (BOOM!!) Player2 100m Player3 3m 2nd Second end Player2 102m Player3 103m (BOOM!!) 3rd Second end Player2 104m (BOOM!!)
     
    Source
     
    Recommend
    zhuyuanchen520
     
     
    这个题目意思是在一个循环赛  每一秒钟选择出跑得最远的让其淘汰  问其淘汰的顺序
    官方解题报告:
    直接暴力查找每秒最大值的n^2的做法会超时。
    法一
    考虑Fi最大只有500,所以501s之后只有 speed 对排名有影响(此时如果F也相同,则按ID顺序),排序即可。前501s暴力查找,然后直接按照排序结果输出。
    法二
    当 way 和 speed 呈二维不递增序列时,排名不会发生变化。排序后暴力查找到way和speed满足这个条件即可。
    法三
    考虑Si最大只有100,所以我们可以建立优先队列数组s[1..100],对于每个优先队列,按第一关键字Fi第二关键字ID排序,每次取出所有的优先队列里最大值,然后直接 计算(Time-1)*Si + Fi 找最大的way,将对应的优先队列pop并输出对应ID即可。  
    我用了第一种和第三种方法来做 一开始第一种自己出的案例都能过 第三种自己的案例过不了 报内存错误 可能是队列溢出  以为错了 从csdn里面弄了一份解题报告 还是内存错误 提交竟让能ac 我的也ac了 我晕死
    用第一种方法和第三种方法对拍了一下基本上全部对得到  真心弄不懂
    终于ac了  原来是按距离排序 开始我是按速度排序 哎 悲剧啊   不懂就不应该动手啊 
     
    View Code
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <time.h>
     5 #include <queue>
     6 using namespace std;
     7 #define  N 50005
     8 struct node 
     9 {
    10     int id,fir,end;
    11     /*    friend bool operator<(const node &s1,const node &s2)
    12     {
    13     if(s1.fir!=s2.fir) return s1.fir<s2.fir;
    14     else return s1.id>s2.id;
    15 }    */
    16 };
    17 
    18 node a[N];
    19 bool flag[N];
    20 bool cmp(const node &s1,const node &s2)
    21 {
    22     if (s1.end!=s2.end)    
    23         return s1.end<s2.end;
    24     else return s1.id>s2.id;
    25 }
    26 
    27 void intput() 
    28 {
    29     srand(time(NULL));
    30     freopen("1.txt","w",stdout);    
    31     int n,t=0;
    32     printf("1\n");
    33     while (t<1)
    34     {
    35         //n=rand()%500+1;
    36         n=500;
    37         printf("%d\n",n);
    38         for(int i=0;i<n;i++)
    39         {
    40             int a;
    41             a=rand()%500;
    42             int b=rand()%100+1;
    43             printf("%d %d\n",a,b);
    44         }    
    45         t++;
    46     }
    47 }
    48 int main()
    49 {
    50       intput();
    51     //freopen("1.txt","r",stdin);    
    52     //     freopen("out.txt","w",stdout);
    53     int n,T;
    54     scanf("%d",&T);
    55     for (int cas=1;cas<=T;cas++)
    56     {
    57         memset(flag,0,sizeof(flag));
    58         int i;
    59         scanf("%d",&n);
    60         for (i=1;i<=n;i++)
    61         {
    62             a[i].id=i;
    63             scanf("%d%d",&a[i].fir,&a[i].end);
    64         }
    65         printf("Case #%d:\n",cas);
    66         //sort(a,a+n,cmp);          
    67         for (i=0;i<501&&i<n;i++)
    68         {
    69             int max=-1;
    70             int low=9999999;
    71             for (int j=1;j<=n;j++)
    72             {
    73                 if (!flag[j])
    74                 {
    75                     int sum=a[j].fir+a[j].end*i;
    76                     if(sum>max||sum==max&&low>a[j].id) 
    77                     {
    78                         max=sum;
    79                         low=j;
    80                     }
    81                 }
    82             }
    83             flag[low]=1;
    84             if (!i)                    
    85                 printf("%d",low);
    86             else printf(" %d",low);            
    87                           }
    88                           if (n>500)
    89                           {
    90                               sort(a+1,a+n+1,cmp);
    91                               for (i=1;i<=n;i++)            
    92                                   if (!flag[i])                
    93                                       printf(" %d",a[i].id);                        
    94                           }
    95                           printf("\n");        
    96     }
    97     return 0;
    98 }    
    View Code
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <algorithm>
      4 #include <time.h>
      5 #include <queue>
      6 #include <cstring>
      7 using namespace std;
      8 #define  N 50005
      9 struct node
     10 {
     11     int id,fir,end;
     12 };
     13 
     14 node a[N];
     15 bool flag[N];
     16 bool cmp(const node &s1,const node &s2)
     17 {
     18     if (s1.fir!=s2.fir)
     19         return s1.fir>s2.fir;
     20     else return s1.id<s2.id;
     21 }
     22 
     23 void intput()
     24 {
     25     srand(time(NULL));
     26     freopen("1.txt","w",stdout);
     27     int n,t=0;
     28     printf("1\n");
     29     while (t<1)
     30     {
     31         //n=rand()%500+1;
     32         n=1000;
     33         printf("%d\n",n);
     34         for(int i=0; i<n; i++)
     35         {
     36             int a;
     37             a=rand()%500;
     38             int b=rand()%100+1;
     39             printf("%d %d\n",a,b);
     40         }
     41         t++;
     42     }
     43 }
     44 int main()
     45 {
     46     //intput();
     47     // freopen("1.txt","r",stdin);
     48     //freopen("out.txt","w",stdout);
     49     int n,T;
     50     scanf("%d",&T);
     51     for (int cas=1; cas<=T; cas++)
     52     {
     53         memset(flag,0,sizeof(flag));
     54         int i;
     55         scanf("%d",&n);
     56         for (i=1; i<=n; i++)
     57         {
     58             a[i].id=i;
     59             scanf("%d%d",&a[i].fir,&a[i].end);
     60         }
     61         printf("Case #%d:\n",cas);
     62         // sort(a+1,a+n+1,cmp);
     63         for (i=0; i<=1000&&i<n; i++)
     64         {
     65             int max=-1;
     66             int low=9999999;
     67             for (int j=1; j<=n; j++)
     68                 if (!flag[j])
     69                 {
     70                     int sum=a[j].fir+a[j].end*i;
     71                     if(sum>max)
     72                     {
     73                         max=sum;
     74                         low=a[j].id;
     75                     }
     76                 }
     77             flag[low]=1;
     78             if (!i)
     79                 printf("%d",low);
     80             else printf(" %d",low);
     81         }
     82         if (n>1000)
     83         {
     84             node pp[N];
     85             int ff=0;
     86           //  sort(a+1,a+n+1,cmp);
     87             for (i=1; i<=n; i++)
     88                 if (!flag[a[i].id])
     89                 {
     90                     pp[ff].fir=a[i].fir+501*a[i].end;
     91                     pp[ff++].id=a[i].id;
     92                 }
     93             sort(pp,pp+ff,cmp);
     94             for (i=0; i<ff; i++)
     95                 printf(" %d",pp[i].id);
     96         }
     97         printf("\n");
     98     }
     99     return 0;
    100 }
  • 相关阅读:
    (九)MySQL数据库
    (八)其他组件(redis、zk、dubbo、MQ、ES)
    (七)Spring体系
    (六)JVM虚拟机
    (五)并发编程与锁机制
    (四)常用集合与原理
    (三)JDK版本区别
    k8s scheduler framework 配置weight不生效的问题
    Elasticsearch ILM delete not working
    etcd 依赖采坑
  • 原文地址:https://www.cnblogs.com/wujianwei/p/2653835.html
Copyright © 2011-2022 走看看