이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define MAXX 100000000010LL
#define MAXN 100010
#define MAXK 110
using namespace std;
ll seg[MAXK][MAXN], dp[MAXN][MAXK], a[MAXN];
int n, k, lv[MAXN];
ll query(int i, int node, int l, int r, int levo, int desno)
{
if (levo>r || desno<l) return MAXX;
if (levo<=l && desno>=r) return seg[i][node];
int mid=l+(r-l)/2;
return min(query(i, 2*node, l, mid, levo, desno),
query(i, 2*node+1, mid+1, r, levo, desno));
}
void upd(int i, int node, int l, int r, int pos, int val)
{
if (pos>r || pos<l) return;
if (l==r) { seg[i][node]=val; return; }
int mid=l+(r-l)/2;
upd(i, 2*node, l, mid, pos, val);
upd(i, 2*node+1, mid+1, r, pos, val);
seg[i][node]=min(seg[i][2*node], seg[i][2*node+1]);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>k;
for (int i=1; i<=n; i++) cin>>a[i];
stack<pii> st;
st.push({1000010, 0});
for (int i=1; i<=n; i++)
{
while (st.top().xx<a[i]) st.pop();
lv[i]=st.top().yy;
//cout<<lv[i]<<" ";
st.push({a[i], i});
}
//cout<<endl;
for (int i=0; i<=n; i++) for (int j=1; j<=k; j++) { dp[i][j]=MAXX; upd(j, 1, 0, n-1, i-1, MAXX); }
dp[1][1]=a[1]; upd(1, 1, 0, n-1, 0, dp[1][1]);
for (int i=2; i<=n; i++)
{
//cout<<i<<endl;
for (int j=1; j<=min(k, i); j++)
{
dp[i][j]=query(j-1, 1, 0, n-1, lv[i], i-2)+a[i];
//cout<<dp[i][j]<<" ";
dp[i][j]=min(dp[i][j], dp[lv[i]][j]);
//cout<<dp[i][j]<<endl;
}
for (int j=i+1; j<=k; j++) dp[i][j]=MAXX;
for (int j=1; j<=k; j++) upd(j, 1, 0, n-1, i-1, dp[i][j]);
}
cout<<dp[n][k];
}
컴파일 시 표준 에러 (stderr) 메시지
blocks.cpp: In function 'int main()':
blocks.cpp:8:14: warning: overflow in conversion from 'long long int' to 'int' changes value from '100000000010' to '1215752202' [-Woverflow]
8 | #define MAXX 100000000010LL
| ^~~~~~~~~~~~~~
blocks.cpp:56:95: note: in expansion of macro 'MAXX'
56 | for (int i=0; i<=n; i++) for (int j=1; j<=k; j++) { dp[i][j]=MAXX; upd(j, 1, 0, n-1, i-1, MAXX); }
| ^~~~
# | 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... |