Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d76f632

Browse files
IOI problems
1 parent 55d0e51 commit d76f632

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed

‎DMOJ/ioi13p1.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long ll;
5+
typedef pair<ll,ll> ii;
6+
typedef tuple<ll,ll,ll> trinca;
7+
8+
const int MAXN = 1e5 + 10;
9+
const ll INF = 1e18;
10+
const ll NEG = -INF;
11+
12+
vector<ii> grafo[MAXN];
13+
vector<ll> centers;
14+
int N,M,L,processado[MAXN];
15+
ll whole_diameter,furthest_distance[MAXN];
16+
17+
int bfs_diameter(int X){
18+
19+
queue<trinca> bfs;
20+
bfs.push(make_tuple(0,X,-1));
21+
22+
ii ans = ii(NEG,NEG);
23+
24+
while(!bfs.empty()){
25+
trinca davez = bfs.front();
26+
bfs.pop();
27+
ll dist = get<0>(davez);
28+
int v = get<1>(davez),p = get<2>(davez);
29+
processado[v]++;
30+
ans = max(ans, {dist,v} );
31+
furthest_distance[v] = max(furthest_distance[v],dist);
32+
33+
for(ii aresta : grafo[v]){
34+
int u = aresta.first, w = aresta.second;
35+
if(u == p) continue;
36+
bfs.push( make_tuple(dist + w,u,v) );
37+
}
38+
39+
}
40+
41+
//printf("X %d {%lld %lld}\n",X,ans.first,ans.second);
42+
whole_diameter = max(whole_diameter,ans.first);
43+
return ans.second;
44+
45+
}
46+
47+
ll bfs_radius(int X){
48+
49+
queue<trinca> bfs;
50+
bfs.push(make_tuple(0,X,-1));
51+
52+
ii ans = ii(INF,INF);
53+
54+
while(!bfs.empty()){
55+
trinca davez = bfs.front();
56+
bfs.pop();
57+
ll dist = get<0>(davez);
58+
int v = get<1>(davez),p = get<2>(davez);
59+
ans = min(ans, {furthest_distance[v],v} );
60+
61+
for(ii aresta : grafo[v]){
62+
int u = aresta.first, w = aresta.second;
63+
if(u == p) continue;
64+
bfs.push( make_tuple(dist +w,u,v) );
65+
}
66+
67+
}
68+
69+
//printf("Xr %d {%lld %lld}\n",X,ans.first,ans.second);
70+
71+
return ans.first;
72+
73+
}
74+
75+
ll process_tree(int v){
76+
77+
int x = bfs_diameter(v);
78+
int y = bfs_diameter(x);
79+
int z = bfs_diameter(y);
80+
81+
ll ret = bfs_radius(z);
82+
//printf("V %d X %d Y %d Z %d RET %lld\n",v,x,y,z,ret);
83+
return ret;
84+
85+
}
86+
87+
int travelTime(int n, int m, int l, int A[], int B[], int T[]){
88+
89+
N = n;
90+
M = m;
91+
L = l;
92+
93+
for(int i = 0;i<M;i++){
94+
int u,v,w;
95+
u = A[i];
96+
v = B[i];
97+
w = T[i];
98+
grafo[u].push_back({v,w});
99+
grafo[v].push_back({u,w});
100+
}
101+
102+
for(int i = 0;i<N;i++){
103+
if(processado[i]) continue;
104+
centers.push_back(process_tree(i));
105+
}
106+
107+
sort(centers.rbegin(),centers.rend());
108+
if(centers.size() >= 2) whole_diameter = max(whole_diameter,
109+
centers[0] + centers[1] + L );
110+
if(centers.size() >= 3) whole_diameter = max(whole_diameter,
111+
centers[1] + centers[2] + 2*L );
112+
113+
return (int)whole_diameter;
114+
115+
}
116+
117+
int main(){
118+
return 0;
119+
}

