Skip to content

Commit 79ce82c

Browse files
authored
More DMOJ files
1 parent 4900099 commit 79ce82c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2117
-0
lines changed

DMOJ/apio10p1.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <cstdio>
2+
#include <algorithm>
3+
#include <vector>
4+
using namespace std;
5+
typedef long long ll;
6+
vector<ll> M,B;
7+
int pointer;
8+
bool bad(int l1,int l2,int l3)
9+
{
10+
/*
11+
intersection(l1,l2) has x-coordinate (b1-b2)/(m2-m1)
12+
intersection(l1,l3) has x-coordinate (b1-b3)/(m3-m1)
13+
set the former greater than the latter, and cross-multiply to
14+
eliminate division
15+
*/
16+
return (B[l3]-B[l1])*(M[l1]-M[l2])<(B[l2]-B[l1])*(M[l1]-M[l3]);
17+
}
18+
//Adds a new line (with lowest slope) to the structure
19+
void add(long long m,long long b)
20+
{
21+
//First, let's add it to the end
22+
m *= -1;
23+
b *= -1;
24+
M.push_back(m);
25+
B.push_back(b);
26+
//If the penultimate is now made irrelevant between the antepenultimate
27+
//and the ultimate, remove it. Repeat as many times as necessary
28+
while (M.size()>=3&&bad(M.size()-3,M.size()-2,M.size()-1))
29+
{
30+
M.erase(M.end()-2);
31+
B.erase(B.end()-2);
32+
}
33+
}
34+
//Returns the minimum y-coordinate of any intersection between a given vertical
35+
//line and the lower envelope
36+
long long query(long long x)
37+
{
38+
//If we removed what was the best line for the previous query, then the
39+
//newly inserted line is now the best for that query
40+
if (pointer>=M.size())
41+
pointer=M.size()-1;
42+
//Any better line must be to the right, since query values are
43+
//non-decreasing
44+
while (pointer<M.size()-1&&
45+
M[pointer+1]*x+B[pointer+1]<M[pointer]*x+B[pointer])
46+
pointer++;
47+
return -(M[pointer]*x+B[pointer]);
48+
}
49+
int main(){
50+
int TC = 1;
51+
while(TC--){
52+
M.clear();
53+
B.clear();
54+
pointer = 0;
55+
int n;
56+
scanf("%d",&n);
57+
ll a,b,c;
58+
scanf("%lld %lld %lld",&a,&b,&c);
59+
add(0LL,0LL);
60+
ll davez, soma = 0;
61+
for(int i=1;i<n;i++){
62+
scanf("%lld",&davez);
63+
soma += davez;
64+
ll best = query(soma) + a*soma*soma + b*soma + c;
65+
add(-2*a*soma,best + a*soma*soma - b*soma);
66+
}
67+
scanf("%lld",&davez);
68+
soma += davez;
69+
ll best = query(soma) + a*soma*soma + b*soma + c;
70+
printf("%lld\n",best);
71+
}
72+
return 0;
73+
}

