zoukankan      html  css  js  c++  java
  • HDU destroy the well of life

    Destroy the Well of Life

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 556    Accepted Submission(s): 197


    Problem Description
    In the game of DotA (Defense of the Ancient), there are two opposite legions called The Sentinel and The Scourage. 
    Now The Scourage has made a big success and The Sentinel is at stake!
    So The Sentinel must destroy the Well of Life of The Scourage.
    The chief of The Sentinel, Prophet, asks EarthShaker to do this arduous task.

    There are N Wells of Life of The Scourage (The Wells of Life are numbered from 1 to N), and EarthShaker’s task is to destroy the Nth Well of Life.
    The following information is known about each Well of Life:
    Wi – the weight of water on i-th Well of Life before it is destroyed.
    Li – if the weight of water on i-th Well of Life is more than Li, the i-th Well of Life will be destroyed and the water of it will pours to the (i + 1)-th Well of Life.
    Pi – EarthShaker has a skill called Echo-Slam, the i-th Well of Life will be immediately destroyed when he uses Echo-Slam to it and the water of it will pours to the (i + 1)-th Well of Life. For the i-th Well of Life, the energy that EarthShaker need to use Echo-Slam to destroy it is Pi. 

    Can you tell EarthShaker the minimum amount of energy needed to destroy the Nth Well of Life? 

    hdu <wbr>1692 <wbr>Destroy <wbr>the <wbr>Well <wbr>of <wbr>Life
     
    Input
    The input consists of several test cases. There is a single number on the first line, the number of cases. There are no more than 10 cases.
    Each case contains a natural number N on the first line, 1<=N<=100,000.
    Following N lines contains three numbers Wi, Li, Pi (Wi<=Li, 0<=Wi, Li, Pi <=20,000), representing the information of the i-th Well of Life.
     
    Output
    For each case, print a number in a line representing the least amount of energy EarthShaker needed to use, so as to destroy the Nth Well of Life. Use the format in the sample.
     
    Sample Input
    
    
    1000 1000 1 
    0 1000 2 
    2 10 100
     
    一开始我读错题了,以为是全部毁掉。交了不下十次,结果去网上搜代码,还不屑一顾的以为那些都是错的(只有俩个代码(一个是集合吧可能没大看懂))这个是AC的代码。
    View Code
    #include<stdio.h>
    
    int main()
    {
    
        int t,i,j,n;
        long count = 0,mindai = 0,dai = 0,sum = 0;
        int w[100005],p[100005],l[100005];
        scanf("%d",&t);
        while(t--)
        {
            mindai = 0,dai = 0,sum = 0;
    
            scanf("%d",&n);
    
            for(i = 0;i < n;i++)//这样可以省去一部分的时间,直接找到从一开始的代价
            {
                scanf("%d%d%d",&w[i],&l[i],&p[i]);
                sum += w[i];
                if(sum <= l[i])
                    mindai += p[i];
    
            }
    
            for(i = 1;i < n;i++)//然后从第二个开始枚举找就是。
            {
                dai = sum = 0;
                for(j = i;j < n;j++)
                {
                    sum += w[j];
                    if(sum <= l[j])
                    {    
                        dai += p[j];                   
                    }
                    if(dai > mindai )
                        break;
                }
                if(mindai > dai)
                    mindai = dai;
            }
            printf("Case %ld: Need to use %ld mana points.\n",++count,mindai);
        }
        return 0;
    }

    这个是全部毁坏用的代码,我觉得如果题目该饿了的话应该也不会超时。

    View Code
    #include<stdio.h>
    __int64 w[100005],p[100005],l[100005];
    int main()
    {
    
        int t;
        __int64 count = 0;
        scanf("%I64d",&t);
        while(t--)
        {
            __int64 i,j,n;
    
            scanf("%I64d",&n);
    
            for(i = 0;i < n;i++)
            {
                scanf("%I64d%I64d%I64d",&w[i],&l[i],&p[i]);
            }
    
            __int64 mindai = p[n-1],dai = 0;
            __int64 sum = 0,maxw = w[n-1];
            for(i = n-1;i >=0;i--)
            {
                dai = 0;
                sum = 0;
                if(maxw < w[i])
                    maxw = w[i];
                for(j = i;j <n;j++)
                {
                    
                    if(sum > l[j])
                        sum += w[j];
                    else
                    {    
                        sum += w[j],dai += p[j];
                        if(mindai + p[i] < dai)//上次最小值加上本次如果大于现在的代价就直接跳出。
                        {
                            break;
                        }
                        
                    }
                    if(sum > maxw )
                        break;
                }
                if(mindai + p[i] >= dai)
                    mindai = dai;
                else
                    mindai += p[i];
            }
            printf("Case %I64d: Need to use %I64d mana points.\n",++count,mindai);
        }
        return 0;
    }
     
     
  • 相关阅读:
    GO学习之 为什么选择GO
    Flask学习之 Jinja2模板引擎
    排序算法之归并排序的python实现
    排序算法之快速排序的python实现
    排序算法之希尔排序的python实现
    排序算法之插入排序的python实现
    排序算法之选择排序的python实现
    Android 中正则表达式工具类
    java之从字符串比较到==和equals方法区别
    Android Studio酷炫插件(一)——自动化快速实现Parcelable接口序列化
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2464772.html
Copyright © 2011-2022 走看看