标签:
1
2
3
4
5
6
7
|
6
+ 3 1
+ 1 2
- 2 1
+ 3 1
+ 1 1
|
1
|
2 5
|
1 #include<iostream> 2 using namespace std; 3 #define inf -0x3f3f3f3f 4 struct node 5 { 6 int left, right, cur, num; 7 char ch; 8 }date[11]; 9 int main() 10 { 11 int n, i, j; 12 while (cin >> n) 13 { 14 int max = inf, k = 0; 15 date[1].num = 0, date[1].right = 1; 16 for (i = 2; i <= n; i++) 17 { 18 cin >> date[i].ch >> date[i].left >> date[i].cur; 19 date[i].right = i; 20 date[i].num = inf; 21 } 22 for (i = 1; i <= n; i++) 23 { 24 for (j = 1; j <= n; j++) 25 { 26 if (date[date[j].right].num != inf&&date[date[j].left].num == inf) 27 { 28 if (date[date[j].right].ch == ‘+‘) 29 date[date[j].left].num = date[date[j].right].num - date[date[j].right].cur; 30 else 31 date[date[j].left].num = date[date[j].right].num + date[date[j].right].cur; 32 } 33 else if (date[date[j].right].num == inf&&date[date[j].left].num != inf) 34 { 35 if (date[date[j].right].ch == ‘+‘) 36 date[date[j].right].num = date[date[j].left].num + date[date[j].right].cur; 37 else 38 date[date[j].right].num = date[date[j].left].num - date[date[j].right].cur; 39 } 40 if (date[j].num > max) 41 max = date[j].num; 42 } 43 } 44 for (i = 1; i <= n; i++) 45 { 46 if (date[i].num == max) 47 { 48 if (k++) cout << ‘ ‘; 49 cout << i; 50 } 51 } 52 cout << endl; 53 } 54 return 0; 55 }
然后后来又想到了一点优化,先把输入数据按照多余对象(比谁多)排序,
给每一组数据加上当前成员编号,然后按成员编号,直接按关系式相加就是,具体的看代码~~
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 struct node{ 6 int no, ptr, x; 7 char v; 8 bool operator<(const node &tmp)const{ 9 if (ptr != tmp.ptr) 10 return ptr < tmp.ptr; 11 return x < tmp.x; 12 } 13 }ss[11]; 14 int main(){ 15 int n, mpt[11], i, k = 0, maxn = -0x7ffffff; 16 memset(mpt, 0, sizeof(mpt)); 17 cin >> n; 18 for (i = 1; i < n; i++){ 19 cin >> ss[i].v >> ss[i].ptr >> ss[i].x; 20 ss[i].no = i; 21 } 22 sort(ss + 1, ss + n); 23 for (i = 1; i < n; i++){ 24 switch (ss[i].v){ 25 case ‘+‘:mpt[ss[i].no] = mpt[ss[i].ptr - 1] + ss[i].x; break; 26 case ‘-‘:mpt[ss[i].no] = mpt[ss[i].ptr - 1] - ss[i].x; break; 27 } 28 } 29 for (i = 0; i < n; i++) 30 maxn = maxn>mpt[i] ? maxn : mpt[i]; 31 for (i = 0; i < n; i++){ 32 if (mpt[i] == maxn){ 33 if (k++) cout << ‘ ‘; 34 cout << i + 1; 35 } 36 } 37 cout << endl; 38 return 0; 39 }
标签:
原文地址:http://www.cnblogs.com/zYx-ac/p/4572543.html