This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int mod = (int)1e9 + 7;
void prepare(); void main_code();
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0; prepare();
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); }
}
void prepare() {};
const int D = (int)5e3 + 2;
const int N = (int)5e5 + 9;
int p[N], q[N], r[N], s[N];
int n, m, d, cnt[D], luu[D][D], ll[D];
vector<int> ev[D];
// tim q[i] dau tien < x
vector<int> val, chain;
int len = 0;
int dist(int a, int b) {
return a < b ? b - a - 1 : d - a - 1 + b;
}
const bool dbg=0;
struct linked_list {
int nxt[D], pre[D];
int max_gap = 0;
void init() {
int lst = -1;
if (dbg) {
cout << "chain: ";
FOR(i, 0, d - 1) if (cnt[i]>0)cout<<i<<' ';
cout<<nl;
}
FOR(i, 0, d - 1) {
if (cnt[i] > 0) {
pre[i] = lst;
if (lst != -1) {
nxt[lst] = i;
maxi(max_gap, dist(lst, i));
}
lst = i;
}
}
assert(lst != -1);
FOR(i, 0, d - 1) {
if (cnt[i] > 0) {
nxt[lst] = i;
pre[i] = lst;
maxi(max_gap, dist(lst, i));
break ;
}
}
if (dbg) cout << "--maxgap: " << max_gap << nl;
}
void era(int v) {
int p = pre[v], n = nxt[v];
maxi(max_gap, dist(p, n));
nxt[p] = n, pre[n] = p;
}
};
bool in(int a, int b, int x) {
if (a <= b) return (x >= a) and (x <= b);
return (x >= a) or (x <= b);
}
int solve(int x) {
int p = (val[0] >= x ? -1 : (--lower_bound(all(val), x)) - val.begin());
int u = (p == -1 ? val.back() : val[p]);
if (dbg) cout << x << ' ' << u << nl;
// ta chi can biet co nhung gia tri nao ma thoi
FOR(i, 0, d - 1) {
cnt[i] = 0, ll[i] = -1;
if (sz(ev[i])) ev[i].clear();
}
for(int v : chain) cnt[v] = 1;
vector<pair<int, int>> pos;
if (x <= u) {
FOR(i, 0, d - 1) if (cnt[i]==0) {
if (u < d - 1 and luu[i][d - 1] > u)
ll[i] = luu[i][d - 1];
if (x > 0 and luu[i][x - 1] != -1)
ll[i] = luu[i][x - 1];
cnt[i] |= ll[i] != -1;
ev[ll[i]].push_back(i);
}
} else {
FOR(i, 0, d - 1) if (cnt[i]==0) {
if (u + 1 == x) break ;
if (luu[i][x - 1] > u)
ll[i] = luu[i][x - 1];
cnt[i] |= ll[i] != -1;
ev[ll[i]].push_back(i);
}
}
linked_list px;
px.init();
int cost = INT_MAX;
int st = (x<=u?u-x+1:d-x+u+1);
for(int j = st; j <= d; ++j) {
cost = min(cost, j * (d - px.max_gap));
++u;
if (u >= d) u -= d;
for(int p : ev[u]) {
px.era(p);
}
}
return cost;
}
void main_code() {
cin >> n >> m >> d;
for(int i = 1; i <= n; ++i) cin >> p[i] >> q[i];
for(int i = 1; i <= m; ++i) cin >> r[i] >> s[i];
FOR(i, 1, n) val.push_back(q[i]);
sort(val.begin(), val.end());
FOR(i, 1, n) chain.push_back(p[i]);
unq(chain);
// [0, d - 1)
// always mot so gia tri o truoc
memset(luu, -1, sizeof luu);
FOR(i, 1, m) {
luu[r[i]][s[i]]=s[i];
}
FOR(i, 0, d - 1) {
FOR(j, 1, d - 1) {
if (luu[i][j] == -1)
luu[i][j] = luu[i][j - 1];
}
}
int ans = d * d;
for(int i = 0; i < d; ++i) {
ans = min(ans, solve(i));
}
cout << ans;
}
/* Let the river flows naturally */
Compilation message (stderr)
garden.cpp: In function 'int main()':
garden.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
garden.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |