Submission #1105281

#TimeUsernameProblemLanguageResultExecution timeMemory
1105281ducanh1234Split the sequence (APIO14_sequence)C++17
0 / 100
3 ms768 KiB
#include<bits/stdc++.h>
using namespace std;
template<class... T> void debug(T&&... t){ int x = sizeof...(t); ((cout << t << (--x ? ", " : "]\n")), ...); }

#if ONLINE_JUDGE
   #define default_file() 88
   #define dbg(...) 48
#else 
   #define default_file() freopen("C:\\Users\\DUC ANH\\Downloads\\Gen\\default\\input.in", "r", stdin);  freopen("C:\\Users\\DUC ANH\\Downloads\\Gen\\default\\output.out", "w", stdout); 
   #define dbg(...) cout << "LINE(" << __LINE__ << ") : [" << #__VA_ARGS__ << "] = [", debug(__VA_ARGS__)
#endif
#define FOR(i, a, b) for(int i = (a); i <= (b); ++(i))
#define all(x) x.begin(), x.end()

const int mxN = 1e5 + 6;

int a[mxN], pre[mxN];

auto sumSegment(int l, int r) { 
    return pre[r] - (l > 0 ? pre[l - 1] : 0); 
}

auto getMidPos(int l, int r){
    return lower_bound(pre + l, pre + r + 1, (l > 0 ? sumSegment(0, l - 1) : 0) + (sumSegment(l, r) / 2)) - pre;
} 
auto maxPointInSeg(int l, int r){
    int midPos = getMidPos(l, r);
    int64_t point = 1LL * sumSegment(l, midPos) * sumSegment(midPos + 1, r);
    assert(l != r);
    return point;
}

struct Segment{
    int l, r;
    Segment(int _l, int _r): l(_l), r(_r) {}

    bool operator < (const Segment& other) const{
        return maxPointInSeg(l, r) > maxPointInSeg(other.l, other.r); 
    } 
};

void solve() {
    int n, k; 
    cin >> n >> k;
    
    for(int i = 0; i < n; ++i){
        cin >> a[i];
        pre[i] = (i > 0 ? pre[i-1] : 0) + a[i];
    }
    
    set<Segment> val;
    vector<int> pos;
    val.insert(Segment(0, n - 1));

    int64_t ans = 0;
    for(int i = 1; i <= k; ++i){
        auto [l, r] = *val.begin();
        // dbg(sumSegment(l, r));
        // dbg(l, r);
        val.erase(val.begin());
        ans += maxPointInSeg(l, r);
        auto midPos = getMidPos(l, r);
        pos.push_back(midPos+1);
        if(midPos > l) val.insert(Segment(l, midPos));
        if(midPos + 1 < r) val.insert(Segment(midPos + 1, r));
    }
    cout << ans << "\n";
    for(auto& p : pos)
        cout << p << ' ';
}


int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    default_file();
    solve();
    return 0;   
}

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:9:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |    #define default_file() freopen("C:\\Users\\DUC ANH\\Downloads\\Gen\\default\\input.in", "r", stdin);  freopen("C:\\Users\\DUC ANH\\Downloads\\Gen\\default\\output.out", "w", stdout);
      |                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:75:5: note: in expansion of macro 'default_file'
   75 |     default_file();
      |     ^~~~~~~~~~~~
sequence.cpp:9:113: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |    #define default_file() freopen("C:\\Users\\DUC ANH\\Downloads\\Gen\\default\\input.in", "r", stdin);  freopen("C:\\Users\\DUC ANH\\Downloads\\Gen\\default\\output.out", "w", stdout);
      |                                                                                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:75:5: note: in expansion of macro 'default_file'
   75 |     default_file();
      |     ^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...