답안 #270375

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
270375 2020-08-17T14:27:53 Z MKopchev Bulldozer (JOI17_bulldozer) C++14
100 / 100
1732 ms 50036 KB
#include<bits/stdc++.h>
using namespace std;

const int nmax=2e3+42;

struct info
{
    long long x,y,gain;
};

int n;

info inp[nmax];

bool cmp(pair<long long,long long> a,pair<long long,long long> b)
{
    return a.first*b.second<a.second*b.first;
}

pair<long long,long long> cur;

bool eq(info u,info v)
{
    return (u.x-v.x)*cur.second==cur.first*(u.y-v.y);
}


bool cmp_2(info u,info v)
{
    return (u.x-v.x)*cur.second<cur.first*(u.y-v.y);
}
long long solve(pair<long long,long long> a)
{
    cur=a;

    sort(inp+1,inp+n+1,cmp_2);

    long long ret=0,cur_best=0,sum_now=0;

    for(int i=1;i<=n;i++)
    {
        sum_now+=inp[i].gain;

        if(eq(inp[i],inp[i+1])==0||i==n)
        {
            cur_best=max(cur_best+sum_now,sum_now);
            sum_now=0;
        }

        ret=max(ret,cur_best);
    }
    return ret;
}

struct line
{
    int i,j;
    long long up;
    long long down;
};

vector<line> all,help;

bool eq_line(line u,line v)
{
    return u.up*v.down==u.down*v.up;
}

bool cmp_line(line a,line b)
{
    if(a.up*b.down!=a.down*b.up)return a.up*b.down<a.down*b.up;
    if(a.j!=b.j)return a.j>b.j;
    return a.i>b.i;
}

struct mem
{
    long long sum,mx_pref,mx_suff,mx_interval;
};

mem tree[4*nmax];

mem my_merge(mem a,mem b)
{
    mem ret;
    ret.sum=a.sum+b.sum;
    ret.mx_pref=max(a.mx_pref,a.sum+b.mx_pref);
    ret.mx_suff=max(b.mx_suff,b.sum+a.mx_suff);

    ret.mx_interval=max(a.mx_interval,b.mx_interval);
    ret.mx_interval=max(ret.mx_interval,a.mx_suff+b.mx_pref);
    return ret;
}
void update(int node,int l,int r,int pos,long long add)
{
    if(l==r)
    {
        tree[node].sum+=add;
        tree[node].mx_pref=max(tree[node].sum,0LL);
        tree[node].mx_suff=max(tree[node].sum,0LL);
        tree[node].mx_interval=max(tree[node].sum,0LL);
        return;
    }

    int av=(l+r)/2;

    if(pos<=av)update(node*2,l,av,pos,add);
    else update(node*2+1,av+1,r,pos,add);

    tree[node]=my_merge(tree[node*2],tree[node*2+1]);
}

int where[nmax];

int main()
{
    scanf("%i",&n);
    for(int i=1;i<=n;i++)
        scanf("%lld%lld%lld",&inp[i].x,&inp[i].y,&inp[i].gain);

    if(n==1)
    {
        printf("%lld\n",max(0LL,inp[1].gain));
        return 0;
    }

    pair<long long,long long> start={0,1};

    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
            long long up=inp[i].x-inp[j].x;
            long long down=inp[i].y-inp[j].y;

            if(down)
            {
                if(down<0)down=-down,up=-up;

                if(up*start.second<down*start.first)start={up,down};
            }
        }
    start.first--;

    long long outp=0;

    outp=max(outp,solve(start));

    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
            line cur;
            cur.i=i;
            cur.j=j;

            long long up=inp[i].x-inp[j].x;
            long long down=inp[i].y-inp[j].y;

            if(down)
            {
                if(down<0)down=-down,up=-up;

                cur.up=up;
                cur.down=down;

                all.push_back(cur);
            }
        }

    if(all.size())sort(all.begin(),all.end(),cmp_line);

    for(int i=1;i<=n;i++)
    {
        where[i]=i;
        update(1,1,n,i,inp[i].gain);
    }

    for(int i=0;i<all.size();i++)
    {
        int j=i;

        for(j=i;j<all.size()&&eq_line(all[i],all[j]);j++)
        {
        int le=where[all[j].i];
        int ri=where[all[j].j];
        /*
        cout<<all[j].i<<" "<<all[j].j<<" -> "<<le<<" "<<ri<<" "<<all[j].up<<" "<<all[j].down<<endl;
        for(int t=1;t<=n;t++)cout<<where[t]<<" ";cout<<endl;
        */

        long long diff_le=-inp[le].gain;
        long long diff_ri=-inp[ri].gain;

        swap(where[all[j].i],where[all[j].j]);
        swap(inp[le],inp[ri]);

        diff_le+=inp[le].gain;
        diff_ri+=inp[ri].gain;

        update(1,1,n,le,diff_le);
        update(1,1,n,ri,diff_ri);
        }

        i=j-1;

        outp=max(outp,tree[1].mx_interval);
    }

    printf("%lld\n",outp);
    return 0;
}

