250:
题目大意:
在一个N行无限大的网格图里,每经过一个格子都要付出一定的代价。同一行的每个格子代价相同。 给出起点和终点,求从起点到终点的付出的最少代价。
思路:
最优方案肯定是从起点沿竖直方向走到某一行,然后沿水平方向走到终点那一列,然后再沿竖直方向走到终点那一行。
枚举是通过哪一行的格子从起点那列走到终点那列的,求个最小值就好了。
代码:
1 // BEGIN CUT HERE
2
3 // END CUT HERE
4 #line 5 "LongMansionDiv1.cpp"
5 #include <vector>
6 #include <list>
7 #include <map>
8 #include <set>
9 #include <deque>
10 #include <stack>
11 #include <bitset>
12 #include <algorithm>
13 #include <functional>
14 #include <numeric>
15 #include <utility>
16 #include <sstream>
17 #include <iostream>
18 #include <iomanip>
19 #include <cstdio>
20 #include <cmath>
21 #include <cstdlib>
22 #include <ctime>
23 #include <cstring>
24 using namespace std;
25
26 typedef long long ll;
27
28 class LongMansionDiv1
29 {
30 public:
31 ll F(int i, int j, vector <int> &t)
32 {
33 ll res = 0;
34 if (i > j) swap(i, j);
35 for (int k = i; k <= j; ++k)
36 res += t[k];
37 return res;
38 }
39 long long minimalTime(vector <int> t, int sX, int sY, int eX, int eY)
40 {
41 //$CARETPOSITION$
42 int n = t.size();
43 if (sY > eY) swap(sX, eX), swap(sY, eY);
44 ll ans = 1e18;
45 for (int j = 0; j < n; ++j)
46 {
47 ans = min(ans, F(sX, j, t) + 1ll * t[j] * (eY - sY + 1) + F(j, eX, t) - t[j] - t[j]);
48 }
49 return ans;
50 }
51
52 // BEGIN CUT HERE
53 public:
54 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
55 private:
56 template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '"' << *iter << "","; os << " }"; return os.str(); }
57 void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << " Expected: "" << Expected << '"' << endl; cerr << " Received: "" << Received << '"' << endl; } }
58 void test_case_0() { int Arr0[] = {5, 3, 10}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 0; int Arg3 = 2; int Arg4 = 2; long long Arg5 = 29LL; verify_case(0, Arg5, minimalTime(Arg0, Arg1, Arg2, Arg3, Arg4)); }
59 void test_case_1() { int Arr0[] = {5, 3, 10}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 2; int Arg3 = 0; int Arg4 = 0; long long Arg5 = 15LL; verify_case(1, Arg5, minimalTime(Arg0, Arg1, Arg2, Arg3, Arg4)); }
60 void test_case_2() { int Arr0[] = {137, 200, 184, 243, 252, 113, 162}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 2; int Arg3 = 4; int Arg4 = 2; long long Arg5 = 1016LL; verify_case(2, Arg5, minimalTime(Arg0, Arg1, Arg2, Arg3, Arg4)); }
61 void test_case_3() { int Arr0[] = {995, 996, 1000, 997, 999, 1000, 997, 996, 1000, 996, 1000, 997, 999, 996, 1000, 998, 999, 995, 995, 998, 995, 998, 995, 997, 998, 996, 998, 996, 997, 1000, 998, 997, 995, 1000, 996, 997, 1000, 997, 997, 999, 998, 995, 999, 999, 1000, 1000, 998, 997, 995, 999}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 18; int Arg2 = 433156521; int Arg3 = 28; int Arg4 = 138238863; long long Arg5 = 293443080673LL; verify_case(3, Arg5, minimalTime(Arg0, Arg1, Arg2, Arg3, Arg4)); }
62 void test_case_4() { int Arr0[] = {1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 0; int Arg3 = 0; int Arg4 = 0; long long Arg5 = 1LL; verify_case(4, Arg5, minimalTime(Arg0, Arg1, Arg2, Arg3, Arg4)); }
63
64 // END CUT HERE
65
66 };
67
68 // BEGIN CUT HERE
69 int main()
70 {
71 LongMansionDiv1 ___test;
72 ___test.run_test(-1);
73 system("pause");
74 }
75 // END CUT HERE
500:
题目大意:
给出一棵以0为根的树,从根出发,走过一些节点。 每个节点有一个得分,可能正可能负,可以重复经过节点,但是只有第一次经过会改变当前得分。 如果当前得分为负,会马上变成0.
求最大得分。
比赛的时候没有想出来,好菜菜QAQ。
思路:
这道题主要是 “如果当前得分为负,会马上变成0” 这个地方不好处理。
考虑最后一次得分由负变成0的时刻。这个时候访问过的节点也组成以0为根的树,可以认为这棵树上的节点的得分都是0.
因此可以把问题转化一下:
可以把包含节点0的一个联通块内的节点得分都变成0, 然后再求一个包含节点0的联通块得分和最大。
用A,B两个数组来做DP。
A[x]表示考虑以x为根的子树,且x必须取的最大值。
B[x]表示考虑以x为根的子树,且允许把x的权值变成0(相当于允许不取x)的最大值。
A[x] = max(val[x] + sum(A[sons of x]), 0)
B[x] = max(sum(B[sons of x], A[x]))
代码:
1 // BEGIN CUT HERE
2
3 // END CUT HERE
4 #line 5 "OwaskiAndTree.cpp"
5 #include <vector>
6 #include <list>
7 #include <map>
8 #include <set>
9 #include <deque>
10 #include <stack>
11 #include <bitset>
12 #include <algorithm>
13 #include <functional>
14 #include <numeric>
15 #include <utility>
16 #include <sstream>
17 #include <iostream>
18 #include <iomanip>
19 #include <cstdio>
20 #include <cmath>
21 #include <cstdlib>
22 #include <ctime>
23 #include <cstring>
24 using namespace std;
25 class OwaskiAndTree
26 {
27 public:
28 int maximalScore(vector <int> parent, vector <int> pleasure)
29 {
30 //$CARETPOSITION$
31 int n = pleasure.size();
32 long long a[1010] = {0}, b[1010] = {0};
33 for (int i = n - 1; i >= 0; --i)
34 {
35 a[i] += pleasure[i];
36 a[i] = max(a[i], 0ll);
37 b[i] = max(b[i], a[i]);
38
39 if (i == 0) continue;
40 int u = parent[i - 1];
41 a[u] += max(a[i], 0ll);
42 b[u] += b[i];
43 }
44 return b[0];
45 }
46
47 // BEGIN CUT HERE
48 public:
49 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
50 private:
51 template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '"' << *iter << "","; os << " }"; return os.str(); }
52 void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << " Expected: "" << Expected << '"' << endl; cerr << " Received: "" << Received << '"' << endl; } }
53 void test_case_0() { int Arr0[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 1, -1, -1, -1, -1, 1, 1, 1, 1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 4; verify_case(0, Arg2, maximalScore(Arg0, Arg1)); }
54 void test_case_1() { int Arr0[] = {0, 0, 1, 2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 3, 4, -1, -1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 9; verify_case(1, Arg2, maximalScore(Arg0, Arg1)); }
55 void test_case_2() { int Arr0[] = {0, 0, 1, 1, 2, 2, 5, 5}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 2, -3, -7, 3, 2, 7, -1, 3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 17; verify_case(2, Arg2, maximalScore(Arg0, Arg1)); }
56 void test_case_3() { int Arr0[] = {0, 1, 1, 1, 0, 3, 1, 3, 4, 4, 3, 6, 8, 0, 12, 12, 11, 7, 7}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {-154011, 249645, 387572, 292156, -798388, 560085, -261135, -812756, 191481, -165204, 81513, -448791, 608073, 354614, -455750, 325999, 227225, -696501, 904692, -297238}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3672275; verify_case(3, Arg2, maximalScore(Arg0, Arg1)); }
57 void test_case_4() { int Arr0[] = {}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {-1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(4, Arg2, maximalScore(Arg0, Arg1)); }
58
59 // END CUT HERE
60
61 };
62
63 // BEGIN CUT HERE
64 int main()
65 {
66 OwaskiAndTree ___test;
67 ___test.run_test(-1);
68 system("pause");
69 }
70 // END CUT HERE