HDU 4393 Throw nails (STL)

题意:

T组测试数据,每组N个(从1到N)二元一次函数(y=kx+b)给出ki和bi,求x=0,1,2,3.......时,y最大的那个,并删去

思路一:

考虑到本题的数据范围,0<k<=100,且k,b都为整数,可以对所有函数按K分类,每次只比较100个K集当中B最大的那个。即每求一个x,比较100次。

x<=50000,最多跑5W*100次。

思路二:

考虑到本题的数据范围,0<=b<=500,且k,b都为整数,当x大于501时,函数大小次序就只与k有关,b的影响可以忽略。

故先暴力枚举前501组,剩下的按k序输出

代码:

思路一(AC):

#include <bits/stdc++.h>

using namespace std;
const int MAXN=50010;
const int INF=0x3f3f3f3f;
typedef struct Node{
    int b,id;
    Node(int bb=0,int ii=0){b=bb;id=ii;}
    bool operator < (const Node &a)const{
        if(a.b==b)
            return a.id<id;
        return a.b>b;
    }
}Node;
priority_queue <Node> k[105];

int main()
{
    int T,n,b,kk;
    scanf("%d",&T);
    for(int ppp=1;ppp<=T;ppp++){
        printf("Case #%d:\n",ppp);
        for(int i=0;i<105;i++)
            while(!k[i].empty())
                k[i].pop();
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&b,&kk);
            k[kk].push(Node(b,i));
        }
        for(int i=0;i<n;i++){
            int ans=-1;
            int ansj=-1;
            int maxn=-1;
            for(int j=1;j<101;j++){
                if(!k[j].empty()){
                    Node temp=k[j].top();
                    if(temp.b+i*j==maxn){
                        if(temp.id<ans){
                            ans=temp.id;
                            ansj=j;
                        }
                    }else if(temp.b+i*j>maxn){
                        ans=temp.id;
                        ansj=j;
                        maxn=temp.b+i*j;
                    }
                }
            }
            if(i)
                printf(" %d",ans);
            else
                printf("%d",ans);
            k[ansj].pop();
        }
        printf("\n");
    }
}


思路二(TLE):

#include <bits/stdc++.h>

using namespace std;
const int MAXN=50010;
const int INF=0x3f3f3f3f;
typedef struct Node{
    int now,sp,id;
    Node(int nn=0,int ss=0,int ii=0){now=nn;sp=ss;id=ii;}
    bool operator < (const Node &a)const{
        if(a.now==now)
            return a.id>id;
        return a.now<now;
    }
}Node;
Node a[MAXN];
bool cmp(Node b,Node c){
    if(b.sp==c.sp)
        return b.now<c.now;
    return b.sp<c.sp;
}
void solve(int len){
    for(int i=0;i<len;i++)
        a[i].now+=a[i].sp;
    sort(a,a+len);
    printf(" %d",a[0].id);
    a[0].now=-INF;
    a[0].sp=-1;
    return ;
}
int main()
{
    int T,n;
    scanf("%d",&T);
    for(int ppp=1;ppp<=T;ppp++){
        printf("Case #%d:\n",ppp);
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d",&a[i].now,&a[i].sp);
            a[i].id=i+1;
        }
        int lop=n>501?501:n;
        sort(a,a+n);
        printf("%d",a[0].id);
        a[0]=Node(-INF,-1,a[0].id);
        for(int i=0;i<lop-1;i++)
            solve(n);
        if(n>501){
            sort(a,a+n,cmp);
            for(int i=0;i<n-501;i++)
                printf(" %d",a[i].id);
        }
        printf("\n");
    }
}




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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值