답안 #989708

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
989708 2024-05-28T15:50:41 Z vjudge1 수열 (APIO14_sequence) C++17
100 / 100
696 ms 83040 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>


using namespace std;

// #define int long long
#define ll long long
#define vec vector<long long>
#define pb push_back
#define sz(a) a.size()
#define fi first
#define se second
#define ret(a) return cout<<(a)<<"\n",void()
#define endl "\n"
#define el cout<<"\n"
#define f(a) for(int i=0;i<(a);i++)
#define f1(a) for(int i=1;i<(a);i++)
#define all(v) v.begin(),v.end()
template<class T> void PRINTARR(T a[], int n){for(int i=0;i<n;i++){std::cout<<a[i]<<" ";}std::cout<<'\n';}
template<class T> void PRINTVEC(std::vector<T> arr){for(int i=0;i<arr.size();i++){std::cout<<arr[i]<<" ";}std::cout << '\n';}

int tc=1;
void input(){
}

const int oo = 1e9+7;
const int maxn = 1e5+10;
const int mod = 1e9+7;
const int S = 360;

//REMEMBER:
//USE LONG LONG WHEN NECESSARY
//ALWAYS USE "\n" OR el;
//PRAGMA?

ll dp[2][maxn];
int trace[203][maxn];
int n,k;
vector<ll> pf;
vector<int> a;

void cal(int cur, int l, int r, int otpl, int otpr){
	if(l>r) return;
	int mid=(l+r)/2;
	int otp;
	for(int k=min(mid,otpr);k>=otpl;k--){
		// update(k,mid);
		ll x = dp[0][k-1] + (pf[mid] - pf[k-1])*(pf[n] - pf[mid]);
		if(x>=dp[1][mid]){
			dp[1][mid]=x;
            trace[cur][mid]=k-1;
			otp=k;
		}
	}
	cal(cur, l,mid-1,otpl,otp);
	cal(cur, mid+1,r,otp,otpr);
}

void solve()
{
	cin>>n>>k;
	a.resize(n+1);
    pf.resize(n+1);
	f1(n+1){
        cin>>a[i];
        pf[i] = a[i];
        pf[i] += pf[i-1];
    }
    for(int i=0;i<203;i++){
        for(int j=0;j<maxn;j++){
            trace[i][j]=-1;
            if(i<2){
                dp[i][j]=-oo;
            }
        }
    }

    for(int i=1;i<=n;i++){
        dp[0][i] = pf[i]*(pf[n]-pf[i]);
        trace[1][i] = 0;
    }

	for(int i=2;i<=k+1;i++){
		cal(i,1,n,1,n);
		for(int j=0;j<=n;j++){
			dp[0][j]=dp[1][j];
			dp[1][j]=-oo;
		}
	}
    cout<<dp[0][n]<<"\n";
    int xx = n, yy=k+1;
    vector<int> sus;
    while(trace[yy][xx]!=-1){
        // cout<<trace[yy][xx]+1<<" ";
        sus.pb(trace[yy][xx]+1);
        xx=trace[yy][xx];
        yy--;
    }
    sus.pop_back();
    reverse(all(sus));
    for(int x:sus)cout<<x-1<<" ";
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	input();
    while (tc--)
        solve();

    return 0;
}

Compilation message

sequence.cpp: In function 'void cal(int, int, int, int, int)':
sequence.cpp:58:5: warning: 'otp' may be used uninitialized in this function [-Wmaybe-uninitialized]
   58 |  cal(cur, l,mid-1,otpl,otp);
      |  ~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 81232 KB contestant found the optimal answer: 108 == 108
2 Correct 33 ms 81376 KB contestant found the optimal answer: 999 == 999
3 Correct 39 ms 81380 KB contestant found the optimal answer: 0 == 0
4 Correct 32 ms 81232 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 33 ms 81428 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 33 ms 81240 KB contestant found the optimal answer: 1 == 1
7 Correct 30 ms 81244 KB contestant found the optimal answer: 1 == 1
8 Correct 29 ms 81244 KB contestant found the optimal answer: 1 == 1
9 Correct 30 ms 81244 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 29 ms 81244 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 30 ms 81244 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 29 ms 81440 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 31 ms 81244 KB contestant found the optimal answer: 140072 == 140072
14 Correct 37 ms 81260 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 30 ms 81236 KB contestant found the optimal answer: 805 == 805
16 Correct 30 ms 81232 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 30 ms 81244 KB contestant found the optimal answer: 999919994 == 999919994
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 81244 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 29 ms 81240 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 32 ms 81232 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 34 ms 81244 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 32 ms 81236 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 32 ms 81204 KB contestant found the optimal answer: 933702 == 933702
7 Correct 32 ms 81452 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 32 ms 81244 KB contestant found the optimal answer: 687136 == 687136
9 Correct 33 ms 81236 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 38 ms 81232 KB contestant found the optimal answer: 29000419931 == 29000419931
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 81236 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 31 ms 81292 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 32 ms 81236 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 30 ms 81244 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 35 ms 81236 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 32 ms 81232 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 43 ms 81288 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 42 ms 81236 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 33 ms 81232 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 32 ms 81232 KB contestant found the optimal answer: 490686959791 == 490686959791
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 81236 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 31 ms 81396 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 33 ms 81236 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 32 ms 81244 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 38 ms 81236 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 35 ms 81236 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 35 ms 81500 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 33 ms 81236 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 37 ms 81488 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 36 ms 81236 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 81488 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 32 ms 81496 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 73 ms 81516 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 35 ms 81488 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 72 ms 81500 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 69 ms 81576 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 76 ms 81380 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 63 ms 81488 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 70 ms 81500 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 74 ms 81500 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 82512 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 48 ms 82512 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 529 ms 83040 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 44 ms 82768 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 696 ms 82808 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 471 ms 82772 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 540 ms 82772 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 377 ms 83028 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 416 ms 82748 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 476 ms 82848 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845