Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

第一次贴wa的代码让高手们给我查错,真不好意思,我也用stl的heap+greedy

Posted by yuanyirui at 2007-04-03 08:31:19 on Problem 2431
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
struct Lo{
    int oil,lo;
    bool friend operator < (Lo a,Lo b)
    {
         return a.lo>b.lo;
    }
}s[10001];
int main()
{
  //  freopen("in.txt","r",stdin);
  //  freopen("out.txt","w",stdout);
    int n,l,p,i,j,k,cover,len,ans; //最后三个分别是 最大到达距离,堆长度,计算结果 
    int sta[10001];  // 油站的堆数组 
    while(scanf("%d",&n)!=EOF){
        for(i=0;i<n;i++) scanf("%d%d",&s[i].lo,&s[i].oil);
        sort(s,s+n);  // 按路程从大到小排序 
        scanf("%d%d",&l,&p);
        cover=l-p;  //不用加油能走到位置 
        len=ans=0;
        for(i=0;i<n;i++) if(s[i].lo<=l) break;   //把在原始位置后面的省略  
        while(cover>0){  // 只要能继续前行 
            for(;i<n;i++){
                if(s[i].lo<=cover){ // 在能走到的范围内,都把油加进堆中 
                    sta[len++]=s[i].oil;  // 压堆 
                    push_heap(sta,sta+len); 
                }
            }
            if(len==0) break; //如果堆中没有元素,而cover的路程又没有到终点,则失败 
            cover -= sta[0];  // 减去最大的油 
            ans++;           //计数器加1 
            pop_heap(sta,sta+len); // 出堆 
            len--;
        }
        if(cover>0) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator