首页 > ACM题库 > HDU-杭电 > HDU 1519 Think Positive[解题报告] C++
2013
12-12

HDU 1519 Think Positive[解题报告] C++

Think Positive

问题描述 :

It is well known, that the year on planet Eisiem has n days. Of course, some days are very good for people, while some others are just horrible. Long observations have shown for each day of the year whether this day is good for most people, or bad.

The new president of the Planet Federation wants all people to be happy. He knows that good emotions have a tendency to accumulate, just like bad ones do. The New Year however is a special event and all emotions accumulated by this moment just disappear. Therefore the president wants to change the calendar on Eisiem and choose the new first day of the year, so that the positive emotions would prevail the whole year.

More precisely, for all i from 1 to n let ai be 1 if i-th day is good for most people and -1 if it is bad. Let sjk be the sum of ai for all days from the j-th day of the year to the k-th, that is:

President wants to find such j to order the j-th day to be the first day of the year, that sjk is positive for all k from 1 to n. Since he wants several variants to choose from, he asks you to find all such j. Since he doesn’t want to get too much information at once, first of all he wants to know the number of such j. That is exactly your task.

输入:

The input contains multiple test cases. The first line of the input is a single integer T (1 <= T <= 30) which is the number of test cases. T test cases follow, each preceded by a single blank line.

The first line of each test case contains n – the number of days (1 <= n <= 200 000). Next line contains n integer numbers – ai.

输出:

For each case, output the number of different indices j, such that sjk is positive for all k, in a single line.

样例输入:

2

5
1 -1 1 -1 1
7
1 1 1 1 1 1 1

样例输出:

1
7

#include<stdio.h>
int map[30][30][30];
int main()
{
    int a,b,c,i,j,k;
    for(i=0;i<=20;++i)
        for(j=0;j<=20;++j)
            for(k=0;k<=20;++k)
                if(i==0 || j==0 || k==0)
                    map[i][j][k]=1;
                else if(i<j && j<k)
                    map[i][j][k]=map[i][j][k-1]+map[i][j-1][k-1]-map[i][j-1][k-1];
                else
                    map[i][j][k]=map[i-1][j][k]+map[i-1][j-1][k]+map[i-1][j][k-1]-map[i-1][j-1][k-1];
    while(scanf("%d%d%d",&a,&b,&c),a!=-1 || b!=-1 || c!=-1)
    {
        if(a<=0 || b<=0 || c<=0)
            i=1;
        else if(a>20 || b>20 || c>20)
            i=map[20][20][20];
        else
            i=map[a][b][c];
        printf("w(%d, %d, %d) = %d\n",a,b,c,i);
    }
    return 0;
}

解题报告转自:http://blog.csdn.net/hong0220/article/details/8892045


  1. #include <cstdio>

    int main() {
    //answer must be odd
    int n, u, d;
    while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
    if(n<=u) { puts("1"); continue; }
    n-=u; u-=d; n+=u-1; n/=u;
    n<<=1, ++n;
    printf("%dn",n);
    }
    return 0;
    }

  2. 第23行:
    hash = -1是否应该改成hash[s ] = -1

    因为是要把从字符串s的start位到当前位在hash中重置

    修改提交后能accept,但是不修改居然也能accept