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 f254151

Browse files
IvanIvan
Ivan
authored and
Ivan
committed
BOI,IOI, Dynamic Connectivity
1 parent 400e338 commit f254151

File tree

6 files changed

+554
-0
lines changed

6 files changed

+554
-0
lines changed

‎DMOJ/banknotes.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef pair<int,int> ii;
4+
const int MAXN = 201;
5+
const int MAXK = 20001;
6+
int N,K,b[MAXN],c[MAXN],resposta[MAXN],dp[MAXN][MAXK];
7+
int main(){
8+
cin >> N;
9+
for(int i = 1;i<=N;i++) cin >> b[i];
10+
for(int i = 1;i<=N;i++) cin >> c[i];
11+
cin >> K;
12+
for(int i = 0;i<=K;i++) dp[0][i] = MAXK;
13+
dp[0][0] = 0;
14+
for(int i = 1;i<=N;i++){
15+
for(int j = 0;j<=K;j++){
16+
dp[i][j] = dp[i-1][j];
17+
}
18+
for(int resto = 0;resto < b[i];resto++){
19+
int ultimo = (K/b[i])*b[i];
20+
int sobra = K % b[i];
21+
if(resto <= sobra){
22+
ultimo += resto;
23+
}
24+
else{
25+
ultimo += (sobra - resto);
26+
}
27+
deque<ii> janela;
28+
for(int j = 1;j<c[i] && ultimo - j*b[i] >= 0;j++){
29+
ii novo = ii(dp[i-1][ultimo - j*b[i]] + j,ultimo - j*b[i]);
30+
while(!janela.empty() && janela.back().first >= novo.first) janela.pop_back();
31+
janela.push_back(novo);
32+
}
33+
for(int j = 0,val = ultimo;val > 0;j++,val -= b[i]){
34+
while(!janela.empty() && janela.front().second >= val) janela.pop_front();
35+
if(val - c[i]*b[i] >= 0){
36+
ii novo = ii(dp[i-1][val - c[i]*b[i]] + j + c[i],val - c[i]*b[i]);
37+
while(!janela.empty() && janela.back().first >= novo.first) janela.pop_back();
38+
janela.push_back(novo);
39+
}
40+
if(!janela.empty()) dp[i][val] = min(dp[i][val],janela.front().first - j);
41+
//printf("I %d Resto %d Val %d Dp %d %d\n",i,resto,val,dp[i][val],dp[i-1][val]);
42+
}
43+
}
44+
}
45+
cout << dp[N][K] << endl;
46+
return 0;
47+
}

‎DMOJ/boi2011p5.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef tuple<int,int,int> trinca;
5+
typedef tuple<double,int,int,int,int> aresta;
6+
typedef tuple<int,int,int,int> quadra;
7+
typedef pair<int,int> ii;
8+
9+
const int MAXN = 210;
10+
const double INF = 1e16;
11+
const double EPS = 1e-6;
12+
13+
map<quadra,ii> valido;
14+
vector<trinca> grafo[MAXN];
15+
int processado[MAXN],N,M,tem_que_exbir;
16+
int matA[MAXN][MAXN],matB[MAXN][MAXN];
17+
vector<quadra> pares;
18+
19+
int Prim(double alpha,int minimiza_b){
20+
21+
pares.clear();
22+
priority_queue<aresta, vector<aresta>, greater<aresta> > pq;
23+
24+
int s1 = 0,s2 = 0;
25+
26+
for(int i = 0;i<N;i++){
27+
processado[i] = 0;
28+
}
29+
30+
pq.push({0.0,0,0,0,-1});
31+
while(!pq.empty()){
32+
aresta davez = pq.top();
33+
pq.pop();
34+
int c1 = get<1>(davez),c2 = get<2>(davez);
35+
int v = get<3>(davez),p = get<4>(davez);
36+
if(processado[v]) continue;
37+
processado[v] = 1;
38+
s1 += c1;
39+
s2 += c2;
40+
if(minimiza_b) swap(c1,c2);
41+
if(p != -1) pares.push_back({min(p,v),max(p,v),c1,c2});
42+
for(trinca nxt : grafo[v]){
43+
int u = get<0>(nxt),l1 = get<1>(nxt),l2 = get<2>(nxt);
44+
if(minimiza_b) swap(l1,l2);
45+
if(processado[u]) continue;
46+
pq.push({alpha*l1 + (1.0 - alpha)*l2,l1,l2,u,v});
47+
}
48+
}
49+
50+
return s1*s2;
51+
52+
}
53+
54+
int checa(double alpha){
55+
return min(Prim(alpha,0),Prim(1.0 - alpha,1));
56+
}
57+
58+
int main(){
59+
60+
cin >> N >> M;
61+
for(int i = 1;i<=M;i++){
62+
int u,v,w1,w2;
63+
cin >> u >> v >> w1 >> w2;
64+
grafo[u].push_back({v,w1,w2});
65+
grafo[v].push_back({u,w1,w2});
66+
valido[{min(u,v),max(u,v),w1,w2}] = {u,v};
67+
}
68+
69+
double ini = 0.0,fim = 1.0;
70+
while(fim - ini > EPS){
71+
double m1 = ini + (fim - ini)/3.0;
72+
double m2 = fim - (fim - ini)/3.0;
73+
if(checa(m1) < checa(m2)){
74+
fim = m2 - EPS;
75+
}
76+
else{
77+
ini = m1 + EPS;
78+
}
79+
}
80+
81+
if(Prim(ini,0) < Prim(1.0 - ini,1)){
82+
Prim(ini,0);
83+
int s1 = 0,s2 = 0;
84+
sort(pares.begin(),pares.end());
85+
for(quadra davez : pares){
86+
s1 += get<2>(davez);
87+
s2 += get<3>(davez);
88+
}
89+
printf("%d %d\n",s1,s2);
90+
for(quadra davez : pares){
91+
printf("%d %d\n",valido[davez].first,valido[davez].second);
92+
}
93+
}
94+
else{
95+
Prim(1.0 - ini,1);
96+
int s1 = 0,s2 = 0;
97+
sort(pares.begin(),pares.end());
98+
for(quadra davez : pares){
99+
s1 += get<2>(davez);
100+
s2 += get<3>(davez);
101+
}
102+
printf("%d %d\n",s1,s2);
103+
for(quadra davez : pares){
104+
printf("%d %d\n",valido[davez].first,valido[davez].second);
105+
}
106+
}
107+
108+
return 0;
109+
}

