Skip to content

Commit 05aa990

Browse files
authored
Add files via upload
1 parent 2033062 commit 05aa990

File tree

4 files changed

+296
-0
lines changed

4 files changed

+296
-0
lines changed

DMOJ/apio15p2.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <bits/stdc++.h>
2+
#define MT make_tuple
3+
using namespace std;
4+
typedef tuple<int,int,int> trinca;
5+
typedef pair<trinca,int> Ttrinca;
6+
const int MAXN = 30010;
7+
const int BUCKET = 200;
8+
vector<trinca> grafo[MAXN];
9+
queue<trinca> filas[BUCKET];
10+
priority_queue<Ttrinca, vector<Ttrinca> , greater<Ttrinca> > Dijkstra;
11+
int processado[MAXN][BUCKET],N,M,origem,destino;
12+
trinca top(){
13+
return Dijkstra.top().first;
14+
}
15+
void pop(){
16+
int peso = Dijkstra.top().second;
17+
Dijkstra.pop();
18+
filas[peso].pop();
19+
if(!filas[peso].empty()) Dijkstra.push(make_pair(filas[peso].front(),peso));
20+
}
21+
void push(int peso,trinca davez){
22+
if(filas[peso].empty()) Dijkstra.push(make_pair(davez,peso));
23+
filas[peso].push(davez);
24+
}
25+
bool empty(){
26+
return Dijkstra.empty();
27+
}
28+
int main(){
29+
scanf("%d %d",&N,&M);
30+
for(int i = 0;i<M;i++){
31+
int B,P;
32+
scanf("%d %d",&B,&P);
33+
if(i == 0) origem = B;
34+
else if(i == 1) destino = B;
35+
if(P < BUCKET){
36+
grafo[B].push_back(MT(B,P,0));
37+
}
38+
else{
39+
for(int j = 1;B - j*P >= 0;j++){
40+
grafo[B].push_back(MT(B - j*P,0,j));
41+
}
42+
for(int j = 1;B + j*P < N;j++){
43+
grafo[B].push_back(MT(B + j*P,0,j));
44+
}
45+
}
46+
}
47+
priority_queue<trinca, vector<trinca>, greater<trinca> > Dijkstra;
48+
push(0,MT(0,origem,0));
49+
while(!empty()){
50+
trinca davez = top();
51+
pop();
52+
int dist = get<0>(davez), v = get<1>(davez), power = get<2>(davez);
53+
if(processado[v][power] == 1) continue;
54+
processado[v][power] = 1;
55+
if(!processado[v][0]){
56+
push(0,MT(dist,v,0));
57+
}
58+
if(power < BUCKET && v - power >= 0){
59+
if(!processado[v - power][power]) push(1,MT(dist+1,v - power,power));
60+
}
61+
if(power < BUCKET && v + power < N){
62+
if(!processado[v + power][power]) push(1,MT(dist+1,v + power,power));
63+
}
64+
if(v == destino){
65+
printf("%d\n",dist);
66+
return 0;
67+
}
68+
if(power != 0) continue;
69+
for(trinca nova : grafo[v]){
70+
int nv = get<0>(nova), npower = get<1>(nova), nadd = get<2>(nova);
71+
if(v == nv){
72+
if(processado[v][npower]) continue;
73+
processado[v][npower] = 1;
74+
if(npower < BUCKET && v - npower >= 0){
75+
if(!processado[v - npower][npower]) push(1,MT(dist+1,v - npower,npower));
76+
}
77+
if(npower < BUCKET && v + npower < N){
78+
if(!processado[v + npower][npower]) push(1,MT(dist+1,v + npower,npower));
79+
}
80+
continue;
81+
}
82+
if(!processado[nv][npower]) push(nadd,MT(dist+nadd,nv,npower));
83+
}
84+
}
85+
printf("-1\n");
86+
return 0;
87+
}

