Submission #49059

# Submission time Handle Problem Language Result Execution time Memory
49059 2018-05-21T14:47:36 Z Namnamseo Candies (JOI18_candies) C++17
100 / 100
1046 ms 84664 KB
#include <bits/stdc++.h>
using namespace std;
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define coord_comp(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define v_index(v, x) (lower_bound(all(v),x)-(v).begin())
typedef pair<int,int> pp;
typedef long long ll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T1,typename T2>
void read(pair<T1,T2>& p){ read(p.first); read(p.second); }
template<typename T,typename... Args>
void read(T&a,Args&...b){ read(a); read(b...); }

int n;

struct gugan {
	int left, right;
	int on, off;
	ll cost;

	bool operator<(const gugan& other) const {
		return left < other.left;
	}

	gugan operator+(gugan R) const {
		gugan ret;
		ret.left = left; ret.right = R.right;
		ret.on = on + R.on;
		ret.off= off+ R.off;
		ret.cost = cost + R.cost;
		return ret;
	}

	gugan inv(){
		gugan ret = *this;
		swap(ret.on, ret.off);
		ret.cost = -ret.cost;
		return ret;
	}
};

set<gugan> g;

struct costcomp {
	bool operator()(const gugan& a, const gugan& b){
		return tie(a.cost, a.left) > tie(b.cost, b.left);
	}
};

set<gugan, costcomp> CS;

int a[200010];

int main(){
	scanf("%d", &n);
	for(int i=1; i<=n; ++i){
		scanf("%d",a + i);
	}

	for(int i=1; i<=n; ++i){
		gugan tmp = {i, i+1, 0, 1, a[i]};
		g.insert(tmp);
		CS.insert(tmp);
	}

	ll ans=0;
	for(int cnt=1; cnt<=(n+1)/2; ++cnt){
		gugan a = *CS.begin(); CS.erase(CS.begin());
		gugan ng = a.inv();
		ans += a.cost;

		auto it = g.find(a);
		auto il=it, ir=it;
        bool noin = false;
		if(il != g.begin()){
			--il;
			ng = (*il) + ng;
			g.erase(*il);
			CS.erase(*il);
		} else noin = true;
		++ir;
		if(ir != g.end()){
			ng = ng + *ir;
			g.erase(*ir);
			CS.erase(*ir);
		} else noin = true;
		g.erase(it);

        if(!noin){
            g.insert(ng);
		    CS.insert(ng);
        }

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

Compilation message

candies.cpp: In function 'void read(int&)':
candies.cpp:10:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(int& x){ scanf("%d",&x); }
                    ~~~~~^~~~~~~~~
candies.cpp: In function 'void read(ll&)':
candies.cpp:11:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(ll& x){ scanf("%lld",&x); }
                   ~~~~~^~~~~~~~~~~
candies.cpp: In function 'int main()':
candies.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
candies.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",a + i);
   ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 632 KB Output is correct
2 Correct 6 ms 648 KB Output is correct
3 Correct 6 ms 832 KB Output is correct
4 Correct 5 ms 856 KB Output is correct
5 Correct 5 ms 936 KB Output is correct
6 Correct 4 ms 1140 KB Output is correct
7 Correct 5 ms 1140 KB Output is correct
8 Correct 5 ms 1140 KB Output is correct
9 Correct 4 ms 1140 KB Output is correct
10 Correct 5 ms 1176 KB Output is correct
11 Correct 5 ms 1196 KB Output is correct
12 Correct 5 ms 1216 KB Output is correct
13 Correct 5 ms 1240 KB Output is correct
14 Correct 4 ms 1256 KB Output is correct
15 Correct 4 ms 1276 KB Output is correct
16 Correct 5 ms 1296 KB Output is correct
17 Correct 7 ms 1316 KB Output is correct
18 Correct 4 ms 1316 KB Output is correct
19 Correct 6 ms 1360 KB Output is correct
20 Correct 4 ms 1476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 632 KB Output is correct
2 Correct 6 ms 648 KB Output is correct
3 Correct 6 ms 832 KB Output is correct
4 Correct 5 ms 856 KB Output is correct
5 Correct 5 ms 936 KB Output is correct
6 Correct 4 ms 1140 KB Output is correct
7 Correct 5 ms 1140 KB Output is correct
8 Correct 5 ms 1140 KB Output is correct
9 Correct 4 ms 1140 KB Output is correct
10 Correct 5 ms 1176 KB Output is correct
11 Correct 5 ms 1196 KB Output is correct
12 Correct 5 ms 1216 KB Output is correct
13 Correct 5 ms 1240 KB Output is correct
14 Correct 4 ms 1256 KB Output is correct
15 Correct 4 ms 1276 KB Output is correct
16 Correct 5 ms 1296 KB Output is correct
17 Correct 7 ms 1316 KB Output is correct
18 Correct 4 ms 1316 KB Output is correct
19 Correct 6 ms 1360 KB Output is correct
20 Correct 4 ms 1476 KB Output is correct
21 Correct 1014 ms 30432 KB Output is correct
22 Correct 823 ms 32240 KB Output is correct
23 Correct 1046 ms 34236 KB Output is correct
24 Correct 404 ms 36092 KB Output is correct
25 Correct 426 ms 37820 KB Output is correct
26 Correct 432 ms 39624 KB Output is correct
27 Correct 448 ms 41568 KB Output is correct
28 Correct 359 ms 43548 KB Output is correct
29 Correct 387 ms 45464 KB Output is correct
30 Correct 437 ms 47388 KB Output is correct
31 Correct 461 ms 49432 KB Output is correct
32 Correct 426 ms 51276 KB Output is correct
33 Correct 547 ms 53080 KB Output is correct
34 Correct 620 ms 54760 KB Output is correct
35 Correct 518 ms 56724 KB Output is correct
36 Correct 860 ms 58560 KB Output is correct
37 Correct 783 ms 60528 KB Output is correct
38 Correct 853 ms 62396 KB Output is correct
39 Correct 357 ms 64080 KB Output is correct
40 Correct 351 ms 65964 KB Output is correct
41 Correct 431 ms 67716 KB Output is correct
42 Correct 449 ms 69724 KB Output is correct
43 Correct 387 ms 71768 KB Output is correct
44 Correct 402 ms 73704 KB Output is correct
45 Correct 410 ms 75508 KB Output is correct
46 Correct 509 ms 77696 KB Output is correct
47 Correct 449 ms 79624 KB Output is correct
48 Correct 647 ms 81256 KB Output is correct
49 Correct 642 ms 82956 KB Output is correct
50 Correct 690 ms 84664 KB Output is correct