Skip to content

Commit a65f97b

Browse files
committed
add solutions for problems
1 parent 881d07b commit a65f97b

9 files changed

+634
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
n = int(input())
2+
arr = list(map(int, input().split()))
3+
4+
max_val = max(arr)
5+
if max_val < 0:
6+
print(0, arr[0], arr[-1])
7+
elif max_val == 0:
8+
idx = arr.index(0)
9+
print(0, arr[idx], arr[idx])
10+
else:
11+
curr, ans, start, l, r = 0, 0, 0, 0, 0
12+
for i, x in enumerate(arr):
13+
curr += x
14+
if curr > ans:
15+
ans, l, r = curr, start, i
16+
if curr < 0:
17+
curr = 0
18+
start = i + 1
19+
print(ans, arr[l], arr[r])
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#define _CRT_SECURE_NO_DEPRECATE
2+
#define _SECURE_SCL 0
3+
#pragma comment(linker, "/STACK:66777216")
4+
#include <algorithm>
5+
#include <string>
6+
#include <complex>
7+
#include <cassert>
8+
#include <memory>
9+
#include <set>
10+
#include <stack>
11+
#include <map>
12+
#include <list>
13+
#include <deque>
14+
#include <numeric>
15+
#include <cctype>
16+
#include <cstddef>
17+
#include <vector>
18+
#include <queue>
19+
#include <iostream>
20+
#include <iomanip>
21+
#include <iterator>
22+
#include <cmath>
23+
#include <cstdio>
24+
#include <cstdlib>
25+
#include <sstream>
26+
#include <fstream>
27+
#include <ctime>
28+
#include <cstring>
29+
#include <functional>
30+
#include <bitset>
31+
using namespace std;
32+
33+
#if defined(_MSC_VER) || defined(__BORLANDC__)
34+
typedef unsigned __int64 uint64;
35+
typedef signed __int64 int64;
36+
#else
37+
typedef unsigned long long uint64;
38+
typedef signed long long int64;
39+
#endif
40+
typedef vector<int> VI;
41+
typedef vector<string> VS;
42+
typedef pair<int, int> PII;
43+
typedef pair<int64, int64> PLL;
44+
typedef vector<int64> VL;
45+
46+
#define pb push_back
47+
#define ppb pop_back
48+
#define mp make_pair
49+
#define fi first
50+
#define se second
51+
#define pii pair<int,int>
52+
#define pdd pair<double,double>
53+
#define FOR(i,a,b) for (int _n(b), i(a); i <= _n; i++)
54+
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;i--)
55+
#define all(c) (c).begin(), (c).end()
56+
#define SORT(c) sort(all(c))
57+
#define REP(i,n) FOR(i,1,(n))
58+
#define REPT(i,n) FOR(i,0,(n)-1)
59+
#define L(s) (int)((s).size())
60+
#define C(a) memset((a),0,sizeof(a))
61+
#define IOS ios::sync_with_stdio(false)
62+
63+
const double pi = 3.1415926535897932384626433832795028841971;
64+
const double EPS = 1E-9;
65+
const int64 INF64 = ( int64 )1E18;
66+
const int INF = 1000000000;
67+
68+
static inline bool get( int &v ) {
69+
int s = 1, c;
70+
while( !isdigit( c = getchar() ) && c != '-' )
71+
if( c == EOF ) break ;
72+
if( c == EOF ) return 0;
73+
if( c == '-' ) s = 0 , v = 0;
74+
else v = c ^ 48;
75+
for( ; isdigit( c = getchar() ); v = ( v << 1 ) + ( v << 3 ) + ( c ^ 48 ) );
76+
v = ( s ? v : -v );
77+
return 1 ;
78+
}
79+
80+
/*
81+
注意链表的起始地址可以从中间的节点开始,最后一个Test坑死了
82+
*/
83+
84+
struct node_line
85+
{
86+
int address, data, next;
87+
}tmp;
88+
89+
pair<int, int> node[100099];
90+
91+
vector< node_line > vList;
92+
void run() {
93+
int start, n, k, i, j, prev, data, nxt;
94+
cin >> start >> n >> k;
95+
for (i = 0; i < n; i++) {
96+
cin >> prev >> data >> nxt;
97+
node[prev].first = data;
98+
node[prev].second = nxt;
99+
}
100+
int cur_node = start;
101+
while (cur_node != -1) {
102+
tmp.address = cur_node;
103+
tmp.data = node[cur_node].first;
104+
tmp.next = node[cur_node].second;
105+
cur_node = tmp.next;
106+
vList.push_back(tmp);
107+
}
108+
int num = vList.size() / k;
109+
for (i = 0; i < num; i++) {
110+
for (j = (i+1)*k-1; j >= i*k; j--) {
111+
printf("%05d %d ",vList[j].address, vList[j].data);
112+
113+
if (j == i*k) {
114+
if (i < num-1) {
115+
printf("%05d", vList[(i+2)*k-1].address);
116+
} else {
117+
if ((i+1)*k < vList.size()) {
118+
printf("%05d", vList[(i+1)*k].address);
119+
} else {
120+
printf("-1");
121+
}
122+
}
123+
} else {
124+
printf("%05d", vList[j-1].address);
125+
}
126+
printf("\n");
127+
128+
}
129+
130+
}
131+
if (vList.size() % k != 0) {
132+
num = vList.size()%k;
133+
for (i = vList.size()-num; i < vList.size(); i++) {
134+
printf("%05d %d ", vList[i].address, vList[i].data);
135+
if (i == vList.size()-1) printf("-1\n");
136+
else printf("%05d\n", vList[i+1].address);
137+
}
138+
139+
}
140+
141+
}
142+
143+
144+
int main() {
145+
#ifdef __DEBUG__
146+
freopen( "test.in", "r", stdin );
147+
freopen( "test.out", "w", stdout );
148+
time_t st = clock();
149+
#endif
150+
run();
151+
#ifdef __DEBUG__
152+
printf( "\n=============\n" );
153+
printf( "Time: %.2lf sec\n", ( clock() - st ) / double( CLOCKS_PER_SEC ) );
154+
#endif
155+
return 0;
156+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include <stack>
4+
using namespace std;
5+
6+
int main()
7+
{
8+
int m,n,k,cur,i,val;
9+
scanf("%d %d %d", &m, &n, &k);
10+
while(k--)
11+
{
12+
stack<int> s;
13+
while(!s.empty()) s.pop();
14+
s.push(1);
15+
cur = 2;
16+
int ok = 1;
17+
for(i = 1; i <= n; i++)
18+
{
19+
scanf("%d", &val);
20+
while( (s.empty() || val != s.top()) && (cur <= n) && ((int)s.size() < m) )
21+
{
22+
s.push(cur);
23+
cur++;
24+
}
25+
if(val == s.top())
26+
{
27+
s.pop();
28+
}
29+
else
30+
{
31+
ok = 0;
32+
}
33+
}
34+
if(!ok) puts("NO");
35+
else puts("YES");
36+
}
37+
return 0;
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
n = int(input().strip())
2+
nodes = [tuple(int(x) if x != "-" else -1 for x in input().split()) for _ in range(n)]
3+
is_root = [True] * n
4+
for i, (l, r) in enumerate(nodes):
5+
if l != -1:
6+
is_root[l] = False
7+
if r != -1:
8+
is_root[r] = False
9+
10+
root = is_root.index(True)
11+
leaves = []
12+
q = [root]
13+
while q:
14+
u = q.pop(0)
15+
l, r = nodes[u]
16+
if l == r == -1:
17+
leaves.append(u)
18+
if l != -1:
19+
q.append(l)
20+
if r != -1:
21+
q.append(r)
22+
23+
print(*leaves)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <queue>
4+
#include <cstdio>
5+
#include <cctype>
6+
#include <map>
7+
#include <string>
8+
#include <cstring>
9+
#include <stack>
10+
#include <set>
11+
using namespace std;
12+
13+
class Command {
14+
public:
15+
string op;
16+
int val;
17+
}cmd_ins;
18+
19+
class Node {
20+
public:
21+
Node *left;
22+
Node *right;
23+
int val;
24+
} *root, *nodelist[40];
25+
26+
vector<Command> cmdList;
27+
vector<int> postTravelArr;
28+
stack<int> st;
29+
30+
void postTravel(Node *root) {
31+
if (root->left != NULL)
32+
postTravel(root->left);
33+
if (root->right != NULL)
34+
postTravel(root->right);
35+
postTravelArr.push_back(root->val);
36+
}
37+
38+
39+
int main() {
40+
int n;
41+
cin >> n;
42+
for (int i = 0; i < 2*n; i++) {
43+
cin >> cmd_ins.op;
44+
if (cmd_ins.op == "Push") {
45+
cin >> cmd_ins.val;
46+
st.push(cmd_ins.val);
47+
} else {
48+
cmd_ins.val = st.top();
49+
st.pop();
50+
}
51+
cmdList.push_back(cmd_ins);
52+
nodelist[cmd_ins.val] = new Node;
53+
nodelist[cmd_ins.val]->left = NULL;
54+
nodelist[cmd_ins.val]->right = NULL;
55+
nodelist[cmd_ins.val]->val = cmd_ins.val;
56+
}
57+
58+
root = nodelist[cmdList[0].val];
59+
60+
for (int i = 1; i < cmdList.size(); i++) {
61+
// attach much attention to Push operation
62+
// when two successive Push operation i-1 and i means Node i-1's left child is Node i
63+
// when one operation is Pop, and another operation is Push, so operation i-1 Pop and i Push means, Node i-1's right child is Node i
64+
if (cmdList[i].op == "Push") {
65+
if (cmdList[i-1].op == "Push") nodelist[cmdList[i-1].val]->left = nodelist[cmdList[i].val];
66+
else nodelist[cmdList[i-1].val]->right = nodelist[cmdList[i].val];
67+
}
68+
}
69+
70+
postTravel(root);
71+
cout << postTravelArr[0];
72+
for (int i = 1; i< postTravelArr.size(); i++) {
73+
cout << " " << postTravelArr[i];
74+
}
75+
cout << endl;
76+
return 0;
77+
}

0 commit comments

Comments
 (0)