DMOJ/apio12p1.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
const long long MAXN = 100010;
4+
typedef pair<long long,long long> ii;
5+
typedef struct node* pnode;
6+
struct node{
7+
pnode l,r;
8+
ii key;
9+
long long size,prior;
10+
long long soma;
11+
node(ii key) : l(NULL),r(NULL),key(key),size(1),prior(rand()),soma((long long)key.first){}
12+
};
13+
inline long long sz(pnode t){
14+
if(t == NULL) return 0;
15+
return t->size;
16+
}
17+
inline long long sm(pnode t){
18+
if(t == NULL) return 0LL;
19+
return t->soma;
20+
}
21+
void upd(pnode t){
22+
if(t == NULL) return;
23+
t->size = sz(t->l) + 1 + sz(t->r);
24+
t->soma = (long long)t->key.first + sm(t->l) + sm(t->r);
25+
}
26+
void split(pnode t,ii key,pnode &l,pnode &r){
27+
if(t == NULL){
28+
l = r = NULL;
29+
}
30+
else{
31+
if(key < t->key){
32+
split(t->l,key,l,t->l);
33+
r = t;
34+
}
35+
else{
36+
split(t->r,key,t->r,r);
37+
l = t;
38+
}
39+
}
40+
upd(t);
41+
}
42+
void merge(pnode &t,pnode l,pnode r){
43+
if(l == NULL){t = r;}
44+
else if(r == NULL){t = l;}
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+
upd(t);
54+
}
55+
void insert(pnode &t,ii key){
56+
pnode L,R;
57+
pnode aux = new node(key);
58+
split(t,ii(key.first - 1,MAXN),L,R);
59+
merge(t,L,aux);
60+
merge(t,t,R);
61+
}
62+
long long count(pnode t,long long budget){
63+
if(t == NULL) return 0;
64+
long long agora = (long long)t->key.first + sm(t->l);
65+
if(budget == agora) return sz(t->l) + 1;
66+
if(budget <= agora) return count(t->l,budget);
67+
return sz(t->l) + 1 + count(t->r,budget - agora);
68+
}
69+
vector<long long> grafo[MAXN];
70+
vector<long long> *conjunto[MAXN];
71+
long long peso[MAXN],lideranca[MAXN],n,m,tam[MAXN];
72+
pnode raiz[MAXN];
73+
long long resp;
74+
void dfs(long long x){
75+
tam[x] = 1;
76+
long long big = -1,mx = -1;
77+
for(long long v : grafo[x]){
78+
dfs(v);
79+
tam[x] += tam[v];
80+
if(tam[v] > mx){
81+
mx = tam[v];
82+
big = v;
83+
}
84+
}
85+
if(big == -1){
86+
conjunto[x] = new vector<long long> ();
87+
(*conjunto[x]).push_back(x);
88+
raiz[x] = new node(ii(peso[x],x));
89+
}
90+
else{
91+
raiz[x] = raiz[big];
92+
conjunto[x] = conjunto[big];
93+
(*conjunto[x]).push_back(x);
94+
insert(raiz[x],ii(peso[x],x));
95+
}
96+
for(long long v : grafo[x]){
97+
if(v == big) continue;
98+
for(long long u : (*conjunto[v])){
99+
(*conjunto[x]).push_back(u);
100+
insert(raiz[x],ii(peso[u],u));
101+
}
102+
}
103+
resp = max(resp, 1LL*lideranca[x]*count(raiz[x],(long long)m) );
104+
}
105+
int main(){
106+
cin.tie(0);ios_base::sync_with_stdio(0);
107+
cin >> n >> m;
108+
for(long long i=1;i<=n;i++){
109+
long long j;
110+
cin >> j >> peso[i] >> lideranca[i];
111+
grafo[j].push_back(i);
112+
}
113+
dfs(1);
114+
cout << resp << endl;
115+
return 0;
116+
}

DMOJ/bfs17p1.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
5+
multiset<string> resp;
6+
int n;
7+
cin >> n;
8+
string s;
9+
while(n--){
10+
cin >> s;
11+
if(s.size() <= 10) resp.insert(s);
12+
}
13+
cout << resp.size() << endl;
14+
return 0;
15+
}

DMOJ/bfs17p2.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int qtd[6],last,n;
4+
vector<int> perm;
5+
int check(){
6+
int total = 0;
7+
last = -1;
8+
while(true){
9+
int available = 0;
10+
//for(int i = 0;i<6;i++) cout << qtd[i] << endl;
11+
for(int i = 5;i>=0;i--){
12+
if(i != last) available += qtd[i];
13+
}
14+
if(available == 0) break;
15+
for(int i = 5;i>=0;i--){
16+
if(i == last) continue;
17+
if(qtd[i] == 0) continue;
18+
total++;
19+
last = i;
20+
qtd[i]--;
21+
break;
22+
}
23+
}
24+
return total;
25+
}
26+
int main(){
27+
cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
28+
cin >> n;
29+
for(int i = 1;i<=n;i++){
30+
string s;
31+
cin >> s;
32+
if(s == "red") qtd[0]++;
33+
if(s == "orange") qtd[1]++;
34+
if(s == "yellow") qtd[2]++;
35+
if(s == "green") qtd[3]++;
36+
if(s == "blue") qtd[4]++;
37+
if(s == "black") qtd[5]++;
38+
}
39+
sort(qtd,qtd+6);
40+
cout << check() << endl;
41+
return 0;
42+
}

