이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
int t, n, m, k, a[100010], q, l, r;
int p[100010];
int dp[100010][2];
int32_t dir[100010][202];
bool upper(pair<pair<int, int>, pii> l1, pair<pair<int,int>, pii> l2){
return ( (l1.sc.ft*(l2.ft.ft)+l1.sc.sc)>(l2.sc.ft*(l2.ft.ft)+l2.sc.sc) );
}
int intersect(pair<pair<int, int>, pii> l1, pair<pair<int, int>, pii> l2){
int a1=l1.sc.ft, b1=l1.sc.sc, a2=l2.sc.ft, b2=l2.sc.sc;
if( ((b2-b1)%(a1-a2))==0 ){
return ( ((b2-b1)/(a1-a2)) );
}
else{
return ( ((b2-b1)/(a1-a2))+1 );
}
}
void convex_hull(int g){
vector<pair<pair<int, int>, pii> > st;
for(int j=n; j>=((g==1)?(0):(1)); j--){
pair<pair<int,int >, pii> ac={ {0ll, j}, {-p[j], dp[j][0]}};
while( (!st.empty()) && ((upper(ac, st.back())) ) ){
st.pop_back();
}
if(st.empty()){
st.pb(ac);
}
else{
if(ac.sc.ft==st.back().sc.ft){ac.ft.ft=st.back().ft.ft; st.pb(ac);}
else{
st.pb({{intersect(ac, st.back()), j}, ac.sc});}
}
}
int j=0;
for(int i=n; i>=1; i--){
while((j<(st.size()-1) ) && ( ( ((st[j+1].ft.ft<=(p[n]-p[i])) && (st[j].ft.ft<(p[n]-p[i])) ) || (st[j].ft.sc==i) )) ){
j++;
}
dp[i][1]=(p[n]-p[i])*st[j].sc.ft+st[j].sc.sc+p[i]*(p[n]-p[i]);
dir[i][g]=st[j].ft.sc;
}
for(int i=1; i<=n ;i++){
dp[i][0]=dp[i][1];
}
}
int32_t main(){
INIT
cin>>n>>k;
if(n<=1000){
for(int i=1; i<=n ;i++){
scanf("%d", &a[i]);
p[i]=p[i-1]+a[i];
}
int dp[1010][210];
dp[0][0]=0;
for(int g=1; g<=(k+1); g++){
dp[0][g]=-1;
}
k++;
vector<int> v;
for(int i=1; i<=n; i++){
for(int g=0; g<=k; g++){
dp[i][g]=-1;
}
for(int j=i-1; j>=0; j--){
for(int g=0; g<k; g++){
if(dp[j][g]==-1){continue;}
if(dp[i][g+1]==(-1)){
dp[i][g+1]=dp[j][g]+(p[i]-p[j])*(p[i]-p[j]);
dir[i][g+1]=j;
}
else{
if( (dp[j][g]+(p[i]-p[j])*(p[i]-p[j]))<dp[i][g+1] ){
dir[i][g+1]=j;
}
dp[i][g+1]=min(dp[i][g+1], dp[j][g]+(p[i]-p[j])*(p[i]-p[j]));
}
}
}
}
cout<<( ((p[n]*p[n])-dp[n][k])/2 );
cout<<"\n";
int cnt=1, cr=n;
while(cnt<k){
v.pb(dir[cr][k-cnt+1]);
cr=dir[cr][k-cnt+1];
cnt++;
}
for(int i=k-1-1; i>=0; i--){
cout<<v[i]<<" ";
}
return 0;
}
for(int i=1; i<=n; i++){
cin>>a[i];
p[i]=(p[i-1]+a[i]);
}
for(int g=1; g<=(k+1); g++){
convex_hull(g);
}
cout<<(dp[n][0] )<<"\n"<<flush;
int cnt=0, cr=n;
while(cnt<k){
cout<<dir[cr][k-cnt+1]<<" "; cr=dir[cr][k-cnt+1]; cnt++;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'void convex_hull(long long int)':
sequence.cpp:58:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while((j<(st.size()-1) ) && ( ( ((st[j+1].ft.ft<=(p[n]-p[i])) && (st[j].ft.ft<(p[n]-p[i])) ) || (st[j].ft.sc==i) )) ){
| ~^~~~~~~~~~~~~~
sequence.cpp: In function 'int32_t main()':
sequence.cpp:96:13: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
96 | scanf("%d", &a[i]);
| ~^ ~~~~~
| | |
| | long long int*
| int*
| %lld
sequence.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
96 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# | 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... |