Submission #547983

# Submission time Handle Problem Language Result Execution time Memory
547983 2022-04-12T06:52:48 Z fcmalkcin Sightseeing in Kyoto (JOI22_kyoto) C++17
100 / 100
738 ms 29028 KB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define endl "\n"
#define pb push_back
#define F(i,a,b) for(ll i=a;i<=b;i++)

const ll maxn=5e5+100;
const ll base=300;
const ll mod= 1e9+7 ;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());




struct comp
{
    bool operator() (const pair<pll,pll> &a,const pair<pll,pll> &b) const
    {
        auto p=a.ff;
        auto p1=b.ff;
        if (p.ff*p1.ss>p1.ff*p.ss)
            return true;
        if (p.ff*p1.ss<p1.ff*p.ss)
            return false;
        return (a.ss>b.ss);
    }
};
set<ll> st[2];
ll a[maxn][2];
set<pair<pll,pll>,comp> stp;
ll pos[maxn];
ll val[maxn][2];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen("t.inp","r"))
    {
        freopen("test.inp","r",stdin);
        freopen("test.out","w",stdout);
    }
    ll n, m;
    cin>> n>> m;
    ll cnt=0;
    for (int i=1; i<=n; i++)
    {
        cin>> a[i][0];
        st[0].insert(i);
        if (i>=2)
            stp.insert(make_pair(make_pair(a[i][0]-a[i-1][0],1),make_pair(i,0)));
    }
    for (int i=1; i<=m; i++)
    {
        cin>> a[i][1];
        st[1].insert(i);
        if (i>=2)
            stp.insert(make_pair(make_pair(a[i][1]-a[i-1][1],1),make_pair(i,1)));
    }
    while (stp.size())
    {
        auto it=stp.begin();
        stp.erase(it);
        auto p=((*it).ss);
        ll t=p.ss;
        cnt++;
        val[p.ff][t]=cnt;
        st[t].erase(p.ff);
        auto it1=st[t].lower_bound(p.ff);

        if (it1!=st[t].end())
        {
            ll posnxt=(*it1);
            ll posnw=p.ff;
            it1--;
            ll pospre=(*it1);
            stp.erase(make_pair(make_pair(a[posnxt][t]-a[posnw][t],posnxt-posnw),make_pair(posnxt,t)));
            stp.insert(make_pair(make_pair(a[posnxt][t]-a[pospre][t],posnxt-pospre),make_pair(posnxt,t)));
        }
    }
    ll x=n;
    ll y=m;
    ll ans=0;
    while (max(x,y)!=1)
    {
        if (x==1)
        {
            ans+=a[x][0];
            y--;
        }
        else if (y==1)
        {
            ans+=a[y][1];
            x--;
        }
        else
        {
            if (val[x][0]>val[y][1])
            {
                ans+=a[x][0];
                y--;
            }
            else
            {
                ans+=a[y][1];
                x--;
            }
        }
    }
    cout <<ans<<endl;
}

Compilation message

kyoto.cpp: In function 'int main()':
kyoto.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen("test.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
kyoto.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen("test.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 468 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 0 ms 340 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 616 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 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 2 ms 468 KB Output is correct
4 Correct 215 ms 13004 KB Output is correct
5 Correct 71 ms 12340 KB Output is correct
6 Correct 67 ms 5452 KB Output is correct
7 Correct 680 ms 29024 KB Output is correct
8 Correct 699 ms 29028 KB Output is correct
9 Correct 697 ms 28572 KB Output is correct
10 Correct 738 ms 28500 KB Output is correct
11 Correct 171 ms 28384 KB Output is correct
12 Correct 666 ms 28604 KB Output is correct
13 Correct 686 ms 28500 KB Output is correct
14 Correct 372 ms 28480 KB Output is correct
15 Correct 140 ms 28464 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 2 ms 468 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 0 ms 340 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 616 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 2 ms 468 KB Output is correct
32 Correct 215 ms 13004 KB Output is correct
33 Correct 71 ms 12340 KB Output is correct
34 Correct 67 ms 5452 KB Output is correct
35 Correct 680 ms 29024 KB Output is correct
36 Correct 699 ms 29028 KB Output is correct
37 Correct 697 ms 28572 KB Output is correct
38 Correct 738 ms 28500 KB Output is correct
39 Correct 171 ms 28384 KB Output is correct
40 Correct 666 ms 28604 KB Output is correct
41 Correct 686 ms 28500 KB Output is correct
42 Correct 372 ms 28480 KB Output is correct
43 Correct 140 ms 28464 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 0 ms 340 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 217 ms 14012 KB Output is correct
56 Correct 2 ms 468 KB Output is correct
57 Correct 17 ms 1752 KB Output is correct
58 Correct 37 ms 4292 KB Output is correct
59 Correct 693 ms 28504 KB Output is correct
60 Correct 703 ms 28500 KB Output is correct
61 Correct 709 ms 28596 KB Output is correct
62 Correct 673 ms 28500 KB Output is correct
63 Correct 150 ms 28492 KB Output is correct
64 Correct 697 ms 28480 KB Output is correct
65 Correct 696 ms 28504 KB Output is correct
66 Correct 234 ms 28504 KB Output is correct
67 Correct 151 ms 28448 KB Output is correct