Compilation message

bulldozer.cpp: In function 'int main()':
bulldozer.cpp:177:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  177 |     for(int i=0;i<all.size();i++)
      |                 ~^~~~~~~~~~~
bulldozer.cpp:181:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  181 |         for(j=i;j<all.size()&&eq_line(all[i],all[j]);j++)
      |                 ~^~~~~~~~~~~
bulldozer.cpp:117:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  117 |     scanf("%i",&n);
      |     ~~~~~^~~~~~~~~
bulldozer.cpp:119:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  119 |         scanf("%lld%lld%lld",&inp[i].x,&inp[i].y,&inp[i].gain);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 4 ms 672 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 4 ms 640 KB Output is correct
6 Correct 4 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 256 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 3 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 3 ms 640 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 4 ms 672 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 4 ms 640 KB Output is correct
6 Correct 4 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 256 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 3 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 3 ms 640 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
33 Correct 1630 ms 49736 KB Output is correct
34 Correct 1663 ms 49892 KB Output is correct
35 Correct 1651 ms 49836 KB Output is correct
36 Correct 1658 ms 49836 KB Output is correct
37 Correct 1628 ms 49836 KB Output is correct
38 Correct 1608 ms 49872 KB Output is correct
39 Correct 1667 ms 49936 KB Output is correct
40 Correct 1672 ms 49836 KB Output is correct
41 Correct 1703 ms 49836 KB Output is correct
42 Correct 1732 ms 49852 KB Output is correct
43 Correct 1676 ms 49780 KB Output is correct
44 Correct 1679 ms 49860 KB Output is correct
45 Correct 1652 ms 49836 KB Output is correct
46 Correct 1622 ms 49860 KB Output is correct
47 Correct 1642 ms 49836 KB Output is correct
48 Correct 1636 ms 49860 KB Output is correct
49 Correct 1669 ms 49836 KB Output is correct
50 Correct 1637 ms 49836 KB Output is correct
51 Correct 1649 ms 49840 KB Output is correct
52 Correct 1618 ms 49888 KB Output is correct
53 Correct 1674 ms 49832 KB Output is correct
54 Correct 1632 ms 49860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 4 ms 672 KB Output is correct
3 Correct 3 ms 640 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 4 ms 640 KB Output is correct
6 Correct 4 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 256 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 3 ms 640 KB Output is correct
27 Correct 4 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 4 ms 640 KB Output is correct
30 Correct 3 ms 640 KB Output is correct
31 Correct 3 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
33 Correct 1630 ms 49736 KB Output is correct
34 Correct 1663 ms 49892 KB Output is correct
35 Correct 1651 ms 49836 KB Output is correct
36 Correct 1658 ms 49836 KB Output is correct
37 Correct 1628 ms 49836 KB Output is correct
38 Correct 1608 ms 49872 KB Output is correct
39 Correct 1667 ms 49936 KB Output is correct
40 Correct 1672 ms 49836 KB Output is correct
41 Correct 1703 ms 49836 KB Output is correct
42 Correct 1732 ms 49852 KB Output is correct
43 Correct 1676 ms 49780 KB Output is correct
44 Correct 1679 ms 49860 KB Output is correct
45 Correct 1652 ms 49836 KB Output is correct
46 Correct 1622 ms 49860 KB Output is correct
47 Correct 1642 ms 49836 KB Output is correct
48 Correct 1636 ms 49860 KB Output is correct
49 Correct 1669 ms 49836 KB Output is correct
50 Correct 1637 ms 49836 KB Output is correct
51 Correct 1649 ms 49840 KB Output is correct
52 Correct 1618 ms 49888 KB Output is correct
53 Correct 1674 ms 49832 KB Output is correct
54 Correct 1632 ms 49860 KB Output is correct
55 Correct 1637 ms 49836 KB Output is correct
56 Correct 1658 ms 49860 KB Output is correct
57 Correct 1633 ms 49832 KB Output is correct
58 Correct 1660 ms 49836 KB Output is correct
59 Correct 1626 ms 49836 KB Output is correct
60 Correct 1630 ms 50004 KB Output is correct
61 Correct 1626 ms 49836 KB Output is correct
62 Correct 1693 ms 49840 KB Output is correct
63 Correct 1637 ms 50036 KB Output is correct
64 Correct 1618 ms 49860 KB Output is correct
65 Correct 1654 ms 49836 KB Output is correct
66 Correct 1654 ms 49836 KB Output is correct
67 Correct 1636 ms 49860 KB Output is correct
68 Correct 1659 ms 49860 KB Output is correct
69 Correct 1614 ms 49860 KB Output is correct
70 Correct 1653 ms 49836 KB Output is correct
71 Correct 1644 ms 49816 KB Output is correct
72 Correct 1632 ms 49860 KB Output is correct
73 Correct 1643 ms 49860 KB Output is correct
74 Correct 1663 ms 49836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 4 ms 672 KB Output is correct
18 Correct 3 ms 640 KB Output is correct
19 Correct 3 ms 640 KB Output is correct
20 Correct 4 ms 640 KB Output is correct
21 Correct 4 ms 640 KB Output is correct
22 Correct 3 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 0 ms 256 KB Output is correct
27 Correct 0 ms 256 KB Output is correct
28 Correct 0 ms 384 KB Output is correct
29 Correct 0 ms 384 KB Output is correct
30 Correct 0 ms 256 KB Output is correct
31 Correct 0 ms 256 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 0 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 0 ms 256 KB Output is correct
36 Correct 3 ms 640 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 3 ms 640 KB Output is correct
39 Correct 3 ms 640 KB Output is correct
40 Correct 3 ms 640 KB Output is correct
41 Correct 3 ms 640 KB Output is correct
42 Correct 4 ms 640 KB Output is correct
43 Correct 3 ms 640 KB Output is correct
44 Correct 4 ms 640 KB Output is correct
45 Correct 3 ms 640 KB Output is correct
46 Correct 3 ms 640 KB Output is correct
47 Correct 3 ms 640 KB Output is correct
48 Correct 1630 ms 49736 KB Output is correct
49 Correct 1663 ms 49892 KB Output is correct
50 Correct 1651 ms 49836 KB Output is correct
51 Correct 1658 ms 49836 KB Output is correct
52 Correct 1628 ms 49836 KB Output is correct
53 Correct 1608 ms 49872 KB Output is correct
54 Correct 1667 ms 49936 KB Output is correct
55 Correct 1672 ms 49836 KB Output is correct
56 Correct 1703 ms 49836 KB Output is correct
57 Correct 1732 ms 49852 KB Output is correct
58 Correct 1676 ms 49780 KB Output is correct
59 Correct 1679 ms 49860 KB Output is correct
60 Correct 1652 ms 49836 KB Output is correct
61 Correct 1622 ms 49860 KB Output is correct
62 Correct 1642 ms 49836 KB Output is correct
63 Correct 1636 ms 49860 KB Output is correct
64 Correct 1669 ms 49836 KB Output is correct
65 Correct 1637 ms 49836 KB Output is correct
66 Correct 1649 ms 49840 KB Output is correct
67 Correct 1618 ms 49888 KB Output is correct
68 Correct 1674 ms 49832 KB Output is correct
69 Correct 1632 ms 49860 KB Output is correct
70 Correct 1637 ms 49836 KB Output is correct
71 Correct 1658 ms 49860 KB Output is correct
72 Correct 1633 ms 49832 KB Output is correct
73 Correct 1660 ms 49836 KB Output is correct
74 Correct 1626 ms 49836 KB Output is correct
75 Correct 1630 ms 50004 KB Output is correct
76 Correct 1626 ms 49836 KB Output is correct
77 Correct 1693 ms 49840 KB Output is correct
78 Correct 1637 ms 50036 KB Output is correct
79 Correct 1618 ms 49860 KB Output is correct
80 Correct 1654 ms 49836 KB Output is correct
81 Correct 1654 ms 49836 KB Output is correct
82 Correct 1636 ms 49860 KB Output is correct
83 Correct 1659 ms 49860 KB Output is correct
84 Correct 1614 ms 49860 KB Output is correct
85 Correct 1653 ms 49836 KB Output is correct
86 Correct 1644 ms 49816 KB Output is correct
87 Correct 1632 ms 49860 KB Output is correct
88 Correct 1643 ms 49860 KB Output is correct
89 Correct 1663 ms 49836 KB Output is correct
90 Correct 1659 ms 49836 KB Output is correct
91 Correct 1662 ms 49892 KB Output is correct
92 Correct 1616 ms 49836 KB Output is correct
93 Correct 1663 ms 49876 KB Output is correct
94 Correct 1683 ms 49836 KB Output is correct
95 Correct 1641 ms 49832 KB Output is correct
96 Correct 1611 ms 49840 KB Output is correct
97 Correct 1614 ms 49836 KB Output is correct
98 Correct 1674 ms 49836 KB Output is correct
99 Correct 1648 ms 49860 KB Output is correct
100 Correct 1564 ms 49836 KB Output is correct
101 Correct 1591 ms 49836 KB Output is correct
102 Correct 1646 ms 49836 KB Output is correct
103 Correct 1584 ms 49832 KB Output is correct
104 Correct 1556 ms 49832 KB Output is correct
105 Correct 1608 ms 49860 KB Output is correct
106 Correct 1577 ms 49840 KB Output is correct
107 Correct 1665 ms 49860 KB Output is correct
108 Correct 1603 ms 49832 KB Output is correct
109 Correct 1605 ms 49836 KB Output is correct
110 Correct 1528 ms 49836 KB Output is correct
111 Correct 1542 ms 49836 KB Output is correct
112 Correct 1567 ms 49840 KB Output is correct
113 Correct 1576 ms 49860 KB Output is correct
114 Correct 1578 ms 49876 KB Output is correct
115 Correct 1562 ms 49836 KB Output is correct
116 Correct 1568 ms 49860 KB Output is correct
117 Correct 1562 ms 49816 KB Output is correct
118 Correct 1545 ms 49836 KB Output is correct
119 Correct 1557 ms 49860 KB Output is correct
120 Correct 1 ms 384 KB Output is correct
121 Correct 0 ms 384 KB Output is correct
122 Correct 1658 ms 49860 KB Output is correct
123 Correct 1690 ms 49836 KB Output is correct
124 Correct 1632 ms 49864 KB Output is correct
125 Correct 1622 ms 49864 KB Output is correct
126 Correct 1658 ms 49860 KB Output is correct
127 Correct 1671 ms 49860 KB Output is correct
128 Correct 1639 ms 49836 KB Output is correct
129 Correct 1655 ms 49836 KB Output is correct
130 Correct 1665 ms 49860 KB Output is correct
131 Correct 1730 ms 49860 KB Output is correct
132 Correct 1678 ms 49836 KB Output is correct
133 Correct 1687 ms 49864 KB Output is correct