DMOJ/bfs17p3.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef pair<int,int> ii;
4+
const int MAXN = 3010;
5+
int n,m,X1[MAXN],Y1[MAXN],R1[MAXN],X2[MAXN],Y2[MAXN],R2[MAXN],ini1,ini2,max1,max2,obj1,obj2,min1,min2;
6+
int processado1[MAXN],processado2[MAXN];
7+
int sq(int x){return x*x;}
8+
int euclid(int x,int y){return sq(x) + sq(y);}
9+
int main(){
10+
scanf("%d %d",&n,&m);
11+
min1 = 2*MAXN;
12+
min2 = 2*MAXN;
13+
for(int i = 1;i<=n;i++){
14+
scanf("%d %d %d",&X1[i],&Y1[i],&R1[i]);
15+
if(Y1[i] > max1){
16+
max1 = Y1[i];
17+
ini1= i;
18+
}
19+
if(R1[i] == 9001){
20+
obj1 = i;
21+
}
22+
}
23+
for(int i = 1;i<=m;i++){
24+
scanf("%d %d %d",&X2[i],&Y2[i],&R2[i]);
25+
if(Y2[i] > max2){
26+
max2 = Y2[i];
27+
ini2= i;
28+
}
29+
if(R2[i] == 9001){
30+
obj2 = i;
31+
}
32+
}
33+
queue<ii> bfs1;
34+
bfs1.push(ii(0,ini1));
35+
while(!bfs1.empty()){
36+
int v = bfs1.front().second;
37+
int perc = bfs1.front().first;
38+
bfs1.pop();
39+
if(processado1[v] == 1) continue;
40+
processado1[v] = 1;
41+
if(v == obj1){
42+
min1 = perc;
43+
break;
44+
}
45+
for(int u = 1;u<=n;u++){
46+
if(processado1[u] != 0) continue;
47+
int vaivai = euclid(X1[v] - X1[u],Y1[v] - Y1[u]);
48+
if(vaivai <= R1[v]*R1[v]){
49+
processado1[u] = 2;
50+
bfs1.push(ii(perc+1,u));
51+
}
52+
}
53+
}
54+
queue<ii> bfs2;
55+
bfs2.push(ii(0,ini2));
56+
while(!bfs2.empty()){
57+
int v = bfs2.front().second;
58+
int perc = bfs2.front().first;
59+
bfs2.pop();
60+
if(processado2[v] == 1) continue;
61+
processado2[v] = 1;
62+
if(v == obj2){
63+
min2 = perc;
64+
break;
65+
}
66+
for(int u = 1;u<=m;u++){
67+
if(processado2[u] != 0) continue;
68+
int vaivai = euclid(X2[v] - X2[u],Y2[v] - Y2[u]);
69+
if(vaivai <= R2[v]*R2[v]){
70+
processado2[u] = 2;
71+
bfs2.push(ii(perc+1,u));
72+
}
73+
}
74+
}
75+
if(min1 < min2){
76+
printf("We are the champions!\n");
77+
}
78+
else if(min1 == min2){
79+
printf("SUDDEN DEATH\n");
80+
}
81+
else{
82+
printf(":'(\n");
83+
}
84+
return 0;
85+
}

DMOJ/bts17p1.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
int flag = 0;
5+
string s;
6+
while(cin >> s){
7+
if('A' <= s[0] && s[0] <= 'Z' && flag) cout << ". ";
8+
else if(flag){
9+
cout << " ";
10+
}
11+
else{
12+
flag = 1;
13+
}
14+
cout << s;
15+
}
16+
cout << "." << endl;
17+
return 0;
18+
}

DMOJ/bts17p2.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
double resp = 1.0;
5+
int n;
6+
scanf("%d",&n);
7+
while(n--){
8+
int e,g;
9+
scanf("%d %d",&e,&g);
10+
double fator = (g - e)/double(g);
11+
resp *= fator;
12+
}
13+
printf("%.6lf\n",resp);
14+
return 0;
15+
}

0 commit comments

Comments
 (0)