HNU Profits

来源:互联网 发布:域名注册的要求 编辑:程序博客网 时间:2024/05/17 03:08

ProfitsTime Limit: 2000ms,Special Time Limit:5000ms, Memory Limit:65536KBTotal submit users: 13,Accepted users: 13 Problem 13042 : No special judgementProblem description

In a business of diamond mining from a river, the government allows you to put the machine in theriver only once, and you must remove the machine passing certain deadline. However, you can removeit at any time after you put the machine in the river. The machine in your company requires diamond inorder to operate (it is used as the drilling head). Your company has a smart detector that can predict thenumber of diamond that you will gain or loss each day, from the first day to the last day that thegovernment grants the request. You are tasked with creating a program. This program finds the periodthat your company can gain the most diamond.



Input

The first line contains one number, N ? the total number of test cases, 1 <= N <= 2000. Each of thefollowing N lines contains a single test case. Each test case has M + 1 numbers. The first number is M? the total number of days granted by the government, 1<= M <= 500. The following M numbers arethe predicted of gain and loss of diamond on each day. The number of diamond loss or gains is within[-100,100]. All input numbers are integer.q



Output

The output contains N lines (one line per one test case). For each test case, the program must print out 3numbers, the beginning day, the ending day, and the maximum total profits. The first day in the input isconsidered “1”. The last day in the input is considered “M”. If there is no positive profit, the outputmust be “0 0 0”. In case of a tie of profits of two time periods, choose the time period with is shorterfirst. If the time periods and profits are the same for both periods, pick the time period that begins first.



Sample Input
616 4 3 -10 3 -1 2 0 -3 5 7 -4 -8 -10 4 7 -306 -1 0 -3 -1 0 -110 -1 -4 -5 -9 -14 5 6 7 -10 103 -2 -3 -417 1 2 3 4 5 6 7 8 9 -10 -11 10 9 8 -7 -6 137 1 2 3 -10 3 2 1
Sample Output
4 10 130 0 06 8 180 0 01 14 511 3 6
 

题意说的乱七八糟的  其实就是求最大连续子序列和 然后还要输出子序列的起点和终点

很简单的dp 主要是记录下标  用s[i]数组存储以i位置作为子序列重点时  子序列起点的下标

这个题有输出的要求 要是利益相同输出时间短的  要是利益跟时间都相同就输出最先出现的

#include<iostream>#include <cstdio>using namespace std;int s[10001],a[10001],f[10001];int main(){//freopen("test.txt","r",stdin);    int n,i,mx,st,ed;    bool flag;    int tc;    scanf("%d",&tc);    while(tc--)    {        int n;        scanf("%d",&n);        for(i=1; i<=n; i++)            scanf("%d",&a[i]);        f[1]=a[1];        s[1]=1;        for(i=2; i<=n; i++)        {            if(f[i-1]+a[i]>a[i])            {                f[i]=f[i-1]+a[i];                s[i]=s[i-1];            }            else            {                f[i]=a[i];                s[i]=i;            }        }        /* for(i=1;i<=n;i++)        cout<<f[i]<<" ";          cout<<endl;*/        mx=0;        st=1;        ed=1;        for(i=1; i<=n; i++)        {            if(f[i]>mx)            {                mx=f[i];                st=s[i];                ed=i;            }            if(f[i]==mx)            {                int l=s[i];                int r=i;                if((r-l)<(ed-st))                {                    st=l;                    ed=r;                }            }        }        if(mx>0)            cout<<st<<" "<<ed<<" "<<mx<<endl;        else            cout<<0<<" "<<0<<" "<<0<<endl;    }return 0;}




0 0
原创粉丝点击