제출 #1015785

#제출 시각아이디문제언어결과실행 시간메모리
1015785andrei_iorgulescuJakarta Skyscrapers (APIO15_skyscraper)C++14
22 / 100
3 ms4444 KiB
#include <bits/stdc++.h>

using namespace std;

int hsh(pair<int,int> pr)
{
    return pr.first * 31000 + pr.second;
}

int n,m;
int b[30005],p[30005];
vector<int> what[30005];
set<int> resturi[30005];
vector<int> r[30005];
vector<int> noduri[30005];
int splv[30005];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
        cin >> b[i] >> p[i],what[b[i]].push_back(p[i]);
    for (int i = 1; i <= m; i++)
        resturi[p[i]].insert(b[i] % p[i]);
    for (int i = 1; i < n; i++)
        for (auto it : resturi[i])
            r[i].push_back(it);
    for (int i = 0; i < n; i++)
        noduri[0].push_back(i);
    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < n; j += i)
        {
            for (auto it : r[i])
                if (it + j < n)
                    noduri[i].push_back(j + it);
        }
    }
    int N = 0;
    for (int i = 0; i < n; i++)
        N += noduri[i].size();
    vector<int> d(N);
    for (int i = 0; i < N; i++)
        d[i] = 1e9;
    d[b[1]] = 0;
    vector<bool> luat(N);
    vector<pair<int,int>> cine;///inaltimea si pozitia in cadrul noduri[h]
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < noduri[i].size(); j++)
            cine.push_back({i,j});
    }
    splv[0] = noduri[0].size();
    for (int i = 1; i < n; i++)
        splv[i] = splv[i - 1] + noduri[i].size();
    deque<int> dq;
    dq.push_back(b[1]);
    while (!dq.empty())
    {
        int nod = dq.front();
        dq.pop_front();
        if (luat[nod])
            continue;
        luat[nod] = true;
        if (nod >= n)
        {
            ///nu e primul nivel
            pair<int,int> pos = cine[nod];
            int i = noduri[pos.first][pos.second];
            if (d[i] > d[nod])
            {
                d[i] = d[nod];
                dq.push_front(i);
            }

            if (i - pos.first >= 0)
            {
                if (d[nod - r[pos.first].size()] > d[nod] + 1)
                {
                    d[nod - r[pos.first].size()] = d[nod] + 1;
                    dq.push_back(nod - r[pos.first].size());
                }
            }

            if (i + pos.first < n)
            {
                if (d[nod + r[pos.first].size()] > d[nod] + 1)
                {
                    d[nod + r[pos.first].size()] = d[nod] + 1;
                    dq.push_back(nod + r[pos.first].size());
                }
            }
        }
        else
        {
            ///primul nivel
            for (auto it : what[nod])
            {
                int vec = 0,pas = 1 << 25;
                while (pas != 0)
                {
                    if (vec + pas < N)
                    {
                        pair<int,int> xx = {cine[vec + pas].first,noduri[cine[vec + pas].first][cine[vec + pas].second]};
                        pair<int,int> yy = {it,nod};
                        if (xx <= yy)
                            vec += pas;
                    }
                    pas /= 2;
                }
                if (d[vec] > d[nod])
                {
                    d[vec] = d[nod];
                    dq.push_front(vec);
                }
            }
        }
    }
    if (d[b[2]] == (int)1e9)
        cout << -1;
    else
        cout << d[b[2]];
    return 0;
}

/**
Mama lor de limite...
Pai bun, complexitatea e O(Nsqrt), acum ca sa fie si constanta mai buna pot face gen:
- pun pe fiecare inaltime ce cladiri am nevoie
- ordinea nodurilor din graf va fi sortata dupa h, la eg dupa i
- asta imi face viata simpla pentru ca nodul din st / dr lui x = (h,i) este x +- (cate resturi modulo h apar in alea de la h)
**/

컴파일 시 표준 에러 (stderr) 메시지

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:53:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         for (int j = 0; j < noduri[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~~~~
#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...