Submission #425570

# Submission time Handle Problem Language Result Execution time Memory
425570 2021-06-13T07:40:30 Z Charis02 Ancient Books (IOI17_books) C++14
50 / 100
269 ms 60632 KB
#include "books.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define ll long long
#define pi pair < ll,ll >
#define rep(i,a,b) for(int i = a;i < b;i++)
#define mp(a,b) make_pair(a,b)
#define N 1000003
#define INF 1e9+7

using namespace std;

vector < pi > graph[N];
int id[N];
bool vis[N];

ll abso(ll x)
{
    return (x < 0) ? -x : x;
}

ll dijkstra_weight(int n,int source)
{
    ll d[n];
    ll w[n];

    rep(i,0,n)
        d[i] = INF;

    d[source] = 0;
    w[source]=0;
    priority_queue < pi > pq;
    pq.push(mp(0,source));

    while(!pq.empty())
    {
        pi cur = pq.top();
        pq.pop();

        cur.first*=-1;

        if(cur.first > d[cur.second])
            continue;

        rep(i,0,graph[cur.second].size())
        {
            int v = graph[cur.second][i].first;
            ll wv = graph[cur.second][i].second;

          //  cout <<"in " << v << endl;

            if(d[v] > cur.first + wv)
            {
                d[v]=cur.first+wv;
                w[v]=wv;
                pq.push(mp(-d[v],v));
            }
        }
    }

    ll res = 0;

    rep(i,0,n)
    {
        res += w[i];
    }

    return 2*res;
}

int dsu[N];
vector < pair < ll,pi > > edges;
int f(int x)
{
    if(dsu[x]==x)
        return x;
    return dsu[x]=f(dsu[x]);
}
bool issameset(int x,int y)
{
    return f(x)==f(y);
}

void join(int x,int y)
{
    dsu[f(x)]=f(y);
    return;
}

ll mst_weight(int n,int source)
{
    rep(i,0,n)
        dsu[i]=i;
    ll res = 0;
    sort(edges.begin(),edges.end());

    rep(i,0,edges.size())
    {
        int u = edges[i].second.first;
        int v = edges[i].second.second;
        ll w = edges[i].first;

        if(!issameset(u,v))
        {
            join(u,v);
            res+=2*w;
        }
    }

    return res;
}

ll get_cycle_cost(vector < int > c)
{
    if(c.size()==1)
        return 0;

    ll res = 0;

    rep(i,1,c.size())
    {
        res += abso(c[i]-c[i-1]);
    }

    res += abso(c[0]-c[c.size()-1]);

    return res;
}

ll get_distance(pi a,pi b)
{
    if(a.first>a.second)
        swap(a.first,a.second);
    if(b.first>b.second)
        swap(b.first,b.second);
    return max((ll)0,max(a.first,b.first)-min(a.second,b.second));
}

ll calculate_weights(const vector < int >&p,const vector < vector < int > >&cycles)
{
    edges.clear();
    vector < pi > ranges;

    rep(i,0,cycles.size())
    {
        ranges.push_back(mp(cycles[i][0],cycles[i][0]));

        rep(j,0,cycles[i].size())
        {
            ll v = cycles[i][j];
            ll pv = p[v];
            ranges[i].first = min(ranges[i].first,v);
            ranges[i].first = min(ranges[i].first,pv);
            ranges[i].second = max(ranges[i].second,v);
            ranges[i].second = max(ranges[i].second,pv);
        }
    }

    sort(ranges.begin(),ranges.end());
    vector < pi > nranges;

    ll curlow = ranges[0].first;
    ll curhigh = ranges[0].second;

    rep(i,1,ranges.size())
    {
        if(ranges[i].first > curhigh)
        {
            nranges.push_back(mp(curlow,curhigh));
            curlow = ranges[i].first;
            curhigh = ranges[i].second;
        }
        else
            curhigh = max(ranges[i].second,curhigh);
    }

    nranges.push_back(mp(curlow,curhigh));

    ll res = 0;

    rep(i,1,nranges.size())
    {
        res += nranges[i].first-nranges[i-1].second;
    }

    return res*2;
}

long long minimum_walk(std::vector<int> p, int s)
{
    int n = p.size();
    vector < vector < int > > cycles;
    ll ans = 0;

    rep(i,0,n)
    {
        if(p[i] == i || vis[i])
            continue;

        int cur = i;
        int ind = cycles.size();
        cycles.push_back(vector < int > ());

        while(!vis[cur])
        {
            id[cur] = ind;
            vis[cur] = true;
            cycles[ind].push_back(cur);
            cur = p[cur];
        }

        ans += get_cycle_cost(cycles[ind]);
    }

    if(!vis[s])
    {
        vis[s]=true;
        int ind = cycles.size();
        cycles.push_back(vector < int > ());
        cycles[ind].push_back(s);
        id[s] = ind;
    }

    ans += calculate_weights(p,cycles);

	return ans;
}

