codeforces GYM 100500 Problem H - ICPC Quest

Problem H. ICPC Quest

Time limit     10000 ms

Memory limit     262144 kB

Noura Boubou is a Syrian volunteer at ACM ACPC (Arab Collegiate Programming Contest) since 2011. She graduated from Tishreen University where she studied Information Technology. She is the head of the photography team responsible for photo-shooting the Arabs joining the world finals.

On the first day of the world finals last year, Dr Mohamed Fo’ad, the ACPC Regional Contest Director, asked Noura to play one of the ICPC Quests. ICPC Quests are a set of challenges that might require the contestants to move around the city searching for some monuments, solving puzzles, or getting a high score in a certain game. The rules of the quests states that the contestants will post the answer to the quest to Twitter using these hashtags #ICPC2014 #QuestN where N is the quest number. The contestant will post a photo or a short video (a Vine) of the challenge he accomplished. The scores of the challenges are accumulated and the one with the highest score will get an Android tablet.

Noura was so enthusiastic about the Quest number 14. Quest 14 was about a grid of n rows and m columns, and each cell of the grid contains an integer, and whenever a contestant steps on a cell he is going to add its value to his total accumulated sum. The task was to start from the top left cell located at (1,1) and to move only right or down in order to get to cell (n,m), and during this journey the contestant has to get the maximum sum possible. The player who can get the maximum possible score is announced as the winner of this quest.

You will be given the description of the grid, please help Noura determining the maximum possible score she can get.

Input

The first line will be the number of test cases T. Each test case starts with 2 integers n, m where n is the number of rows while m is the number of columns. They will be followed by n rows each containing m numbers, and the absolute value of the number in each cell will not exceed 100.

1 <= T <= 100

1 <= n,m <= 1000

-100 <= celli,j <= 100

Output

For each test case print a single line containing:

Case_x:_y

x is the case number starting from 1.

y is is the required answer.

Replace underscores with spaces.

Input

2

3    3

 1   2   3

-1  -2 -3

 1   1   1

2   3

1   1   2

2   1   5

Output

Case 1: 4

Case 2: 9

简单dp。。

从左上角走到右下角, 求走过路径数字加和的最大值。 你现在的位置只可能是你从左边或者上边走过来的, 因此只要找出当前位置的左边和上边的最大值并加上当前位置的值就是当前位置的最大值。第一行和第一列要特判。

#include <bits/stdc++.h>

using namespace std;

int d[1010][1010], a[1010][1010];
const int INF = 0x3f3f3f3f; //int型的无穷大。
int main(){
    int t, m, n, cnt;
    scanf("%d", &t);
    cnt = 0;
    while(t--){
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++)
                scanf("%d", &a[i][j]);
        }
        memset(d, -3-0x3f3f3f3f, sizeof(d));
        d[0][1] = 0, d[1][0] = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++)
                d[i][j] = max(d[i - 1][j], d[i][j - 1]) + a[i][j];
        }
        printf("Case %d: %d\n", ++cnt, d[n][m]);
    }
    return 0;
}

险些超时。。后台测试数据运行3秒多。。

后来看了大佬们的代码,思路和方法都基本差不多,但是一看运行时间竟然0.6秒。。

WTF????

发现读入的方式不一样, 大佬还是大佬啊。

贴下大佬的读入方式。

贼快

inline long long read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

看了半天才看懂。。这方法真鸡儿六, get到了。

猜你喜欢

转载自blog.csdn.net/zx__zh/article/details/82081542