답안 #399630

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
399630 2021-05-06T10:24:10 Z Lightning 수열 (APIO14_sequence) C++14
0 / 100
8 ms 1868 KB
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <cassert>
#include <stack>
#include <queue>
#include <deque>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>-

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
#define int ll
typedef pair <int, int> pii;

// template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//  order_of_key (k) : Number of items strictly smaller than k .
//  find_by_order(k) : K-th element in a set (counting from zero).
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define show(a) cerr << #a <<" -> "<< a <<" "
#define nl cerr <<"\n"

const int N = 1e5 + 5;

int n, k, a[N], sum[N], ans;
set <pii> st;

int calc(int suml, int sumr, int pos) {
    return (sum[pos] - suml) * (sumr - (sum[pos] - suml));
}

set <pii> pos;
vector <int> vec;

void solve() {
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        sum[i] = sum[i-1] + a[i];
    }

    st.insert({0, 0});
    st.insert({sum[n], n});

    pos.insert({0, 0});
    pos.insert({n, sum[n]});

    for (int _it = 1; _it <= k; ++_it) {
        // r, sumr -> st
        auto it = st.rbegin();
        int r = (it -> S) - 1;
        int sumr = (it -> F);

        // l, suml -> pos {r}
        auto it1 = prev(pos.lower_bound({r, 0}));
        int l = (it1 -> F) + 1;
        int suml = (it1 -> S);

        //show(l); show(r); show(suml); show(sumr); nl;
        while (l < r) {
            int mid = (l + r) / 2;
            if (calc(suml, sumr, mid) >= calc(suml, sumr, mid+1)) r = mid;
            else l = mid + 1;
        }
        ans += calc(suml, sumr, r);
        //show(calc(suml, sumr, r)); nl;
        vec.pb(r);

        st.insert({sumr - (sum[r] - suml), (it -> S)});
        st.insert({sum[r] - suml, r});

        pos.insert({(it -> S), sumr - (sum[r] - suml)});
        pos.insert({r, sum[r] - suml});
        //if (pos.find({it->S, it->F}) != pos.end())
        pos.erase(pos.find({it->S, it->F}));

        st.erase(*it);

        /*cout << "V set V\n";
        for (auto x : st){
            cout << x.F <<" "<< x.S <<"\n";
        }*/
    }
    cout << ans <<"\n";
    for (int p : vec) {
        cout << p <<" ";
    }
}

 main () {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int tests = 1;
	//cin >> tests;
	while (tests --) {
		solve();
	}
	return 0;
}
/*
7 3
4 1 3 4 0 2 3
*/

Compilation message

sequence.cpp:107:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  107 |  main () {
      |        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB contestant found the optimal answer: 108 == 108
2 Incorrect 1 ms 204 KB contestant didn't find the optimal answer: 951 < 999
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB position 49 occurs twice in split scheme
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB contestant didn't find the optimal answer: 413986107 < 610590000
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB declared answer doesn't correspond to the split scheme: declared = 32700474, real = 20016104
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 460 KB declared answer doesn't correspond to the split scheme: declared = 1300041111, real = 1089084171
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 8 ms 1868 KB contestant didn't find the optimal answer: 19788938955 < 19795776960
2 Halted 0 ms 0 KB -