-
Notifications
You must be signed in to change notification settings - Fork 13
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
Showing
12 changed files
with
451 additions
and
9 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
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
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
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,42 @@ | ||
#include <algorithm> | ||
#include <cstdio> | ||
#include <cstring> | ||
const int size = 4005; | ||
int A[size][size], B[size][size], dp[2][size], *F, *G; | ||
void solve(int l, int r, int b, int e) { | ||
if(l > r) | ||
return; | ||
int m = (l + r) >> 1, end = std::min(e, m - 1); | ||
int dpv = 1 << 30, tp; | ||
for(int i = b; i <= end; ++i) { | ||
int cv = F[i] + B[i + 1][m]; | ||
if(cv < dpv) | ||
dpv = cv, tp = i; | ||
} | ||
G[m] = dpv; | ||
solve(l, m - 1, b, tp); | ||
solve(m + 1, r, tp, e); | ||
} | ||
int main() { | ||
int n, k; | ||
scanf("%d%d", &n, &k); | ||
for(int i = 1; i <= n; ++i) | ||
for(int j = 1; j <= n; ++j) { | ||
scanf("%d", &A[i][j]); | ||
A[i][j] += A[i][j - 1]; | ||
} | ||
for(int i = 1; i <= n; ++i) { | ||
for(int j = i; j <= n; ++j) | ||
B[i][j] = | ||
B[i][j - 1] + A[j][j] - A[j][i - 1]; | ||
} | ||
memset(dp, 0x3f, sizeof(dp)); | ||
F = dp[0], G = dp[1]; | ||
G[0] = 0; | ||
for(int i = 1; i <= k; ++i) { | ||
std::swap(F, G); | ||
solve(1, n, 0, n); | ||
} | ||
printf("%d\n", G[n]); | ||
return 0; | ||
} |
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,64 @@ | ||
#include <algorithm> | ||
#include <cstdio> | ||
#include <cstring> | ||
int read() { | ||
int res = 0, c; | ||
do | ||
c = getchar(); | ||
while(c < '0' || c > '9'); | ||
while('0' <= c && c <= '9') { | ||
res = res * 10 + c - '0'; | ||
c = getchar(); | ||
} | ||
return res; | ||
} | ||
const int size = 50005; | ||
struct Edge { | ||
int u, v, w, c, rw; | ||
bool operator<(const Edge& rhs) const { | ||
return rw != rhs.rw ? rw < rhs.rw : c < rhs.c; | ||
} | ||
} E[2 * size]; | ||
int fa[size]; | ||
int find(int x) { | ||
return fa[x] != -1 ? fa[x] = find(fa[x]) : x; | ||
} | ||
std::pair<int, int> solve(int n, int m, int k) { | ||
memset(fa, -1, sizeof(int) * n); | ||
for(int i = 0; i < m; ++i) | ||
E[i].rw = E[i].w + (E[i].c ? 0 : k); | ||
std::sort(E, E + m); | ||
int sumw = 0, wcnt = 0; | ||
for(int i = 0; i < m; ++i) { | ||
int u = find(E[i].u), v = find(E[i].v); | ||
if(u != v) { | ||
sumw += E[i].rw; | ||
wcnt += (E[i].c == 0); | ||
fa[u] = v; | ||
} | ||
} | ||
return std::make_pair(wcnt, sumw); | ||
} | ||
int main() { | ||
int n = read(); | ||
int m = read(); | ||
int c = read(); | ||
for(int i = 0; i < m; ++i) { | ||
E[i].u = read(); | ||
E[i].v = read(); | ||
E[i].w = read(); | ||
E[i].c = read(); | ||
} | ||
const int range = 100; | ||
int l = -range, r = range, ans; | ||
while(l <= r) { | ||
int mid = (l + r) >> 1; | ||
std::pair<int, int> res = solve(n, m, mid); | ||
if(res.first >= c) | ||
l = mid + 1, ans = res.second - mid * c; | ||
else | ||
r = mid - 1; | ||
} | ||
printf("%d\n", ans); | ||
return 0; | ||
} |
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,54 @@ | ||
#include <algorithm> | ||
#include <cstdio> | ||
typedef double FT; | ||
const FT eps = 1e-8; | ||
const int size = 2005; | ||
FT A[size], B[size], C[size]; | ||
struct Res { | ||
FT fv; | ||
int a, b; | ||
Res(FT fv, int a, int b) : fv(fv), a(a), b(b) {} | ||
}; | ||
void CAS(FT& fv, int& da, int& db, FT cv, int a, | ||
int b) { | ||
if(cv > fv) | ||
fv = cv, da = a, db = b; | ||
} | ||
Res solve(int n, FT ka, FT kb) { | ||
FT kc = ka + kb, cfv = 0.0; | ||
int ca = 0, cb = 0; | ||
for(int i = 1; i <= n; ++i) { | ||
FT nfv = 0.0; | ||
int da = 0, db = 0; | ||
CAS(nfv, da, db, A[i] - ka, 1, 0); | ||
CAS(nfv, da, db, B[i] - kb, 0, 1); | ||
CAS(nfv, da, db, C[i] - kc, 1, 1); | ||
ca += da, cb += db, cfv += nfv; | ||
} | ||
return Res(cfv, ca, cb); | ||
} | ||
int main() { | ||
int n, a, b; | ||
scanf("%d%d%d", &n, &a, &b); | ||
for(int i = 1; i <= n; ++i) | ||
scanf("%lf", &A[i]); | ||
for(int i = 1; i <= n; ++i) | ||
scanf("%lf", &B[i]); | ||
for(int i = 1; i <= n; ++i) | ||
C[i] = A[i] + B[i] - A[i] * B[i]; | ||
FT la = 0.0, ra = 1.0, ans; | ||
while(ra - la > eps) { | ||
FT ma = (la + ra) / 2.0; | ||
FT lb = 0.0, rb = 1.0; | ||
while(rb - lb > eps) { | ||
FT mb = (lb + rb) / 2.0; | ||
Res res = solve(n, ma, mb); | ||
(res.b >= b ? lb : rb) = mb; | ||
} | ||
Res res = solve(n, ma, lb); | ||
(res.a >= a ? la : ra) = ma; | ||
ans = res.fv + a * ma + b * lb; | ||
} | ||
printf("%.8lf\n", ans); | ||
return 0; | ||
} |
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,117 @@ | ||
#include <algorithm> | ||
#include <cstdio> | ||
#include <cstring> | ||
namespace IO { | ||
const int size = 1 << 23; | ||
char in[size], *S = in; | ||
void init() { | ||
setvbuf(stdin, 0, _IONBF, 0); | ||
setvbuf(stdout, 0, _IONBF, 0); | ||
fread(in, 1, size, stdin); | ||
} | ||
char getc() { | ||
return *S++; | ||
} | ||
} | ||
int read() { | ||
int res = 0, c; | ||
bool flag = false; | ||
do { | ||
c = IO::getc(); | ||
flag |= c == '-'; | ||
} while(c < '0' || c > '9'); | ||
while('0' <= c && c <= '9') { | ||
res = res * 10 + c - '0'; | ||
c = IO::getc(); | ||
} | ||
return flag ? -res : res; | ||
} | ||
int iabs(int x) { | ||
return x < 0 ? -x : x; | ||
} | ||
const int size = 300005; | ||
struct Edge { | ||
int to, nxt, w; | ||
} E[2 * size]; | ||
int last[size], cnt = 0; | ||
void addEdge(int u, int v, int w) { | ||
++cnt; | ||
E[cnt].to = v, E[cnt].nxt = last[u], E[cnt].w = w; | ||
last[u] = cnt; | ||
} | ||
typedef long long Int64; | ||
#define asInt64 static_cast<Int64> | ||
Int64 C; | ||
struct Chain { | ||
Int64 sum; | ||
int cnt; | ||
Chain() : sum(0), cnt(0) {} | ||
Chain(Int64 sum, int cnt) : sum(sum), cnt(cnt) {} | ||
bool operator<(const Chain& rhs) const { | ||
return sum == rhs.sum ? cnt > rhs.cnt : | ||
sum < rhs.sum; | ||
} | ||
Chain operator+(const Chain& rhs) const { | ||
return Chain(sum + rhs.sum, cnt + rhs.cnt); | ||
} | ||
Chain operator+(int len) const { | ||
return Chain(sum + len, cnt); | ||
} | ||
}; | ||
Chain makeChain(const Chain& c) { | ||
return Chain(c.sum + C, c.cnt + 1); | ||
} | ||
struct Info { | ||
Chain L0, L1; | ||
Info(const Chain& L0, const Chain& L1) | ||
: L0(L0), L1(L1) {} | ||
}; | ||
Info DFS(int u, int p) { | ||
Chain L0, L1, L2; | ||
if(C > 0) | ||
L2.sum = C, L2.cnt = 1; | ||
for(int i = last[u]; i; i = E[i].nxt) { | ||
int v = E[i].to; | ||
if(v == p) | ||
continue; | ||
Info vres = DFS(v, u); | ||
L2 = | ||
std::max(L2 + vres.L0, | ||
makeChain(L1 + vres.L1 + E[i].w)); | ||
L1 = std::max(L1 + vres.L0, | ||
L0 + vres.L1 + E[i].w); | ||
L0 = L0 + vres.L0; | ||
} | ||
return Info( | ||
std::max(makeChain(L1), std::max(L0, L2)), L1); | ||
} | ||
int main() { | ||
IO::init(); | ||
int n = read(); | ||
int k = read() + 1; | ||
Int64 sum = 0; | ||
for(int i = 1; i < n; ++i) { | ||
int u = read(); | ||
int v = read(); | ||
int w = read(); | ||
addEdge(u, v, w); | ||
addEdge(v, u, w); | ||
sum += iabs(w); | ||
} | ||
if(k > 1) | ||
sum = sum / (k - 1) + 1; | ||
Int64 l = -sum, r = sum, ans = 0; | ||
while(l <= r) { | ||
Int64 m = (l + r) >> 1; | ||
C = m; | ||
Chain res = DFS(1, 0).L0; | ||
if(res.cnt <= k) | ||
l = m + 1, ans = res.sum - asInt64(m) * k; | ||
else | ||
r = m - 1; | ||
if(res.cnt == k) | ||
break; | ||
} | ||
printf("%lld\n", ans); | ||
return 0; | ||
} |
Oops, something went wrong.