#18.03.09 vijos1011清帝之惑之顺治

背景

顺治帝福临,是清朝入关后的第一位皇帝。他是皇太极的第九子,生于崇德三年(1638)崇德八年八月二ten+six日在沈阳即位,改元顺治,在位18年。卒于顺治十八年(1661),终24岁。

顺治即位后,由叔父多尔衮辅政。顺治七年,多尔衮出塞射猎,死于塞外。14岁的福临提前亲政。顺治帝天资聪颖,读书勤奋,他吸收先进的汉文化,审时度势,对成法祖制有所更张,且不顾满洲亲贵大臣的反对,倚重汉官。为了使新兴的统治基业长治久安,他以明之兴亡为借鉴,警惕宦官朋党为祸,重视整饬吏治,注意与民休息,取之有节。但他少年气盛,刚愎自用,急噪易怒,当他宠爱的董妃去世后,转而消极厌世,终于匆匆走完短暂的人生历程,英年早逝。他是清朝历史上唯一公开归依禅门的皇帝。

描述

顺治喜欢滑雪,这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待太监们来载你。顺治想知道载一个区域中最长的滑坡。

区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子:

1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

顺治可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。

格式

输入格式

输入的第一行表示区域的行数R和列数C(1 <= R,C <= 500)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。

输出格式

输出最长区域的长度。

样例1

样例输入1

5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

样例输出1

25

限制

各个测试点2s

来源

Vivian Snow

 1 #include <iostream>
 2 #include <string.h>
 3 #include <cstdio>
 4 #include <stdlib.h>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 struct area
 9 {
10     int x, y;//坐标 (x,y) (行,列)
11     int high;//高度
12 };
13 
14 int r, c;
15 area f[250002];
16 int dp[505][505] = { 0 }, map[505][505];
17 
18 int comp(const void *q, const void *w)
19 {
20     return ((struct area*)q)->high - ((struct area*)w)->high;
21 }
22 
23 int main()
24 {
25     int direr[4] = { 0,0,-1,1 }, direl[4] = { -1,1,0,0 };
26     cin >> r >> c;//r行数 c列数
27     int count = 0;
28     for (int i = 1; i <= r; i++)
29         for (int j = 1; j <= c; j++)
30         {
31             cin >> f[count].high;
32             f[count].x = i;
33             f[count].y = j;
34             map[i][j] = f[count].high;
35             count++;
36         }
37     qsort(f, count, sizeof(struct area), comp);
38     for (int i = 0; i<count; i++)
39     {
40         for (int j = 0; j <= 3; j++)
41         {
42             if (map[f[i].x + direr[j]][f[i].y + direl[j]]>map[f[i].x][f[i].y])
43                 dp[f[i].x + direr[j]][f[i].y + direl[j]] = max(dp[f[i].x + direr[j]][f[i].y + direl[j]], dp[f[i].x][f[i].y] + 1);
44         }
45     }
46     int max0 = 0;
47     for (int i = 1; i <= r; i++)
48         for (int j = 1; j <= c; j++)
49             max0 = max(max0, dp[i][j]);
50     printf("%d
", max0+1);
51     return 0;
52 }
View Code

总算是搞出来了

然而耗时实在太多

#    状态    耗时    内存占用
#1     Accepted     197ms    7.0 MiB
#2     Accepted     49ms    3.898 MiB
#3     Accepted     73ms    3.582 MiB
#4     Accepted     27ms    2.719 MiB
#5     Accepted     128ms    6.75 MiB
#6     Accepted     26ms    2.75 MiB
#7     Accepted     11ms    2.969 MiB
#8     Accepted     99ms    6.0 MiB
#9     Accepted     32ms    3.27 MiB
#10     Accepted     90ms    6.215 MiB

到学了dp还有复杂度啥的再回过头来看看吧………

以及顺治真的喜欢滑雪么(小小声)

注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
原文地址:https://www.cnblogs.com/yalphait/p/8535495.html