QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#531118#5080. Folding StickTiga_Pilot_2#TL 1ms3692kbC++203.0kb2024-08-24 18:26:392024-08-24 18:26:41

Judging History

你现在查看的是最新测评结果

  • [2024-08-24 18:26:41]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3692kb
  • [2024-08-24 18:26:39]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
    enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e;};
sim > rge<c> range(c i, c j) {return rge<c>{i,j};}
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug{
    ~debug() {cerr << endl;}
    eni(!=) cerr << boolalpha << i; ris; }
    eni(==) ris << range(begin(i), end(i)); }
    sim, class b dor(pair <b, c> d) {
        ris <<"(" <<d.fi <<", " <<d.se <<")";
    }
    sim dor(rge<c> d) {
        *this << "[";
        for(auto it=d.b;it!=d.e;++it) {
            *this <<", " + 2*(it==d.b) <<*it;
        }
        ris << "]";
    }
};
#define imie(...) "[" <<#__VA_ARGS__ ": " << (__VA_ARGS__) <<"]"

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = a; i > (b); --i)
#define ar array
#define sz(x) (int) (x).size()
#define pii pair<int,int>
#define fi first
#define se second
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
typedef pair<double,int> pdi;
typedef vector<int> vi;
#define all(x) (x).begin(), (x).end()

template<typename T>
void min_self(T& A, T B) {
    A = min(A,B);
}
template<typename T>
void max_self(T& A, T B) {
    A = max(A,B);
}

const int mxn=1e5;
int n;
int a[mxn];
int ans = 2e9;
vi pref;

bool chk(int r, int sm) {
    if(r<0) return 1;
    int pos = -1,pos2=-1;
    int li=0,ri=r;
    int bwh = sm-a[r+1];
    while(li<=ri) {
        int mid = (li+ri)/2;
        int sm2 = pref[r+1]-pref[mid];
        if(sm2<=bwh) {
            pos2 = mid;
            ri =mid-1;
        } else if(sm2>sm) {
            li = mid+1;
        } else {
            pos = mid;
            li = mid+1;
        }
    }
    if(pos==-1) {
        if(pos2!=-1) {
            int sm2 = pref[r+1]-pref[pos2];
            return chk(pos2-1,sm2);
        } else {
            return false;
        }
    }
    while(pos>=0) {
        int sm2 = pref[r+1]-pref[pos];
        if(sm2>sm) break;
        if(chk(pos-1,sm2)) return true;
        pos--;
    }
    return false;
}

void solve() {
    cin >>n;
    rep(i,0,n) {
        cin >>a[i];
    }
    pref.resize(n+1,0);
    rep(i,0,n) {
        pref[i+1] = pref[i]+a[i];
    }
    int suf = 0;
    per(i,n-1,-1) {
        int sm = 0;
        per(j,i,-1) {
            sm += a[j];
            if(sm<=suf) continue;
            if(sm>ans) break;
            bool res = chk(j-1,sm);
            if(res) {
                // debug() <<imie(i) imie(j) imie(sm) imie(suf);
                ans = sm;
                break;
            }
        }
        suf += a[i];
        if(suf>ans) break;
    }
    cout <<ans <<"\n";
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3580kb

input:

4
3 2 2 3

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

5
1 1 1 1 1

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

7
1 3 2 3 4 2 2

output:

6

result:

ok single line: '6'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

9
5 6 3 4 8 8 2 2 5

output:

9

result:

ok single line: '9'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

10
5 6 3 4 8 6 2 1 8 5

output:

9

result:

ok single line: '9'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

10
5 8 1 2 6 8 4 3 6 5

output:

14

result:

ok single line: '14'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

3
4 2 1

output:

4

result:

ok single line: '4'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

14
7 2 2 2 2 3 4 1 3 5 4 3 1 6

output:

8

result:

ok single line: '8'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3692kb

input:

35
46 93 64 27 72 55 77 11 17 17 79 83 74 26 32 101 54 112 92 111 77 60 51 19 105 11 68 7 100 49 88 54 106 80 57

output:

366

result:

ok single line: '366'

Test #10:

score: 0
Accepted
time: 0ms
memory: 3640kb

input:

150
87 121 113 120 23 32 107 92 107 40 61 29 100 120 30 62 61 53 103 40 110 56 16 38 12 55 11 71 109 26 60 72 19 121 74 97 11 87 117 32 58 40 104 91 101 118 19 59 79 21 40 111 100 36 105 58 122 61 33 75 66 11 65 97 84 28 90 18 76 68 70 58 112 100 95 28 61 25 24 110 93 117 80 119 105 52 66 66 101 77 ...

output:

765

result:

ok single line: '765'

Test #11:

score: 0
Accepted
time: 1ms
memory: 3620kb

input:

200
52 59 17 69 83 28 76 102 79 67 66 19 14 114 28 50 68 88 92 23 84 38 71 51 77 76 30 82 67 18 34 41 92 23 72 35 27 94 96 74 12 92 32 60 56 42 123 38 14 99 20 83 16 111 47 101 33 34 81 111 123 108 53 45 73 59 11 26 65 59 111 86 81 73 86 106 101 37 109 56 91 91 26 77 122 99 118 42 43 103 118 19 116 ...

output:

842

result:

ok single line: '842'

Test #12:

score: -100
Time Limit Exceeded

input:

9999
96 57 105 94 47 39 95 44 103 98 84 99 94 109 65 83 113 102 57 67 13 81 113 31 123 55 27 76 74 94 12 40 88 91 52 78 60 76 118 43 92 102 13 118 76 47 64 99 80 83 108 14 120 16 12 105 82 24 102 99 52 121 81 72 14 74 108 58 117 24 72 14 56 58 122 50 70 114 87 60 61 21 54 58 103 56 75 51 112 27 23 5...

output:


result: