Submission #1054760

# Submission time Handle Problem Language Result Execution time Memory
1054760 2024-08-12T11:58:49 Z FaustasK Palembang Bridges (APIO15_bridge) C++14
100 / 100
104 ms 11712 KB
#include <bits/stdc++.h>

using namespace std;

struct gyventojas
{
    char p;
    double s;
    char q;
    double t;
};

const int maxn = 100000;
const long long daug = 1000000000000000;

int k, n, kiek;
gyventojas M[maxn];

vector <int> reikalingi;
vector <int> nereikalingi;
vector <int> medianai;
vector <int> unikalus;
set <int> buvo;

long long skaiciuok_keliu_suma(int tiltas1, int tiltas2)
{
    long long ats = 0;
    if(kiek>0)
    {
        if(k == 1)
        {
            for(int i = 0; i<reikalingi.size(); i++)
            {
                int j = reikalingi[i];
                ats += abs(M[j].s - tiltas1) + abs(M[j].t - tiltas1);
            }
        }
        else
        {
            for(int f = 0; f<reikalingi.size(); f++)
            {
                int x = reikalingi[f];
                int a1 = abs(M[x].s - tiltas1) + abs(M[x].t - tiltas1);
                int a2 = abs(M[x].s - tiltas2) + abs(M[x].t - tiltas2);
                ats += min(a1, a2);
            }
        }
    }
    for(int i = 0; i<nereikalingi.size(); i++)
    {
        int j = nereikalingi[i];
        ats += abs(M[j].s - M[j].t);
    }
    return ats + reikalingi.size();
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> k >> n;
    pair<double, int> st2[n];
    int j = 0;
    for(int i = 0; i<n; i++)
    {
        cin >> M[i].p >> M[i].s >> M[i].q >> M[i].t;
        if(M[i].p != M[i].q)
        {
            //cout << i << endl;
            medianai.push_back(M[i].s);
            medianai.push_back(M[i].t);
            st2[j++] = {(M[i].s+M[i].t)/2, i};
            reikalingi.push_back(i);
        }
        else nereikalingi.push_back(i);
    }
    /*for(int i = 0; i<reikalingi.size(); i++)
    {
        cout << st2[i].first << ' ' << st2[i].second << endl;
    }
    cout << st2[0].second << ' ' << st2[1].second;*/
    sort(medianai.begin(), medianai.end());
    kiek = reikalingi.size();

    if(k==1)
    {
        if(kiek > 0) cout << skaiciuok_keliu_suma(medianai[medianai.size()/2], -1);
        else cout << skaiciuok_keliu_suma(-1, -1);
    }
    else
    {
        long long galats = 0;
        if(kiek > 0)
        {
            galats = daug;
            n = reikalingi.size();
            sort(st2, st2+n);
            long long sumos_kaire[n-1];
            long long sumos_desine[n-1];
             ///i kaire puse
             priority_queue <int> q1;
             priority_queue <int> q2;
             long long s1 = 0;
             long long s2 = 0;

             int s = M[st2[0].second].s;
             int t = M[st2[0].second].t;
             //cout << '[' << st2[0].second+1 << endl;

             if(s>t) swap(s, t);
             q1.push(s);
             q2.push(-t);
             s1 += s;
             s2 += t;
             sumos_kaire[0] = -s1+s2;
             //cout << sumos_kaire[0] << ' ';
             //cout << ']' << s1 << ' ' << s2 << endl;

             for(int i = 1; i<n-1; i++)
             {
                 //cout << '[' << st2[i].second+1 << endl;

                 if(q2.top() > 0) cout << i << "#############\n";
                 s = M[st2[i].second].s;
                 t = M[st2[i].second].t;
                 if(s>t) swap(s,t);
                 if(s <= -q2.top() && t >= q1.top())
                 {
                     //cout << '!';
                     q1.push(s);
                     q2.push(-t);
                     s1 += s;
                     s2 += t;
                     //cout << s1 << ' ' << s2 << endl;
                 }
                 else if(t < q1.top())
                 {
                     //cout << '?';
                     q2.push(-q1.top());
                     s2 += q1.top();
                     s1 -= q1.top();
                     q1.pop();
                     q1.push(s);
                     q1.push(t);
                     s1+= t+s;
                 }
                 else if(s > -q2.top())
                 {
                     //cout << '/';
                     if(-q2.top() < 0) cout << "@@@@@@@@@@@@@@";
                     q1.push(-q2.top());
                     s1 += -q2.top();
                     s2 -= -q2.top();
                     q2.pop();
                     q2.push(-s);
                     q2.push(-t);
                     s2+= t+s;
                 }
                 sumos_kaire[i] = -s1+s2;
                 //if(i == n-2) cout << '{' << s1 << ' ' << s2 << endl;
                 //cout << sumos_kaire[i] << ' ';
             }
             /// desine puse
             while(!q1.empty())q1.pop();
             while(!q2.empty())q2.pop();
             s1 = 0;
             s2 = 0;

             s = M[st2[n-1].second].s;
             t = M[st2[n-1].second].t;

             //cout << "!!!!!!" << st2[n-1].first << endl;
             if(s>t) swap(s, t);
             q1.push(s);
             q2.push(-t);
             s1 += s;
             s2 += t;
             sumos_desine[n-2] = -s1+s2;
             //cout << sumos_desine[n-2] << ' ';
             //cout << ']' << s1 << ' ' << s2 << endl;

             for(int i = n-2; i>=1; i--)
             {
                 //cout << "!!!!!!" << st2[i].first << endl;
                 s = M[st2[i].second].s;
                 t = M[st2[i].second].t;
                 if(s>t) swap(s,t);
                 if(s <= -q2.top() && t >= q1.top())
                 {
                     //cout << '!';
                     q1.push(s);
                     q2.push(-t);
                     s1 += s;
                     s2 += t;
                     //cout << s1 << ' ' << s2 << endl;
                 }
                 else if(t < q1.top())
                 {
                     //cout << '?';
                     q2.push(-q1.top());
                     s2 += q1.top();
                     s1 -= q1.top();
                     q1.pop();
                     q1.push(s);
                     q1.push(t);
                     s1+= t+s;
                 }
                 else if(s > -q2.top())
                 {
                     //cout << '/';
                     q1.push(-q2.top());
                     s1 += -q2.top();
                     s2 -= -q2.top();
                     q2.pop();
                     q2.push(s);
                     q2.push(t);
                     s2+= t+s;
                 }
                 sumos_desine[i-1] = -s1+s2;
                 //cout << sumos_desine[i-1] << ' ';
             }
             for(int i = 0; i<n-1; i++)
             {
                 galats = min(galats, sumos_kaire[i] + sumos_desine[i]);
             }
         }
         for(int i = 0; i<nereikalingi.size(); i++)
         {
             int j = nereikalingi[i];
             galats += abs(M[j].s - M[j].t);
         }
         cout << galats + reikalingi.size();
    }
    return 0;
}
/*
2 4
A 1 B 6
A 5 B 3
A 8 B 7
A 10 B 10





2 2
A 10 B 5
A 1 B 1

2 3
A 10 B 5
A 6 B 8
A 1 B 1

2 4
A 10 B 5
A 6 B 8
A 3 B 4
A 1 B 1
*/