Compilation message

books.cpp: In function 'long long int dijkstra_weight(int, int)':
books.cpp:8:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rep(i,a,b) for(int i = a;i < b;i++)
......
   47 |         rep(i,0,graph[cur.second].size())
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
books.cpp:47:9: note: in expansion of macro 'rep'
   47 |         rep(i,0,graph[cur.second].size())
      |         ^~~
books.cpp: In function 'long long int mst_weight(int, int)':
books.cpp:8:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rep(i,a,b) for(int i = a;i < b;i++)
......
   99 |     rep(i,0,edges.size())
      |         ~~~~~~~~~~~~~~~~            
books.cpp:99:5: note: in expansion of macro 'rep'
   99 |     rep(i,0,edges.size())
      |     ^~~
books.cpp: In function 'long long int get_cycle_cost(std::vector<int>)':
books.cpp:8:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rep(i,a,b) for(int i = a;i < b;i++)
......
  122 |     rep(i,1,c.size())
      |         ~~~~~~~~~~~~                
books.cpp:122:5: note: in expansion of macro 'rep'
  122 |     rep(i,1,c.size())
      |     ^~~
books.cpp: In function 'long long int calculate_weights(const std::vector<int>&, const std::vector<std::vector<int> >&)':
books.cpp:8:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rep(i,a,b) for(int i = a;i < b;i++)
......
  146 |     rep(i,0,cycles.size())
      |         ~~~~~~~~~~~~~~~~~           
books.cpp:146:5: note: in expansion of macro 'rep'
  146 |     rep(i,0,cycles.size())
      |     ^~~
books.cpp:8:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rep(i,a,b) for(int i = a;i < b;i++)
......
  150 |         rep(j,0,cycles[i].size())
      |             ~~~~~~~~~~~~~~~~~~~~    
books.cpp:150:9: note: in expansion of macro 'rep'
  150 |         rep(j,0,cycles[i].size())
      |         ^~~
books.cpp:8:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rep(i,a,b) for(int i = a;i < b;i++)
......
  167 |     rep(i,1,ranges.size())
      |         ~~~~~~~~~~~~~~~~~           
books.cpp:167:5: note: in expansion of macro 'rep'
  167 |     rep(i,1,ranges.size())
      |     ^~~
books.cpp:8:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define rep(i,a,b) for(int i = a;i < b;i++)
......
  183 |     rep(i,1,nranges.size())
      |         ~~~~~~~~~~~~~~~~~~          