DMOJ/ioi11p2.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <bits/stdc++.h>
2+
#define MP make_pair
3+
#define PB push_back
4+
using namespace std;
5+
typedef long long ll;
6+
typedef pair<ll,ll> ii;
7+
typedef map<ll,ll>::iterator ite;
8+
ll N,K,resp;
9+
const ll MAXN = 200010;
10+
vector<ii> grafo[MAXN];
11+
map<ll,ll> *conjunto[MAXN];
12+
ll tam[MAXN],percorrido[MAXN],nivel[MAXN],ac1[MAXN],ac2[MAXN];
13+
void dfs_sack(ll v,ll p){
14+
tam[v] = 1;
15+
ll big = -1,ligabig = -1,mx = -1;
16+
for(ll i=0;i<grafo[v].size();i++){
17+
ll u = grafo[v][i].first, ligau = grafo[v][i].second;
18+
if(u != p){
19+
nivel[u] = nivel[v] + 1;
20+
percorrido[u] = percorrido[v] + ligau;
21+
dfs_sack(u,v);
22+
tam[v] += tam[u];
23+
if(tam[u] > mx){
24+
mx = tam[u];
25+
ligabig = ligau;
26+
big = u;
27+
}
28+
}
29+
}
30+
if(big == -1){
31+
conjunto[v] = new map<ll,ll> ();
32+
(*conjunto[v])[0] = 0;
33+
}
34+
else{
35+
conjunto[v] = conjunto[big];
36+
ac1[v] = ac1[big] + ligabig;
37+
ac2[v] = ac2[big] + 1;
38+
if((*conjunto[v]).count(K - ac1[v])) resp = min(resp,(*conjunto[v])[K - ac1[v]] + ac2[v]);
39+
(*conjunto[v])[-ac1[v]] = -ac2[v];
40+
}
41+
for(ll i=0;i<grafo[v].size();i++){
42+
ll u = grafo[v][i].first, ligau = grafo[v][i].second;
43+
if(u != p && u != big){
44+
for(ite it = (*conjunto[u]).begin();it != (*conjunto[u]).end();it++){
45+
ll dist1 = (*it).first + ligau + ac1[u];
46+
ll dist2 = (*it).second + 1 + ac2[u];
47+
if((*conjunto[v]).count(K - dist1 - ac1[v])) resp = min(resp,(*conjunto[v])[K- dist1 - ac1[v]] + dist2 + ac2[v]);
48+
}
49+
for(ite it = (*conjunto[u]).begin();it != (*conjunto[u]).end();it++){
50+
ll dist1 = (*it).first + ligau + ac1[u];
51+
ll dist2 = (*it).second + 1 + ac2[u];
52+
if((*conjunto[v]).count(dist1 - ac1[v])){
53+
(*conjunto[v])[dist1 - ac1[v]] = min((*conjunto[v])[dist1 - ac1[v]],dist2 - ac2[v]);
54+
}
55+
else{
56+
(*conjunto[v])[dist1 - ac1[v]] = dist2 - ac2[v];
57+
}
58+
}
59+
}
60+
}
61+
}
62+
int best_path(int n, int k, int h[][2], int l[]){
63+
resp = MAXN;
64+
N = n;
65+
K = k;
66+
for(int i = 0;i<n-1;i++){
67+
ll u = h[i][0];
68+
ll v = h[i][1];
69+
ll peso = l[i];
70+
u++;
71+
v++;
72+
grafo[u].PB(MP(v,peso));
73+
grafo[v].PB(MP(u,peso));
74+
}
75+
dfs_sack(1,-1);
76+
if(resp == MAXN) resp = -1;
77+
return resp;
78+
}
79+
int main(){
80+
return 0;
81+
}

DMOJ/ioi11p3.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <cstdio>
2+
#include <algorithm>
3+
using namespace std;
4+
typedef long long ll;
5+
const ll MAXN = 100010;
6+
ll vetor[MAXN],soma[MAXN],resp,ini,fim,mediana,custo,orcamento,n;
7+
void calcula_mediana(){
8+
mediana = (ini+fim)/2;
9+
}
10+
void calcula_custo(){
11+
ll temp = 0;
12+
ll lo,hi;
13+
if(mediana-1>=ini){
14+
lo = ini;
15+
hi = mediana-1;
16+
temp += (hi-lo+1)*vetor[mediana];
17+
temp -= (soma[hi] - soma[lo-1]);
18+
}
19+
if(mediana + 1 <= fim){
20+
lo = mediana+1;
21+
hi = fim;
22+
temp += soma[hi] - soma[lo-1];
23+
temp -= (hi-lo+1)*vetor[mediana];
24+
}
25+
custo = temp;
26+
}
27+
int besthub(int R,int L,int X[],long long B){
28+
n = R;
29+
orcamento = B;
30+
for(ll i=1;i<=n;i++) vetor[i] = X[i-1];
31+
for(ll i=1;i<=n;i++) soma[i] = soma[i-1] + vetor[i];
32+
ini = 1;
33+
fim = 1;
34+
resp = 1;
35+
for(fim=2;fim<=n;fim++){
36+
calcula_mediana();
37+
calcula_custo();
38+
while(custo > orcamento){
39+
ini++;
40+
calcula_mediana();
41+
calcula_custo();
42+
}
43+
resp = max(resp,fim - ini + 1);
44+
}
45+
return (int)resp;
46+
}
47+
48+
int main(){
49+
return 0;
50+
}

DMOJ/ncco3d1p1.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
typedef pair<ll,ll> ii;
5+
const ll MAXN = 2*1e4 + 20;
6+
ll ADD = 1e4 + 10;
7+
ll freqX[MAXN],freqY[MAXN],X[MAXN],Y[MAXN],totX,totY,N,precoX[MAXN],precoY[MAXN];
8+
set<ii> freq_ponto;
9+
map<ll,ll> freqprecox;
10+
vector<ii> melhores_x,melhores_y;
11+
vector<ll> candidatos;
12+
ll calcX(ll a,ll b){
13+
if(a > b ) return 0;
14+
ll resp = freqX[b];
15+
if(a != 0) resp -= freqX[a-1];
16+
return resp;
17+
}
18+
ll calcY(ll a,ll b){
19+
if(a > b ) return 0;
20+
ll resp = freqY[b];
21+
if(a != 0) resp -= freqY[a-1];
22+
return resp;
23+
}
24+
int main(){
25+
scanf("%lld",&N);
26+
for(ll i = 1;i<=N;i++){
27+
scanf("%lld %lld",&X[i],&Y[i]);
28+
X[i] += ADD;
29+
Y[i] += ADD;
30+
freqX[X[i]]++;
31+
freqY[Y[i]]++;
32+
freq_ponto.insert(ii(X[i],Y[i]));
33+
}
34+
for(ll i = 1;i<MAXN;i++){
35+
freqX[i] += freqX[i-1];
36+
freqY[i] += freqY[i-1];
37+
}
38+
for(ll i = 1;i<=N;i++) totX += X[i];
39+
melhores_x.push_back(ii(totX,0));
40+
precoX[0] = totX;
41+
for(ll i = 1;i<MAXN;i++){
42+
totX += calcX(0,i-1);
43+
totX -= calcX(i,MAXN-1);
44+
melhores_x.push_back(ii(totX,i));
45+
freqprecox[totX]++;
46+
precoX[i] = totX;
47+
}
48+
for(ll i = 1;i<=N;i++) totY += Y[i];
49+
melhores_y.push_back(ii(totY,0));
50+
precoY[0] = totY;
51+
for(ll i = 1;i<MAXN;i++){
52+
totY += calcY(0,i-1);
53+
totY -= calcY(i,MAXN-1);
54+
melhores_y.push_back(ii(totY,i));
55+
precoY[i] = totY;
56+
}
57+
sort(melhores_x.begin(),melhores_x.end());
58+
sort(melhores_y.begin(),melhores_y.end());
59+
while(melhores_x.size() >= 500) melhores_x.pop_back();
60+
while(melhores_y.size() >= 500) melhores_y.pop_back();
61+
for(ii parx : melhores_x){
62+
for(ii pary : melhores_y){
63+
if(!freq_ponto.count(ii(parx.second,pary.second))){
64+
candidatos.push_back(parx.first+pary.first);
65+
}
66+
}
67+
}
68+
sort(candidatos.begin(),candidatos.end());
69+
ll melhor= candidatos[0],qtd = 0;
70+
for(ll i = 0;i<MAXN;i++){
71+
if(freqprecox.count(melhor - precoY[i])) qtd += freqprecox[melhor - precoY[i]];
72+
}
73+
for(ll i = 1;i<=N;i++){
74+
if(precoX[X[i]] + precoY[Y[i]] == melhor) qtd--;
75+
}
76+
printf("%lld %lld\n",melhor,qtd);
77+
return 0;
78+
}

0 commit comments

Comments
 (0)