Compilation message

bridge.cpp: In function 'long long int skaiciuok_keliu_suma(int, int)':
bridge.cpp:32:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |             for(int i = 0; i<reikalingi.size(); i++)
      |                            ~^~~~~~~~~~~~~~~~~~
bridge.cpp:40:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |             for(int f = 0; f<reikalingi.size(); f++)
      |                            ~^~~~~~~~~~~~~~~~~~
bridge.cpp:49:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     for(int i = 0; i<nereikalingi.size(); i++)
      |                    ~^~~~~~~~~~~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:226:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  226 |          for(int i = 0; i<nereikalingi.size(); i++)
      |                         ~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 528 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 21 ms 6616 KB Output is correct
13 Correct 38 ms 6852 KB Output is correct
14 Correct 28 ms 6228 KB Output is correct
15 Correct 23 ms 4436 KB Output is correct
16 Correct 26 ms 6732 KB Output is correct
17 Correct 30 ms 6740 KB Output is correct
18 Correct 46 ms 6676 KB Output is correct
19 Correct 36 ms 6740 KB Output is correct
20 Correct 27 ms 6616 KB Output is correct
21 Correct 51 ms 6728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 456 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 764 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 472 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 476 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 480 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 40 ms 9884 KB Output is correct
26 Correct 61 ms 10364 KB Output is correct
27 Correct 96 ms 10876 KB Output is correct
28 Correct 104 ms 11592 KB Output is correct
29 Correct 81 ms 11696 KB Output is correct
30 Correct 48 ms 6908 KB Output is correct
31 Correct 40 ms 10828 KB Output is correct
32 Correct 62 ms 11592 KB Output is correct
33 Correct 49 ms 11244 KB Output is correct
34 Correct 69 ms 11712 KB Output is correct
35 Correct 62 ms 11068 KB Output is correct
36 Correct 67 ms 11492 KB Output is correct
37 Correct 29 ms 10064 KB Output is correct