이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*input
7 6
10 22 31 9 3 2 9
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1e5 + 100;
const int mod=1e9 + 7;
#define int long long
const int inf=1e18;
#define pii pair<int, int>
#define f first
#define s second
#define mp make_pair
#define FOR(i, n) for(int i=1;i<=n;i++)
#define TRACE(x) cerr << #x << " = " << x << endl
//Trace prints the name of the variable and the value.
int dp[N][210], arr[N], n, k, pref[N], p[N][210];
stack<int> st;int ind;
double inter(int a, int b)
{
double ret=(dp[a][ind] - dp[b][ind] + 0.0)/(pref[a] - pref[b] + 0.0);
return ret;
}
void remove()
{
while(st.size()>=3)
{
int i1=st.top(), i2, i3;st.pop();i2=st.top();st.pop();i3=st.top();
st.pop();st.push(i3);
if(inter(i1, i3)<inter(i2, i3)) st.push(i1);
else {st.push(i2);st.push(i1);return;}
}
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>k;pref[0]=0;
if(n==1)
{
cout<<0;return 0;
}
for(int i=1;i<=n;i++) {cin>>arr[i];pref[i]=arr[i] + pref[i-1];}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++) dp[i][j]=0;
}
for(int i=1;i<=n;i++)
{
dp[i][1]=pref[i]*(pref[n]-pref[i]);
}
for(int j=2;j<=k;j++)
{
st.push(j-1);ind=j-1;
for(int i=j;i<=n;i++)
{
while(st.size()>1)
{
int a=st.top();st.pop();int b=st.top();st.pop();
if(dp[a][ind] - pref[a]*(pref[n]-pref[i])<=dp[b][ind]-pref[b]*(pref[n]-pref[i]))
{
st.push(b);
}
else {st.push(b);st.push(a);break;}
}
int opt=st.top();
dp[i][j]=dp[opt][j-1] -pref[opt]*(pref[n]-pref[i]);
dp[i][j]+=(pref[i])*(pref[n]-pref[i]);p[i][j]=opt;
st.push(i);remove();
}
while(!st.empty()) st.pop();
}
int mi, mval=-inf;
for(int j=1;j<=n;j++)
{
if(mval<dp[j][k])
{
mi=j;mval=dp[j][k];
}
}
cout<<mval<<endl;
stack<int> st;st.push(mi);
while(st.size()<k)
{
int cur=p[st.top()][k-st.size()+1];st.push(cur);
}
while(!st.empty()) {cout<<st.top()<<" ";st.pop();}
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:83:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(st.size()<k)
~~~~~~~~~^~
# | 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... |