books.cpp:183:5: note: in expansion of macro 'rep'
  183 |     rep(i,1,nranges.size())
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23704 KB Output is correct
2 Correct 19 ms 23680 KB Output is correct
3 Correct 18 ms 23884 KB Output is correct
4 Correct 18 ms 23680 KB Output is correct
5 Correct 16 ms 23728 KB Output is correct
6 Correct 20 ms 23792 KB Output is correct
7 Correct 18 ms 23756 KB Output is correct
8 Correct 18 ms 23872 KB Output is correct
9 Correct 16 ms 23756 KB Output is correct
10 Correct 17 ms 23776 KB Output is correct
11 Correct 18 ms 23788 KB Output is correct
12 Correct 16 ms 23756 KB Output is correct
13 Correct 18 ms 23708 KB Output is correct
14 Correct 18 ms 23692 KB Output is correct
15 Correct 18 ms 23748 KB Output is correct
16 Correct 19 ms 23788 KB Output is correct
17 Correct 17 ms 23772 KB Output is correct
18 Correct 19 ms 23740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23704 KB Output is correct
2 Correct 19 ms 23680 KB Output is correct
3 Correct 18 ms 23884 KB Output is correct
4 Correct 18 ms 23680 KB Output is correct
5 Correct 16 ms 23728 KB Output is correct
6 Correct 20 ms 23792 KB Output is correct
7 Correct 18 ms 23756 KB Output is correct
8 Correct 18 ms 23872 KB Output is correct
9 Correct 16 ms 23756 KB Output is correct
10 Correct 17 ms 23776 KB Output is correct
11 Correct 18 ms 23788 KB Output is correct
12 Correct 16 ms 23756 KB Output is correct
13 Correct 18 ms 23708 KB Output is correct
14 Correct 18 ms 23692 KB Output is correct
15 Correct 18 ms 23748 KB Output is correct
16 Correct 19 ms 23788 KB Output is correct
17 Correct 17 ms 23772 KB Output is correct
18 Correct 19 ms 23740 KB Output is correct
19 Correct 14 ms 23800 KB Output is correct
20 Correct 15 ms 23712 KB Output is correct
21 Correct 19 ms 23756 KB Output is correct
22 Correct 16 ms 23808 KB Output is correct
23 Correct 19 ms 23756 KB Output is correct
24 Correct 15 ms 23756 KB Output is correct
25 Correct 15 ms 23756 KB Output is correct
26 Correct 15 ms 23756 KB Output is correct
27 Correct 20 ms 23756 KB Output is correct
28 Correct 15 ms 23832 KB Output is correct
29 Correct 18 ms 23836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23704 KB Output is correct
2 Correct 19 ms 23680 KB Output is correct
3 Correct 18 ms 23884 KB Output is correct
4 Correct 18 ms 23680 KB Output is correct
5 Correct 16 ms 23728 KB Output is correct
6 Correct 20 ms 23792 KB Output is correct
7 Correct 18 ms 23756 KB Output is correct
8 Correct 18 ms 23872 KB Output is correct
9 Correct 16 ms 23756 KB Output is correct
10 Correct 17 ms 23776 KB Output is correct
11 Correct 18 ms 23788 KB Output is correct
12 Correct 16 ms 23756 KB Output is correct
13 Correct 18 ms 23708 KB Output is correct
14 Correct 18 ms 23692 KB Output is correct
15 Correct 18 ms 23748 KB Output is correct
16 Correct 19 ms 23788 KB Output is correct
17 Correct 17 ms 23772 KB Output is correct
18 Correct 19 ms 23740 KB Output is correct
19 Correct 14 ms 23800 KB Output is correct
20 Correct 15 ms 23712 KB Output is correct
21 Correct 19 ms 23756 KB Output is correct
22 Correct 16 ms 23808 KB Output is correct
23 Correct 19 ms 23756 KB Output is correct
24 Correct 15 ms 23756 KB Output is correct
25 Correct 15 ms 23756 KB Output is correct
26 Correct 15 ms 23756 KB Output is correct
27 Correct 20 ms 23756 KB Output is correct
28 Correct 15 ms 23832 KB Output is correct
29 Correct 18 ms 23836 KB Output is correct
30 Correct 249 ms 42728 KB Output is correct
31 Correct 232 ms 43200 KB Output is correct
32 Correct 136 ms 33048 KB Output is correct
33 Correct 261 ms 58600 KB Output is correct
34 Correct 213 ms 58600 KB Output is correct
35 Correct 229 ms 60632 KB Output is correct
36 Correct 195 ms 55476 KB Output is correct
37 Correct 162 ms 45560 KB Output is correct
38 Correct 168 ms 44136 KB Output is correct
39 Correct 169 ms 43908 KB Output is correct
40 Correct 180 ms 42904 KB Output is correct
41 Correct 195 ms 42812 KB Output is correct
42 Correct 170 ms 42668 KB Output is correct
43 Correct 269 ms 59680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 23756 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23704 KB Output is correct
2 Correct 19 ms 23680 KB Output is correct
3 Correct 18 ms 23884 KB Output is correct
4 Correct 18 ms 23680 KB Output is correct
5 Correct 16 ms 23728 KB Output is correct
6 Correct 20 ms 23792 KB Output is correct
7 Correct 18 ms 23756 KB Output is correct
8 Correct 18 ms 23872 KB Output is correct
9 Correct 16 ms 23756 KB Output is correct
10 Correct 17 ms 23776 KB Output is correct
11 Correct 18 ms 23788 KB Output is correct
12 Correct 16 ms 23756 KB Output is correct
13 Correct 18 ms 23708 KB Output is correct
14 Correct 18 ms 23692 KB Output is correct
15 Correct 18 ms 23748 KB Output is correct
16 Correct 19 ms 23788 KB Output is correct
17 Correct 17 ms 23772 KB Output is correct
18 Correct 19 ms 23740 KB Output is correct
19 Correct 14 ms 23800 KB Output is correct
20 Correct 15 ms 23712 KB Output is correct
21 Correct 19 ms 23756 KB Output is correct
22 Correct 16 ms 23808 KB Output is correct
23 Correct 19 ms 23756 KB Output is correct
24 Correct 15 ms 23756 KB Output is correct
25 Correct 15 ms 23756 KB Output is correct
26 Correct 15 ms 23756 KB Output is correct
27 Correct 20 ms 23756 KB Output is correct
28 Correct 15 ms 23832 KB Output is correct
29 Correct 18 ms 23836 KB Output is correct
30 Correct 249 ms 42728 KB Output is correct
31 Correct 232 ms 43200 KB Output is correct
32 Correct 136 ms 33048 KB Output is correct
33 Correct 261 ms 58600 KB Output is correct
34 Correct 213 ms 58600 KB Output is correct
35 Correct 229 ms 60632 KB Output is correct
36 Correct 195 ms 55476 KB Output is correct
37 Correct 162 ms 45560 KB Output is correct
38 Correct 168 ms 44136 KB Output is correct
39 Correct 169 ms 43908 KB Output is correct
40 Correct 180 ms 42904 KB Output is correct
41 Correct 195 ms 42812 KB Output is correct
42 Correct 170 ms 42668 KB Output is correct
43 Correct 269 ms 59680 KB Output is correct
44 Incorrect 19 ms 23756 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -