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 aeb6775

Browse files
COCI 17 problems
1 parent 0d2a9f0 commit aeb6775

File tree

3 files changed

+203
-3
lines changed

3 files changed

+203
-3
lines changed

‎DMOJ/coci17c1p5.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Ivan Carvalho
2+
// Solution to https://dmoj.ca/problem/coci17c1p5
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
const int INF = 1e9 + 1;
7+
typedef struct node* pnode;
8+
9+
struct node{
10+
pnode l,r;
11+
int key,puro,junta,prior;
12+
node(int _key,int _puro) : l(NULL),r(NULL),key(_key),puro(_puro),junta(_puro),prior(rand() ^ (rand() << 16)){}
13+
};
14+
15+
inline int jnt(pnode t){return t ? t->junta : INF;}
16+
17+
inline void update(pnode t){if(t) t->junta = min(min(jnt(t->l),t->puro),jnt(t->r));}
18+
19+
void split(pnode t,int key,pnode &l,pnode &r){
20+
21+
if(t == NULL){
22+
l = r = NULL;
23+
}
24+
else if(key < t->key){
25+
split(t->l,key,l,t->l);
26+
r = t;
27+
}
28+
else{
29+
split(t->r,key,t->r,r);
30+
l = t;
31+
}
32+
33+
update(t);
34+
35+
}
36+
37+
void merge(pnode &t,pnode l,pnode r){
38+
39+
if(l == NULL){
40+
t = r;
41+
}
42+
else if(r == NULL){
43+
t = l;
44+
}
45+
else if(l->prior > r->prior){
46+
merge(l->r,l->r,r);
47+
t = l;
48+
}
49+
else{
50+
merge(r->l,l,r->l);
51+
t = r;
52+
}
53+
54+
update(t);
55+
56+
}
57+
58+
void insert(pnode &t,int key,int puro){
59+
pnode L,R;
60+
pnode aux = new node(key,puro);
61+
split(t,key,L,R);
62+
merge(t,L,aux);
63+
merge(t,t,R);
64+
}
65+
66+
int find(pnode t,int y){
67+
if(jnt(t->l) <= y) return find(t->l,y);
68+
if(t->puro <= y) return t->key;
69+
return find(t->r,y);
70+
}
71+
72+
int query(pnode &t,int y,int b){
73+
pnode L,R;
74+
split(t,b-1,L,R);
75+
int ret = -1;
76+
if(jnt(R) <= y) ret = find(R,y);
77+
merge(t,L,R);
78+
return ret;
79+
}
80+
81+
int main(){
82+
83+
int N,M;
84+
pnode raiz = NULL;
85+
scanf("%d %d",&N,&M);
86+
87+
while(M--){
88+
89+
char op;
90+
scanf(" %c",&op);
91+
if(op == 'M'){
92+
int a,b;
93+
scanf("%d %d",&a,&b);
94+
insert(raiz,b,a);
95+
}
96+
else{
97+
int y,b;
98+
scanf("%d %d",&y,&b);
99+
printf("%d\n",query(raiz,y,b));
100+
}
101+
102+
}
103+
104+
return 0;
105+
106+
}

‎DMOJ/coci17c2p4.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Ivan Carvalho
2+
// Solution to https://dmoj.ca/problem/coci17c2p4
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
const int MAXN = 50;
8+
9+
vector<int> compressao;
10+
int H1[MAXN],H2[MAXN],NT,N,M;
11+
vector<ll> possiveis[MAXN];
12+
ll G1[MAXN],G2[MAXN],K,resposta;
13+
14+
int main(){
15+
16+
cin >> NT >> K;
17+
N = NT/2;
18+
M = NT - N;
19+
20+
compressao.push_back(0);
21+
22+
for(int i = 1;i<=N;i++){
23+
cin >> H1[i] >> G1[i];
24+
compressao.push_back(H1[i]);
25+
}
26+
27+
for(int i = 1;i<=M;i++){
28+
cin >> H2[i] >> G2[i];
29+
compressao.push_back(H2[i]);
30+
}
31+
32+
sort(compressao.begin(),compressao.end());
33+
compressao.erase(unique(compressao.begin(),compressao.end()),compressao.end());
34+
35+
for(int i = 1;i<=N;i++){
36+
H1[i] = lower_bound(compressao.begin(),compressao.end(),H1[i]) - compressao.begin();
37+
}
38+
39+
for(int i = 1;i<=M;i++){
40+
H2[i] = lower_bound(compressao.begin(),compressao.end(),H2[i]) - compressao.begin();
41+
}
42+
43+
for(int bitmask = 1;bitmask < (1 << N);bitmask++){
44+
45+
int last = 0;
46+
ll total = 0;
47+
bool flag = false;
48+
49+
for(int i = 0;i<N;i++){
50+
if(bitmask & (1 << i)){
51+
int j = i + 1;
52+
if(H1[last] > H1[j]) flag = true;
53+
last = j;
54+
total += G1[j];
55+
}
56+
}
57+
58+
if(flag) continue;
59+
possiveis[H1[last]].push_back(total);
60+
if(total >= K) resposta++;
61+
62+
}
63+
64+
for(int i = 0;i<=compressao.size();i++) sort(possiveis[i].begin(),possiveis[i].end());
65+
66+
for(int bitmask = 1;bitmask < (1 << M);bitmask++){
67+
68+
int last = 0;
69+
int first = 0;
70+
ll total = 0;
71+
bool flag = false;
72+
73+
for(int i = 0;i<M;i++){
74+
if(bitmask & (1 << i)){
75+
int j = i + 1;
76+
if(H2[last] > H2[j]) flag = true;
77+
if(first == 0) first = j;
78+
last = j;
79+
total += G2[j];
80+
}
81+
}
82+
83+
if(flag) continue;
84+
if(total >= K) resposta++;
85+
86+
for(int i = 0;i<=H2[first];i++){
87+
resposta += possiveis[i].end() - lower_bound(possiveis[i].begin(),possiveis[i].end(),K - total);
88+
}
89+
90+
}
91+
92+
cout << resposta << endl;
93+
94+
return 0;
95+
}

‎README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# Olympiad Solutions
2-
1+
# Olympiad Solutions [![Algorithms](https://img.shields.io/badge/-algorithms-green.svg)](https://github.com/topics/algorithms) [![Data Structures](https://img.shields.io/badge/-data%20structures-green.svg)](https://github.com/topics/data-structures)
32
This repository contains solutions to a plethora of algorithmic problems that come mainly from 3 online judges:
43

54
* [DMOJ](https://dmoj.ca/) - 400+ problems
@@ -17,4 +16,4 @@ Because the solutions were meant for competitions, they **do** contain software
1716

1817
## About the author
1918

20-
I have participated in many programming competitions, such as the International Olympiad in Informatics, the Brazilian Olympiad in Informatics, the Ibero-American Contest in Informatics and ICPC PacNW. To read more about my experience in competitive programming, see my [blog](https://ivaniscoding.github.io/about/index.html).
19+
I have participated in many programming competitions, such as the International Olympiad in Informatics, the Brazilian Olympiad in Informatics, the Ibero-American Contest in Informatics and ICPC PacNW. To read more about my experience in competitive programming, see my [blog](https://ivaniscoding.github.io/about/index.html).

0 commit comments

Comments
(0)

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