-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,35 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <vector> | ||
using namespace std; | ||
vector<int> post, in, level(100000, -1); | ||
struct node { | ||
int index, value; | ||
}; | ||
bool cmp(node a, node b) { | ||
return a.index < b.index; | ||
} | ||
vector<int> post, in; | ||
vector<node> ans; | ||
void pre(int root, int start, int end, int index) { | ||
if(start > end) return ; | ||
if (start > end) return; | ||
int i = start; | ||
while(i < end && in[i] != post[root]) i++; | ||
level[index] = post[root]; | ||
while (i < end && in[i] != post[root]) i++; | ||
ans.push_back({index, post[root]}); | ||
pre(root - 1 - end + i, start, i - 1, 2 * index + 1); | ||
pre(root - 1, i + 1, end, 2 * index + 2); | ||
} | ||
int main() { | ||
int n, cnt = 0; | ||
int n; | ||
scanf("%d", &n); | ||
post.resize(n); | ||
in.resize(n); | ||
for(int i = 0; i < n; i++) scanf("%d", &post[i]); | ||
for(int i = 0; i < n; i++) scanf("%d", &in[i]); | ||
pre(n-1, 0, n-1, 0); | ||
for(int i = 0; i < level.size(); i++) { | ||
if (level[i] != -1) { | ||
if (cnt != 0) printf(" "); | ||
printf("%d", level[i]); | ||
cnt++; | ||
} | ||
if (cnt == n) break; | ||
for (int i = 0; i < n; i++) scanf("%d", &post[i]); | ||
for (int i = 0; i < n; i++) scanf("%d", &in[i]); | ||
pre(n - 1, 0, n - 1, 0); | ||
sort(ans.begin(), ans.end(), cmp); | ||
for (int i = 0; i < ans.size(); i++) { | ||
if (i != 0) cout << " "; | ||
cout << ans[i].value; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,62 @@ | ||
#include <iostream> | ||
#include <stack> | ||
#define lowbit(i) ((i) & (-i)) | ||
const int maxn = 100010; | ||
#include <queue> | ||
#include <vector> | ||
#include <algorithm> | ||
using namespace std; | ||
int c[maxn]; | ||
stack<int> s; | ||
void update(int x, int v) { | ||
for(int i = x; i < maxn; i += lowbit(i)) | ||
c[i] += v; | ||
} | ||
int getsum(int x) { | ||
int sum = 0; | ||
for(int i = x; i >= 1; i -= lowbit(i)) | ||
sum += c[i]; | ||
return sum; | ||
} | ||
void PeekMedian() { | ||
int left = 1, right = maxn, mid, k = (s.size() + 1) / 2; | ||
while(left < right) { | ||
mid = (left + right) / 2; | ||
if(getsum(mid) >= k) | ||
right = mid; | ||
else | ||
left = mid + 1; | ||
} | ||
printf("%d\n", left); | ||
struct node { | ||
int weight, index, rank, index0; | ||
}; | ||
bool cmp1(node a, node b) { | ||
return a.index0 < b.index0; | ||
} | ||
int main() { | ||
int n, temp; | ||
scanf("%d", &n); | ||
char str[15]; | ||
int n, g, num; | ||
scanf("%d%d", &n, &g); | ||
vector<int> v(n); | ||
vector<node> w(n); | ||
for(int i = 0; i < n; i++) | ||
scanf("%d", &v[i]); | ||
for(int i = 0; i < n; i++) { | ||
scanf("%s", str); | ||
if(str[1] == 'u') { | ||
scanf("%d", &temp); | ||
s.push(temp); | ||
update(temp, 1); | ||
} else if(str[1] == 'o') { | ||
if(!s.empty()) { | ||
update(s.top(), -1); | ||
printf("%d\n", s.top()); | ||
s.pop(); | ||
} else { | ||
printf("Invalid\n"); | ||
scanf("%d", &num); | ||
w[i].weight = v[num]; | ||
w[i].index = i; | ||
w[i].index0 = num; | ||
} | ||
queue<node> q; | ||
for(int i = 0; i < n; i++) | ||
q.push(w[i]); | ||
while(!q.empty()) { | ||
int size = q.size(); | ||
if(size == 1) { | ||
node temp = q.front(); | ||
w[temp.index].rank = 1; | ||
break; | ||
} | ||
int group = size / g; | ||
if(size % g != 0) | ||
group += 1; | ||
node maxnode; | ||
int maxn = -1, cnt = 0; | ||
for(int i = 0; i < size; i++) { | ||
node temp = q.front(); | ||
w[temp.index].rank = group + 1; | ||
q.pop(); | ||
cnt++; | ||
if(temp.weight > maxn) { | ||
maxn = temp.weight; | ||
maxnode = temp; | ||
} | ||
if(cnt == g || i == size - 1) { | ||
cnt = 0; | ||
maxn = -1; | ||
q.push(maxnode); | ||
} | ||
} else { | ||
if(!s.empty()) | ||
PeekMedian(); | ||
else | ||
printf("Invalid\n"); | ||
} | ||
} | ||
sort(w.begin(), w.end(), cmp1); | ||
for(int i = 0; i < n; i++) { | ||
if(i != 0) printf(" "); | ||
printf("%d", w[i].rank); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters