Submission #660905

# Submission time Handle Problem Language Result Execution time Memory
660905 2022-11-23T14:06:46 Z danikoynov Palembang Bridges (APIO15_bridge) C++14
22 / 100
101 ms 5312 KB
#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 1e5 + 10, smalln = 1010;

struct work
{
    ll x, y;
} w[maxn];

int k, n;
ll pref[smalln][smalln][2];

bool cmp(work w1, work w2)
{
    return w1.x + w1.y < w2.x + w2.y;
}

void solve()
{
    cin >> k >> n;
    ll ans = 0;
    vector < ll > v, sx;
    for (int i = 1; i <= n; i ++)
    {
        char c1, c2;
        cin >> c1 >> w[i].x >> c2 >> w[i].y;
        if (w[i].x > w[i].y)
            swap(w[i].x, w[i].y);
        if (c1 == c2)
        {
            ans = ans + w[i].y - w[i].x;
            i --;
            n --;
        }
        else
        {
            v.push_back(w[i].y);
            v.push_back(w[i].x);
            sx.push_back(w[i].x + w[i].y);
            ans ++;
        }
    }

    //for (int i = 1; i <= n; i ++)
      //  cout << w[i].x << " - " << w[i].y << endl;
    if (k == 1)
    {

        sort(v.begin(), v.end());
        for (int i = 0; i < v.size() / 2; i ++)
            ans = ans - (v[i]);
        for (int i = v.size() / 2; i < v.size(); i ++)
            ans = ans + v[i];

        cout << ans << endl;

    }
    else
    {
        ll best = 1e18;
        if (v.empty())
            best = 0;
        sort(w + 1, w + n + 1, cmp);
        sort(v.begin(), v.end());
        for (int i = 0; i < v.size(); i ++)
        {
            for (int j = 1; j <= n; j ++)
            {
                pref[i][j][0] = pref[i][j - 1][0] + abs(v[i] - w[j].x) + abs(v[i] - w[j].y);
            }
            for (int j = n; j > 0; j --)
            {
                pref[i][j][1] = pref[i][j + 1][1] + abs(v[i] - w[j].x) + abs(v[i] - w[j].y);
            }
        }

    for (int i = 1; i <= n; i ++)
    {
        ///cout << "pair " << w[i].x << " " << w[i].y << endl;
    }
        for (int i = 0; i < v.size(); i ++)
        {
            int pt = 1;
            for (int j = i + 1; j < v.size(); j ++)
            {
                while(pt <= n && w[pt].x + w[pt].y <= v[j] + v[i])
                    pt ++;

                ll calc = 0;
                for (int k = 1; k <= pt; k ++)
                {
                    ll s1 = abs(v[i] - w[k].x) + abs(v[i] - w[k].y);
                    ll s2 = abs(v[j] - w[k].x) + abs(v[j] - w[k].y);

                    calc = calc + min(s1, s2);
                }
                /**for (int k = pt + 1; k <= n; k ++)
                {
                    calc = calc + abs(v[j] - w[k].x) + abs(v[j] - w[k].y);
                }*/
                best = min(best, calc);
                ///cout << v[i] << " : " << v[j] << " : " << pt << " " << pref[i][pt][0] << " " << pref[j][pt][1] << endl;
                ///best = min(best, pref[i][pt - 1][0] + pref[j][pt][1]);
            }
        }
        cout << ans + best << endl;
    }


}

int main()
{
    solve();
    return 0;
}
/**
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/

Compilation message

bridge.cpp: In function 'void solve()':
bridge.cpp:61:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |         for (int i = 0; i < v.size() / 2; i ++)
      |                         ~~^~~~~~~~~~~~~~
bridge.cpp:63:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for (int i = v.size() / 2; i < v.size(); i ++)
      |                                    ~~^~~~~~~~~~
bridge.cpp:76:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |         for (int i = 0; i < v.size(); i ++)
      |                         ~~^~~~~~~~~~
bridge.cpp:92:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |         for (int i = 0; i < v.size(); i ++)
      |                         ~~^~~~~~~~~~
bridge.cpp:95:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |             for (int j = i + 1; j < v.size(); j ++)
      |                                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 51 ms 5132 KB Output is correct
13 Correct 101 ms 5208 KB Output is correct
14 Correct 67 ms 4748 KB Output is correct
15 Correct 63 ms 3164 KB Output is correct
16 Correct 75 ms 5200 KB Output is correct
17 Correct 95 ms 5224 KB Output is correct
18 Correct 86 ms 5136 KB Output is correct
19 Correct 101 ms 5176 KB Output is correct
20 Correct 83 ms 5312 KB Output is correct
21 Correct 94 ms 5232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 4 ms 1236 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 4 ms 1236 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 4 ms 1236 KB Output isn't correct
4 Halted 0 ms 0 KB -