답안 #161086

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
161086 2019-10-31T13:39:04 Z stoyan_malinin Bigger segments (IZhO19_segments) C++14
0 / 100
1500 ms 7800 KB
#include<iostream>
#include<vector>
#include<random>

using namespace std;

const int MAXN = 5e5 + 5;

mt19937 rnd(69420);

int n;
long long a[MAXN];

struct segment
{
    int l, r;
    long long sum;

    segment(){}
    segment(int l, int r, long long sum)
    {
        this->l = l;
        this->r = r;
        this->sum = sum;
    }
};

vector <segment> v;
int mySolve()
{
    v.clear();

    v.push_back(segment(0, 0, a[0]));
    segment curr = segment(1, 0, 0);
    for(int i = 1;i<n;i++)
    {
        curr.r++;
        curr.sum += a[i];

        bool update = true;
        while(update==true)
        {
            update = false;
            for(int j = v.size()-1;j>=1;j--)
            {
                while(v[j].sum - a[ v[j].l ] >= v[j-1].sum + a[ v[j].l ])
                {
                    update = true;

                    v[j].sum -= a[ v[j].l ];
                    v[j-1].sum += a[ v[j].l ];

                    v[j].l++;
                    v[j-1].r++;
                }
            }
            for(int j = 1;j<v.size();j++)
            {
                while(v[j].sum - a[ v[j].l ] >= v[j-1].sum + a[ v[j].l ])
                {
                    update = true;

                    v[j].sum -= a[ v[j].l ];
                    v[j-1].sum += a[ v[j].l ];

                    v[j].l++;
                    v[j-1].r++;
                }
            }
        }


        if(curr.sum >= v.back().sum)
        {
            v.push_back(curr);
            curr = segment(i+1, i, 0);
        }
    }

    return v.size();
}

int rec(int index, int lastSum, int currSum, int cnt)
{
    if(index==n) return cnt;

    int ans = 0;
    ans = rec(index+1, lastSum, currSum+a[index], cnt);

    if(currSum+a[index]>=lastSum)
    {
        ans = max(ans, rec(index+1, currSum+a[index], 0, cnt+1));
    }

    return ans;
}

int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(NULL);

    int answer = 0;

    while(true)
    {
        n = 1 + rnd()%10;
        for(int i = 0;i<n;i++)
        {
            a[i] = rnd()%100;
        }

        int ans1 = mySolve();
        int ans2 = rec(0, 0, 0, 0);

        if(ans1!=ans2)
        {
            cout << "FAK" << '\n';
            cout << ans1 << " " << ans2 << '\n';

            cout << n << '\n';
            for(int i = 0;i<n;i++)
            {
                cout << a[i] << " ";
            }
            cout << '\n';

            system("PAUSE");
        }
        else
        {
            cout << "ok" << '\n';
        }
    }
}
/*
5
6 2 3 9 13
*/

Compilation message

segments.cpp: In function 'int mySolve()':
segments.cpp:57:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int j = 1;j<v.size();j++)
                           ~^~~~~~~~~
segments.cpp: In function 'int main()':
segments.cpp:103:9: warning: unused variable 'answer' [-Wunused-variable]
     int answer = 0;
         ^~~~~~
segments.cpp:128:19: warning: ignoring return value of 'int system(const char*)', declared with attribute warn_unused_result [-Wunused-result]
             system("PAUSE");
             ~~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1550 ms 7800 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1550 ms 7800 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1550 ms 7800 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1550 ms 7800 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1550 ms 7800 KB Time limit exceeded
2 Halted 0 ms 0 KB -