QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#125252 | #3998. The Profiteer | HaccerKat | TL | 402ms | 19660kb | C++20 | 6.7kb | 2023-07-16 11:31:50 | 2023-07-16 11:31:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template<typename T>
int SIZE(T (&t)){
return t.size();
}
template<typename T, size_t N>
int SIZE(T (&t)[N]){
return N;
}
string to_string(char t){
return "'" + string({t}) + "'";
}
string to_string(bool t){
return t ? "true" : "false";
}
string to_string(const string &t, int x1=0, int x2=1e9){
string ret = "";
for(int i = min(x1,SIZE(t)), _i = min(x2,SIZE(t)-1); i <= _i; ++i){
ret += t[i];
}
return '"' + ret + '"';
}
string to_string(const char* t){
string ret(t);
return to_string(ret);
}
template<size_t N>
string to_string(const bitset<N> &t, int x1=0, int x2=1e9){
string ret = "";
for(int i = min(x1,SIZE(t)); i <= min(x2,SIZE(t)-1); ++i){
ret += t[i] + '0';
}
return to_string(ret);
}
template<typename T, typename... Coords>
string to_string(const T (&t), int x1=0, int x2=1e9, Coords... C);
template<typename T, typename S>
string to_string(const pair<T, S> &t){
return "(" + to_string(t.first) + ", " + to_string(t.second) + ")";
}
template<typename T, typename... Coords>
string to_string(const T (&t), int x1, int x2, Coords... C){
string ret = "[";
x1 = min(x1, SIZE(t));
auto e = begin(t);
advance(e,x1);
for(int i = x1, _i = min(x2,SIZE(t)-1); i <= _i; ++i){
ret += to_string(*e, C...) + (i != _i ? ", " : "");
e = next(e);
}
return ret + "]";
}
template<int Index, typename... Ts>
struct print_tuple{
string operator() (const tuple<Ts...>& t) {
string ret = print_tuple<Index - 1, Ts...>{}(t);
ret += (Index ? ", " : "");
return ret + to_string(get<Index>(t));
}
};
template<typename... Ts>
struct print_tuple<0, Ts...> {
string operator() (const tuple<Ts...>& t) {
return to_string(get<0>(t));
}
};
template<typename... Ts>
string to_string(const tuple<Ts...>& t) {
const auto Size = tuple_size<tuple<Ts...>>::value;
return print_tuple<Size - 1, Ts...>{}(t);
}
void dbgr(){;}
template<typename Heads, typename... Tails>
void dbgr(Heads H, Tails... T){
cout << to_string(H) << " | ";
dbgr(T...);
}
void dbgs(){;}
template<typename Heads, typename... Tails>
void dbgs(Heads H, Tails... T){
cout << H << " ";
dbgs(T...);
}
/*
formatted functions:
*/
/*
consider __VA_ARGS__ as a whole:
dbgv() prints values only
dbg() prints name and values
*/
#define dbgv(...) cout << to_string(__VA_ARGS__) << endl;
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgv(__VA_ARGS__);
//#define dbg(...)
/*
consider __VA_ARGS__ as a sequence of arguments:
dbgr() prints values only
dbgm() prints names and values
*/
#define dbgr(...) dbgr(__VA_ARGS__); cout << endl;
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgr(__VA_ARGS__);
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
// using u128 = __uint128_t;
// using i128 = __int128;
const int mod = 1000000007;
const int N = 200005;
const int LOG = 20;
const int inf = 2e9 + 5;
const double eps = 1e-11;
int v[N], a[N], b[N];
int n, k, qq, E;
ll out = 0;
void add(vector<int> &dp, int x, int w) {
for (int i = k; i >= w; i--) {
if (dp[i - w] != -inf) {
dp[i] = max(dp[i], dp[i - w] + x);
}
}
}
bool check(vector<int> &dp) {
ll x = 0;
for (int i = 1; i <= k; i++) {
x += dp[i];
}
return x > 1LL * E * k;
}
void dnq(vector<int> dp, int l, int r, int dl, int dr) {
// dbg(dp);
// dbgm(l, r, dl, dr);
r = min(n - 1, r);
if (l > r) return;
if (dl == dr) {
for (int i = l; i <= min(n - 1, r); i++) {
out -= i - dl + 1;
}
return;
}
vector<int> cur = dp, pos;
if (dr < l) {
int sz1 = dr - dl + 1, sz2 = r - l + 1;
pos.resize(sz1 + sz2);
for (int i = 0; i < sz1; i++) {
pos[i] = i + dl;
}
for (int i = 0; i < sz2; i++) {
pos[i + sz1] = i + l;
}
}
else {
int lx = min(l, dl), rx = max(r, dr);
int sz = rx - lx + 1;
pos.resize(sz);
for (int i = 0; i < sz; i++) {
pos[i] = i + lx;
}
}
// dbg(pos);
int dm = (dl + dr) / 2, sz = pos.size(), lx, rx = sz - 1, res;
for (int i = 0; i < sz; i++) {
if (pos[i] == dm) lx = i, res = i - 1;
}
for (int i = 0; i < lx; i++) {
add(cur, v[pos[i]], a[pos[i]]);
}
// dbg(cur);
// dbgm(dm, sz, lx, rx, res);
while (lx <= rx) {
int mid = (lx + rx) / 2;
vector<int> test = cur;
for (int i = lx; i <= rx; i++) {
add(test, v[pos[i]], i <= mid ? b[pos[i]] : a[pos[i]]);
}
if (check(test)) {
for (int i = lx; i <= mid; i++) {
add(cur, v[pos[i]], b[pos[i]]);
}
lx = mid + 1, res = mid;
}
else {
for (int i = mid; i <= rx; i++) {
add(cur, v[pos[i]], a[pos[i]]);
}
rx = mid - 1;
}
}
vector<int> ldp = dp, rdp = dp;
int m = (res == -1 ? dm - 1 : pos[res]);
// dbgm(res, m);
for (int i = 0; i < sz; i++) {
int p = pos[i];
if (!(dl <= p && p <= dm) && !(l <= p && p <= m)) {
add(ldp, v[p], dm < p && p < l ? b[p] : a[p]);
}
if (!(dm + 1 <= p && p <= dr) && !(m + 1 <= p && p <= r)) {
add(rdp, v[p], dr < p && p < m + 1 ? b[p] : a[p]);
}
}
dnq(ldp, l, m, dl, dm);
dnq(rdp, m + 1, r, dm + 1, dr);
}
void solve() {
cin >> n >> k >> E;
for (int i = 0; i < n; i++) {
cin >> v[i] >> a[i] >> b[i];
}
out = 1LL * n * (n + 1) / 2;
vector<int> dp(k + 1);
dnq(dp, 0, n - 1, 0, n);
cout << out << "\n";
}
int32_t main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5500kb
input:
4 5 3 3 2 4 1 2 3 2 1 2 3 1 3
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3492kb
input:
4 5 4 3 2 4 1 2 3 2 1 2 3 1 3
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3484kb
input:
4 5 5 3 2 4 1 2 3 2 1 2 3 1 3
output:
7
result:
ok single line: '7'
Test #4:
score: 0
Accepted
time: 402ms
memory: 19660kb
input:
200000 50 23333 2620 5 21 8192 17 34 6713 38 46 6687 13 42 390 9 13 4192 7 37 7898 17 21 1471 16 45 3579 22 40 9628 8 23 7164 28 45 3669 14 31 5549 29 35 4670 30 39 5811 15 20 4162 18 29 7590 29 41 7786 23 35 383 9 40 5576 39 46 5586 4 9 1110 14 33 8568 4 6 8548 39 42 9133 10 42 6679 22 39 8353 33 3...
output:
0
result:
ok single line: '0'
Test #5:
score: -100
Time Limit Exceeded
input:
200000 50 233332 8019 18 20 3111 27 40 2015 16 47 6491 17 18 1224 30 38 428 23 34 7521 4 41 7252 6 33 4121 32 45 4230 18 35 1605 21 42 9489 17 25 3704 35 45 6202 8 22 6493 1 5 3041 14 46 4509 23 43 9646 11 48 5294 19 27 3562 19 40 9408 30 47 1751 21 29 4053 4 27 5596 32 49 8609 13 29 1686 4 31 3588 ...