이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
struct SegmentTree
{
vector<int> st, lazy;
int n;
/// real n
SegmentTree(int _n = 0) : n(_n)
{
st.resize(4 * n + 10);
lazy.resize(4 * n + 10);
}
void down(int id, int l, int r)
{
st[id << 1] += lazy[id];
lazy[id << 1] += lazy[id];
st[id << 1 | 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
lazy[id] = 0;
}
void update(int id, int l, int r, int u, int v, int val)
{
if (u > r || l > v)
return;
if (u <= l && r <= v)
{
st[id] += val;
lazy[id] += val;
return;
}
down(id, l, r);
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid + 1, r, u, v, val);
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v)
{
if (l > v || u > r)
return inf;
if (u <= l && r <= v)
return st[id];
down(id, l, r);
int mid = (l + r) >> 1;
return min(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
int get(int l, int r)
{
return get(1, 1, n, l, r);
}
void update(int l, int r, int val)
{
update(1, 1, n, l, r, val);
}
};
int n,d,t;
int a[maxn];
vi nen;
namespace sub4
{
struct BIT
{
int t[maxn];
BIT()
{
memset(t,-1,sizeof t);
}
void up(int x,int val)
{
for( ; x < maxn ; x += lb(x)) maximize(t[x],val);
}
int get(int x)
{
int ans = -1;
for( ; x ; x -= lb(x)) maximize(ans,t[x]);
return ans;
}
}tree;
vector<vi> f;
int l[maxn];
void solve()
{
fo(i,1,n)
{
nen.pb(a[i] - i);
nen.pb(t - i);
}
sort(all(nen));
UNIQUE(nen);
fo(i,1,n)
{
int lb = lower_bound(all(nen),t - i) - nen.begin() + 1;
l[i] = tree.get(lb);
if(a[i] <= t) l[i] = i;
lb = lower_bound(all(nen),a[i] - i) - nen.begin() + 1;
tree.up(lb,i);
}
f.resize(n + 5,vi(d + 5,inf));
f[0][0] = 0;
fo(i,1,n)
{
f[i][0] = f[i - 1][0] + (l[i] != -1);
}
fo(j,1,d)
{
SegmentTree st(n);
fo(i,1,n)
{
st.update(i,i,f[i - 1][j - 1]);
if(l[i] != -1) st.update(1,l[i],1);
f[i][j] = st.get(1,i);
}
}
cout << f[n][d];
}
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> d >> t;
fo(i,1,n) cin >> a[i];
sub4::solve();
}
컴파일 시 표준 에러 (stderr) 메시지
prison.cpp:174:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
174 | main()
| ^~~~
prison.cpp: In function 'int main()':
prison.cpp:179:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
179 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
prison.cpp:180:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
180 | freopen(name ".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... |