‎DMOJ/cco07p6.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
const int MAXN = 1010;
5+
6+
typedef pair<int,int> ii;
7+
8+
vector<ii> arestas;
9+
vector<int> tipo;
10+
int pai[MAXN],grau[MAXN],n,r,pontes;
11+
12+
void reseta(){
13+
for(int i = 1;i<=n;i++) pai[i] = i;
14+
}
15+
16+
int find(int x){
17+
if(x == pai[x]) return x;
18+
return pai[x] = find(pai[x]);
19+
}
20+
21+
void join(int x,int y){
22+
x = find(x);y = find(y);
23+
if(x == y) return;
24+
if(x > y) swap(x,y);
25+
pai[y] = x;
26+
}
27+
28+
int main(){
29+
scanf("%d %d",&n,&r);
30+
reseta();
31+
32+
for(int i = 1;i<=r;i++){
33+
int u,v;
34+
scanf("%d %d",&u,&v);
35+
arestas.push_back(ii(u,v));
36+
}
37+
38+
for(int i = 0;i < r;i++){
39+
reseta();
40+
for(int j = 0;j < i;j++) join(arestas[j].first,arestas[j].second);
41+
for(int j = i+1;j < r;j++) join(arestas[j].first,arestas[j].second);
42+
if(find(arestas[i].first) != find(arestas[i].second)){
43+
tipo.push_back(1);
44+
}
45+
else{
46+
tipo.push_back(0);
47+
}
48+
}
49+
50+
reseta();
51+
for(int i = 0;i<r;i++){
52+
if(tipo[i] == 0) join(arestas[i].first,arestas[i].second);
53+
}
54+
55+
for(int i = 0;i<r;i++){
56+
if(tipo[i] == 1){
57+
grau[find(arestas[i].first)]++;
58+
grau[find(arestas[i].second)]++;
59+
}
60+
}
61+
62+
int folhas = 0;
63+
64+
for(int i = 1;i<=n;i++) if(find(i) == i && grau[i] == 1) folhas++;
65+
66+
printf("%d\n",(folhas+1)/2);
67+
return 0;
68+
}

‎DMOJ/ioi13p5.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+
typedef pair<int,int> ii;
5+
6+
const int MAXN = 1e5 + 10;
7+
8+
vector<int> weak,small;
9+
vector<ii> robots;
10+
int A,B,N;
11+
12+
int checa(int X){
13+
priority_queue<int> pq;
14+
int ptr = 0;
15+
for(int w : weak){
16+
while(ptr < N && robots[ptr].first < w){
17+
pq.push(robots[ptr].second);
18+
ptr++;
19+
}
20+
for(int i = 1;i<=X && !pq.empty();i++){
21+
pq.pop();
22+
}
23+
}
24+
while(ptr < N){
25+
pq.push(robots[ptr].second);
26+
ptr++;
27+
}
28+
for(int s : small){
29+
for(int i = 1;i <= X && !pq.empty();i++){
30+
if(!pq.empty() && pq.top() >= s) return 0;
31+
pq.pop();
32+
}
33+
}
34+
return pq.empty();
35+
}
36+
37+
int putaway(int a, int b, int n, int X[], int Y[], int W[], int S[]){
38+
39+
A = a;B = b;N = n;
40+
for(int i = 0;i<A;i++){
41+
int x = X[i];
42+
weak.push_back(x);
43+
}
44+
for(int i = 0;i<B;i++){
45+
int x = Y[i];
46+
small.push_back(x);
47+
}
48+
for(int i = 0;i<N;i++){
49+
int x = W[i],y = S[i];
50+
robots.push_back({x,y});
51+
}
52+
53+
sort(robots.begin(),robots.end());
54+
sort(weak.begin(),weak.end());
55+
sort(small.rbegin(),small.rend());
56+
57+
int ini = 1,fim = N,meio,resposta = -1;
58+
while(ini <= fim){
59+
meio = (ini+fim)/2;
60+
if(checa(meio)){
61+
resposta = meio;
62+
fim = meio - 1;
63+
}
64+
else{
65+
ini = meio + 1;
66+
}
67+
}
68+
69+
return resposta;
70+
}
71+
72+
int main(){
73+
return 0;
74+
}

‎DMOJ/ncco2d1p1.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long ll;
5+
6+
const int MAXN = 310;
7+
const ll INF = 1e16;
8+
9+
ll dp[MAXN][MAXN][2],A[MAXN],B[MAXN],T,memo[MAXN][MAXN];
10+
int n,m,K,N,M,vis[MAXN][MAXN][2],iteracao;
11+
12+
ll solve(int p1,int p2,int lado){
13+
if(p1 > n || p2 > m) return INF;
14+
if(vis[p1][p2][lado] == iteracao) return dp[p1][p2][lado];
15+
vis[p1][p2][lado] = iteracao;
16+
if(p1 == n && p2 == m) return dp[p1][p2][lado] = 0;
17+
if(lado == 0){
18+
ll opt1 = solve(p1+1,p2,0) + abs(A[p1 +1] - A[p1])*((n - p1) + (m - p2));
19+
ll opt2 = solve(p1,p2+1,1) + abs(B[p2+1] - A[p1])*((n - p1) + (m - p2));
20+
return dp[p1][p2][lado] = min(opt1,opt2);
21+
}
22+
else{
23+
ll opt1 = solve(p1+1,p2,0) + abs(A[p1 + 1] - B[p2])*((n - p1) + (m - p2));
24+
ll opt2 = solve(p1,p2+1,1) + abs(B[p2+1] - B[p2])*((n - p1) + (m - p2));
25+
return dp[p1][p2][lado] = min(opt1,opt2);
26+
}
27+
}
28+
29+
ll testa(int qtd1,int qtd2){
30+
if(memo[qtd1][qtd2] != 0) return memo[qtd1][qtd2];
31+
n = qtd1;m = qtd2;
32+
iteracao++;
33+
return memo[qtd1][qtd2] = (qtd1+qtd2)*T - min(solve(0,0,0),solve(0,0,1));
34+
}
35+
36+
bool compara(ll a,ll b){
37+
return a > b;
38+
}
39+
40+
int main(){
41+
cin >> K >> T;
42+
for(int i = 1;i<=K;i++){
43+
ll x;
44+
cin >> x;
45+
if(x >= 0){
46+
N++;
47+
A[N] = x;
48+
}
49+
else{
50+
M++;
51+
B[M] = x;
52+
}
53+
}
54+
55+
sort(A,A+N+1);
56+
sort(B,B+M+1,compara);
57+
58+
ll ans = 0,opt = M;
59+
for(int a = 0;a<=N;a++){
60+
while(opt - 1 >= 0 && testa(a,opt) < testa(a,opt - 1) ){
61+
opt--;
62+
}
63+
ans = max(ans,testa(a,opt));
64+
}
65+
opt = N;
66+
for(int b = 0;b<=M;b++){
67+
while(opt - 1 >= 0 && testa(opt,b) < testa(opt-1,b)){
68+
opt--;
69+
}
70+
ans = max(ans, testa(opt,b) );
71+
}
72+
73+
cout << ans << endl;
74+
75+
return 0;
76+
}

0 commit comments

Comments
(0)

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