‎DMOJ/ioi13p4.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
const int MAXN = 5010;
5+
6+
int isDefined[MAXN],definedState[MAXN],whichNumber[MAXN],N;
7+
int queryArray[MAXN];
8+
9+
int opens_kth(int got_ans,int target){
10+
if(got_ans == -1) return 1;
11+
return got_ans > target;
12+
}
13+
14+
void divide_and_conquer(int target,int left,int right,int known_color){
15+
16+
if(left == right){
17+
isDefined[left] = 1;
18+
definedState[left] = known_color;
19+
whichNumber[left] = target;
20+
return;
21+
}
22+
23+
memset(queryArray,0,sizeof(queryArray));
24+
int mid = (left + right)/2;
25+
for(int i = 0;i<N;i++){
26+
if(left <= i && i <= mid){
27+
queryArray[i] = known_color;
28+
}
29+
else{
30+
queryArray[i] = 1 ^ known_color;
31+
}
32+
}
33+
for(int i = 0;i<N;i++){
34+
if(isDefined[i]) queryArray[i] = definedState[i];
35+
}
36+
37+
int ans = tryCombination(queryArray);
38+
if(opens_kth(ans,target)){
39+
divide_and_conquer(target,left,mid,known_color);
40+
}
41+
else{
42+
divide_and_conquer(target,mid+1,right,known_color);
43+
}
44+
45+
}
46+
47+
int which_color(int target){
48+
49+
memset(queryArray,0,sizeof(queryArray));
50+
for(int i = 0;i<N;i++){
51+
queryArray[i] = 0;
52+
}
53+
for(int i = 0;i<N;i++){
54+
if(isDefined[i]) queryArray[i] = definedState[i];
55+
}
56+
57+
int ans = tryCombination(queryArray);
58+
if(opens_kth(ans,target)) return 0;
59+
else return 1;
60+
61+
}
62+
63+
void exploreCave(int n) {
64+
65+
N = n;
66+
67+
for(int i = 0;i<N;i++){
68+
int known_color = which_color(i);
69+
divide_and_conquer(i,0,N-1,known_color);
70+
}
71+
72+
answer(definedState,whichNumber);
73+
74+
}

‎DMOJ/ioi16p5.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int whichNumber[129];
5+
int N;
6+
7+
void solve_add(int left,int right){
8+
9+
if(left == right) return;
10+
11+
int mid = left + (right - left)/2;
12+
string s;
13+
14+
for(int i = 0;i<N;i++){
15+
if(left <= i && i <= right){
16+
s.push_back('0');
17+
}
18+
else{
19+
s.push_back('1');
20+
}
21+
}
22+
23+
for(int i = left;i<=mid;i++){
24+
s[i] = '1';
25+
add_element(s);
26+
s[i] = '0';
27+
}
28+
29+
solve_add(left,mid);
30+
solve_add(mid+1,right);
31+
32+
}
33+
34+
void solve_check(int left,int right,vector<int> possible){
35+
36+
if(left == right){
37+
whichNumber[possible[0]] = left;
38+
return;
39+
}
40+
41+
int mid = left + (right - left)/2;
42+
43+
vector<int> firstHalf,lastHalf;
44+
45+
string s;
46+
for(int i = 0;i<N;i++) s.push_back('1');
47+
for(int i : possible) s[i] = '0';
48+
49+
for(int i : possible){
50+
s[i] = '1';
51+
if(check_element(s)){
52+
firstHalf.push_back(i);
53+
}
54+
else{
55+
lastHalf.push_back(i);
56+
}
57+
s[i] = '0';
58+
}
59+
60+
solve_check(left,mid,firstHalf);
61+
solve_check(mid+1,right,lastHalf);
62+
63+
}
64+
65+
int* restore_permutation(int n, int w, int r){
66+
67+
N = n;
68+
vector<int> allElements;
69+
for(int i = 0;i<N;i++) allElements.push_back(i);
70+
71+
solve_add(0,N-1);
72+
compile_set();
73+
solve_check(0,N-1,allElements);
74+
75+
return whichNumber;
76+
77+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /