0% found this document useful (0 votes)
64 views8 pages

Hack With Inf Y

The document discusses optimizing the total tastiness obtained from eating different discrete and continuous dishes with varying initial tastiness and decay rates under a weight limit. It describes representing the dishes as vectors and using sorting and dynamic programming to find the maximum total tastiness.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views8 pages

Hack With Inf Y

The document discusses optimizing the total tastiness obtained from eating different discrete and continuous dishes with varying initial tastiness and decay rates under a weight limit. It describes representing the dishes as vectors and using sorting and dynamic programming to find the maximum total tastiness.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

You are buying lunch at a hotel.

Some of the dishes, such as Chinese dumplings and fried potatoes,


consist of pieces of roughly equal size, and you can pick an integral number of such pieces (no
splitting is allowed). Refer to these as “discrete dishes.” Other dishes, such as cacık or mashed
potatoes, are fluid and you can pick an real-valued amount of them. Refer to this second type as
“continuous dishes.”Of course, you like some of the dishes more than others, but how much you like a
dish also depends on how much of it you have already eaten. For instance, even if you generally
prefer dumplings to potatoes, you might prefer a potato over a dumpling if you have already eaten ten
dumpling
Each dish i has an initial tastiness di,
a rate of decay of the tastiness Δdi.

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

main() {
int D, W;
while (cin >> D >> W) {
vector<pair<int, pair<int, int> > > MD;
vector<pair<int, double> > MC;
for (int i = 0; i < D; i++) {
char ch;
int w, t, dt;
cin >> ch;
if (ch == 'D') {
cin >> w >> t >> dt;
MD.push_back(make_pair(w, make_pair(t, dt)));
} else {
cin >> t >> dt;
MC.push_back(make_pair(t, dt));
}
}
vector<double> vc(W+1, -1e15);
vc[0] = 0;
double cw = 0.0, cv = 0.0;
int cwi = 0;
sort(MC.begin(), MC.end());
while (cwi <= W && MC.size()) {
while (MC.size() >= 2 && MC[MC.size()-2].first == MC[MC.size()-1].first) {
MC[MC.size()-2].second = 1.0 / (1.0/MC[MC.size()-1].second +
1.0/MC[MC.size()-2].second);
MC.pop_back();
}
double curt = MC.back().first, curdt = MC.back().second;
double nw = (curdt == 0.0) ? W+1 : cw + 1.0 / curdt;
while (cwi <= W && cwi <= nw) {
vc[cwi] = cv + (cwi-cw)*(curt + curt-(cwi-cw)*curdt)/2;
cwi++;
}
cv += (nw-cw)*(curt + curt-(nw-cw)*curdt)/2;
cw = nw;
MC.back().first--;
}

vector<long long> vd(W+1, -1000000000000000LL);


vd[0] = 0;
random_shuffle(MD.begin(), MD.end());
for (int i = 0; i < MD.size(); i++) {
int w = MD[i].first, t = MD[i].second.first, dt = MD[i].second.second;
for (int j = W-w; j >= 0; j--) {
long long tt = t, ct = t;
for (int k = j+w; k <= W; k += w) {
if (vd[j] + tt <= vd[k]) break;
vd[k] = vd[j] + tt;
ct -= dt;
tt += ct;
}
}
}

double ret = -1e15;


for (int i = 0; i <= W; i++) ret = max(ret, vd[i] + vc[W-i]);
if (ret > -5e14) {
cout<<setprecision(9)<<fixed<<ret;
}
}
}

The Rocky Mountain National Park is opening up for tourist traffic. The national park has several
sites worth seeing and streets that connect pairs of sites. The park officials have put together a set of
round tours in the park in which visitors can ride buses to view various sites. Each round tour starts at
some site (potentially different sites for different tours), visits some other sites without repeating any,
and then returns to where it started. At least 3 different sites are visited in each round tour. At least
one round tour is possible in the national park.

#include <iostream>
#include <vector>
using namespace std;

int Gcd(int a, int b) {return b ? Gcd(b, a%b) : a;}

vector<vector<int> > c;

int goal, cura, curb, eq;


