답안 #187159

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
187159 2020-01-12T13:22:24 Z Ruxandra985 Alternating Current (BOI18_alternating) C++14
0 / 100
65 ms 9972 KB
#include <bits/stdc++.h>
#define DIMN 100010
using namespace std;
pair <int,pair <int,int> > v[DIMN],w[DIMN];
int sol[DIMN] , par[DIMN] , sp1[DIMN] , sp2[DIMN];
set <pair <int,int> > s;
int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int n , m , i , maxi , poz, poz2 , maxi2, ed , ok;
    fscanf (fin,"%d%d",&n,&m);
    maxi = 0;
    poz = 0;
    for (i=1;i<=m;i++){
        fscanf (fin,"%d%d",&v[i].first,&v[i].second.first);
        v[i].second.second = i;
        w[i] = v[i];
    }
    maxi2 = 0;
    poz2 = 0;
    sort (v + 1 , v + m + 1);
    for (i=1;i<=m;i++){
        if (v[i].first > v[i].second.first && maxi < v[i].second.first){
            maxi = v[i].second.first;
            poz = i;
        }
    }
    for (i=1;i<=m;i++){
        /// check daca i e inclus total in vreun alt interval
        if (v[i].first <= v[i].second.first){ /// e un interval normal
            if (maxi > v[i].second.first){ /// e inclus
                par[i] = poz;
                continue;
            }
            else{
                maxi = v[i].second.first;
                poz = i;
            }
        }
        else {
            maxi = n; /// le tratezi ca pe doua intervale
            poz = i;
            if (maxi2 > v[i].second.first){ /// e inclus
                par[i] = poz2;
                continue;
            }
            else {
                maxi2 = v[i].second.first;
                poz2 = i;
            }
        }
        par[i] = -1; /// e tata
    }
    /// daca au ceva in comun, pui semne opuse
    ed = 0;
    ok = 0;
    for (i=1;i<=m;i++){
        if (par[i] == -1){

            if (v[i].first > v[i].second.first){
                if (!ok){
                    if (ed < v[i].first)
                        sol[v[i].second.second] = 0;
                    else { /// vreau sa il gasesc pe tatal lui, care e unic
                        /// intervalul al carui sfarsit e cel mai mic mai are decat inc
                        while (!s.empty() && v[i].first > s.begin()->first){
                            s.erase(s.begin());
                        }
                        sol[v[i].second.second] = 1 - sol[s.begin()->second];
                    }
                    s.insert(make_pair(v[i].second.first , v[i].second.second));
                    ed = max(ed , v[i].second.first);
                }
                else {
                    sol[v[i].second.second] = 1 - sol[v[ok].second.second];
                }
                ok = i;
            }
            else if (ed < v[i].first)
                sol[v[i].second.second] = 0;
            else { /// vreau sa il gasesc pe tatal lui, care e unic
                /// intervalul al carui sfarsit e cel mai mic mai are decat inc
                while (!s.empty() && v[i].first > s.begin()->first){
                    s.erase(s.begin());
                }
                sol[v[i].second.second] = 1 - sol[s.begin()->second];
            }
            s.insert(make_pair(v[i].second.first , v[i].second.second));
            ed = v[i].second.first;
        }
        else {
            sol[v[i].second.second] = 1 - sol[v[par[i]].second.second];
        }
    }

    for (i=1;i<=m;i++){
        if (w[i].first <= w[i].second.first){
            if (sol[i] == 1){
                sp1[w[i].first]++;
                sp1[w[i].second.first+1]--;
            }
            else {
                sp2[w[i].first]++;
                sp2[w[i].second.first+1]--;
            }
        }
        else {
            if (sol[i] == 1){
                sp1[w[i].first]++;
                sp1[1]++;
                sp1[w[i].second.first+1]--;
            }
            else {
                sp2[w[i].first]++;
                sp2[1]++;
                sp2[w[i].second.first+1]--;
            }
        }
    }

    for (i=1;i<=n;i++){
        sp1[i] += sp1[i-1];
        sp2[i] += sp2[i-1];

        if (!sp1[i] || !sp2[i]){
            fprintf (fout,"impossible");
            return 0;
        }

    }

    for (i=1;i<=m;i++){
        fprintf (fout,"%d",sol[i]);
    }
    return 0;
}

Compilation message

alternating.cpp: In function 'int main()':
alternating.cpp:12:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf (fin,"%d%d",&n,&m);
     ~~~~~~~^~~~~~~~~~~~~~~~~~
alternating.cpp:16:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf (fin,"%d%d",&v[i].first,&v[i].second.first);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 7 ms 376 KB Output is correct
4 Incorrect 2 ms 376 KB 'impossible' claimed, but there is a solution
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 7 ms 376 KB Output is correct
4 Incorrect 2 ms 376 KB 'impossible' claimed, but there is a solution
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 7 ms 376 KB Output is correct
4 Incorrect 2 ms 376 KB 'impossible' claimed, but there is a solution
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 9972 KB Output is correct
2 Correct 4 ms 1132 KB Output is correct
3 Correct 16 ms 2936 KB Output is correct
4 Correct 22 ms 3320 KB Output is correct
5 Correct 52 ms 5240 KB Output is correct
6 Correct 46 ms 5416 KB Output is correct
7 Incorrect 44 ms 4960 KB 'impossible' claimed, but there is a solution
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 7 ms 376 KB Output is correct
4 Incorrect 2 ms 376 KB 'impossible' claimed, but there is a solution
5 Halted 0 ms 0 KB -