Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jaehyun Koo
authored and
Jaehyun Koo
committed
May 29, 2016
1 parent
cd19a5f
commit 759719a
Showing
6 changed files
with
316 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <time.h> | ||
#include <iostream> | ||
#include <functional> | ||
#include <numeric> | ||
#include <algorithm> | ||
#include <stack> | ||
#include <queue> | ||
#include <deque> | ||
#include <vector> | ||
#include <string> | ||
#include <bitset> | ||
#include <map> | ||
#include <set> | ||
using namespace std; | ||
typedef long long lint; | ||
typedef long double llf; | ||
typedef pair<int, int> pi; | ||
const lint inf = 1e16; | ||
|
||
int n, m, sz[250005]; | ||
lint dep[250005], c[250005]; | ||
|
||
vector<int> gph[250005]; | ||
|
||
struct func{ | ||
priority_queue<lint> pq; | ||
lint cost; | ||
int slope; | ||
void init(){ | ||
cost = 0; | ||
slope = -1; | ||
pq.push(0); | ||
pq.push(0); | ||
} | ||
void upperize(int x){ | ||
cost += c[x]; | ||
while(slope + (int)pq.size() > 1){ | ||
pq.pop(); | ||
} | ||
vector<lint> v; | ||
while(!pq.empty() && slope + (int)pq.size() >= 0){ | ||
v.push_back(pq.top()); | ||
pq.pop(); | ||
} | ||
while(!v.empty()){ | ||
pq.push(v.back() + c[x]); | ||
v.pop_back(); | ||
} | ||
} | ||
}dp[250005]; | ||
|
||
bool cmp(int a, int b){ | ||
return sz[a] > sz[b]; | ||
} | ||
|
||
void dfs(int x){ | ||
if(x > n){ | ||
sz[x] = 1; | ||
return; | ||
} | ||
for(int i=0; i<gph[x].size(); i++){ | ||
dep[gph[x][i]] = dep[x] + c[gph[x][i]]; | ||
dfs(gph[x][i]); | ||
sz[x] += sz[gph[x][i]]; | ||
} | ||
sort(gph[x].begin(), gph[x].end(), cmp); | ||
} | ||
|
||
int solve(int x){ | ||
if(x > n){ | ||
dp[x].init(); | ||
return x; | ||
} | ||
int ret = solve(gph[x][0]); | ||
dp[ret].upperize(gph[x][0]); | ||
for(int i=1; i<gph[x].size(); i++){ | ||
int t = solve(gph[x][i]); | ||
dp[t].upperize(gph[x][i]); | ||
dp[ret].cost += dp[t].cost; | ||
dp[ret].slope += dp[t].slope; | ||
while(!dp[t].pq.empty()){ | ||
dp[ret].pq.push(dp[t].pq.top()); | ||
dp[t].pq.pop(); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
int main(){ | ||
cin >> n >> m; | ||
for(int i=2; i<=n+m; i++){ | ||
int p; | ||
scanf("%d %lld",&p,&c[i]); | ||
gph[p].push_back(i); | ||
} | ||
dfs(1); | ||
func ret = dp[solve(1)]; | ||
ret.upperize(0); | ||
lint ansp = ret.pq.top(); | ||
lint ans = ret.cost + ansp * ret.slope; | ||
while(!ret.pq.empty()){ | ||
ans += ansp - ret.pq.top(); | ||
ret.pq.pop(); | ||
} | ||
printf("%lld",ans); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <time.h> | ||
#include <iostream> | ||
#include <functional> | ||
#include <numeric> | ||
#include <algorithm> | ||
#include <stack> | ||
#include <queue> | ||
#include <deque> | ||
#include <vector> | ||
#include <string> | ||
#include <bitset> | ||
#include <map> | ||
#include <set> | ||
using namespace std; | ||
typedef long long lint; | ||
typedef long double llf; | ||
typedef pair<int, int> pi; | ||
|
||
int n; | ||
pi a[100005]; | ||
int ret[100005]; | ||
int main(){ | ||
cin >> n; | ||
for(int i=0; i<n; i++){ | ||
int s, t; | ||
scanf("%d %d",&s, &t); | ||
a[i] = pi(t+s, t-s); | ||
} | ||
sort(a, a+n); | ||
map<int, int> mp; | ||
int pt = 0; | ||
for(int i=0; i<n; i++){ | ||
auto tmp = mp.upper_bound(a[i].second); | ||
if(tmp == mp.begin()){ | ||
mp[a[i].second] = ++pt; | ||
ret[i] = pt; | ||
} | ||
else{ | ||
tmp--; | ||
int pnt = tmp->second; | ||
mp.erase(tmp); | ||
mp[a[i].second] = pnt; | ||
ret[i] = pnt; | ||
} | ||
} | ||
printf("%d\n",pt); | ||
for(int i=0; i<n; i++){ | ||
lint s = (1ll * a[i].first - 1ll * a[i].second) / 2; | ||
lint t = a[i].first - s; | ||
printf("%lld %lld %d\n",s, t, ret[i]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include <cstdio> | ||
|
||
int main(){ | ||
int a, b; | ||
scanf("%d %d",&a,&b); | ||
printf("%d",a+b); | ||
} |
Oops, something went wrong.