vector<int> depth;
pair<int, bool> doit(int x, int prev, int d) {
pair<int, bool> ret(d, x == goal);
depth[x] = d;
for (int i = 0; i < c[x].size(); i++) {
int y = c[x][i];
if (y == prev) continue;
if (cura == x && curb == y || curb == x && cura == y) continue;
if (depth[y] != -1) {
ret.first = min(ret.first, depth[y]);
} else {
pair<int, bool> v = doit(y, x, d+1);
ret.first = min(ret.first, v.first);
if (v.second) ret.second = true;
if (v.second && v.first == d+1) eq++;
//if (v.second && v.first == d+1) cout << ' ' << x << ',' << y;
}
}
return ret;
}

main() {
int n, m;
while (cin >> n >> m) {
c = vector<vector<int> >(n+1);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
c[a].push_back(b);
c[b].push_back(a);
}

int ret = 0;
for (int a = 1; a <= n; a++)
for (int i = 0; i < c[a].size(); i++) if (c[a][i] > a) {
depth = vector<int>(n+1, -1);
cura = a; goal = curb = c[a][i]; eq = 1;
//cout << cura << ',' << curb << " -";
if (doit(a, -1, 0).second) ret = Gcd(ret, eq);
//cout << endl;
}

cout << "1";


for (int i = 2; i <= ret; i++) if (ret%i == 0) cout << ' ' << i;
cout << endl;
}
}

You are about to meet Peterson, a close friend, and former partner in crime. Peterson lost most of his
money betting on a coding contest, so now he needs another job. For this, he needs your help, even
though you have retired from a life of crime. There is a shipment of expensive consumer gadgets in a
nearby distribution center and Peterson going to steal as much of it as he can. Finding a way into the
building, Peterson must pass through incapacitating security guards, arrays of laser beams. However,
the heart of the distribution center has been equipped with a security system that Peterson cannot
disable. So only he needs your help.

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

typedef vector<int> VI;


typedef vector<VI> VVI;
// Returns -1 for unmatched items.
// Complexity: O(V*E)
VI BipartiteMatch(const VVI &mat, VI *back_match = NULL) {
int max_item = -1;
VI fmat(mat.size(), -1), seen(mat.size(), -1), prev(mat.size());
for (int i = 0; i < mat.size(); i++) if (mat[i].size())
max_item = max(max_item, *max_element(mat[i].begin(), mat[i].end()));
VI bmat(max_item+1, -1);

for (int i = 0; i < mat.size(); i++) {


VI q(1, i);
seen[i] = i; prev[i] = -1;
int x, y;
while (!q.empty()) {
x = q.back(); q.pop_back();
for (VI::const_iterator it = mat[x].begin(); it != mat[x].end(); ++it) {
int bm = bmat[*it];
if (bm == -1) {y = *it; goto found_match;}
if (seen[bm] < i) {
seen[bm] = i; prev[bm] = x;
q.push_back(bm);
}
}
}
continue;
found_match:
while (x != -1) {
bmat[y] = x;
swap(y, fmat[x]);
x = prev[x];
}
}

if (back_match) *back_match = bmat;


return fmat;
}

int main() {
int X, Y;
while (cin >> Y >> X) {
VVI g(Y, vector<int>(X));
VI xh(X), yh(Y);
long long ret = 0;
for (int y = 0; y < Y; y++)
for (int x = 0; x < X; x++) {
cin >> g[y][x];
ret += max(0, g[y][x] - 1);
xh[x] = max(xh[x], g[y][x]);
yh[y] = max(yh[y], g[y][x]);
}

set<int> seen;
for (int x = 0; x < X; x++) if (xh[x] > 1) {
ret -= xh[x]-1;
if (seen.count(xh[x])) continue;
seen.insert(xh[x]);

VVI mat;
for (int x2 = 0; x2 < X; x2++) if (xh[x2] == xh[x]) {
mat.push_back(vector<int>());
for (int y2 = 0, yi = 0; y2 < Y; y2++) if (yh[y2] == xh[x]) {
if (g[y2][x2]) mat.back().push_back(yi);
yi++;
}
}
VI v = BipartiteMatch(mat);
for (int i = 0; i < v.size(); i++) if (v[i] != -1) ret += xh[x]-1;
}
for (int y = 0; y < Y; y++) ret -= max(0, yh[y]-1);

cout << ret << endl;


}
}

You might also like