Submission #317351

# Submission time Handle Problem Language Result Execution time Memory
317351 2020-10-29T15:13:29 Z CaroLinda Bulldozer (JOI17_bulldozer) C++14
75 / 100
1085 ms 17200 KB
#include <bits/stdc++.h>
 
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
#define ll long long
 
const int MAXN = 2010 ;
 
using namespace std ;
 
struct Seg
{
	
	long long soma[MAXN*4] ;
	long long bestLeft[MAXN*4] ;
	long long bestRight[MAXN*4] ;
	long long bestSum[MAXN*4] ;
 
	int m(int l, int r ) { return (l+r)>>1 ; }
 
	void print(int pos, int l, int r ) 
	{

		if(l == r ) return (void)(printf("%lld ", soma[pos] ) ) ;
 
		print(pos<<1 , l, m(l,r) ) ;
		print(pos<<1|1 , m(l,r)+1, r ) ;
 
	}
 
 
	void upd(int pos, int l, int r, int k, long long val ) //vamos de seg de setar?
	{
		if( l == r ) 
		{
			soma[pos] = val ;
			bestLeft[pos] = bestRight[pos] = bestSum[pos] = max(val, 0LL ) ;
			return ;
		}
 
		if( k <= m(l,r) ) upd(pos<<1 , l , m(l,r) , k, val ) ;
		else upd(pos<<1|1 , m(l,r)+1, r, k, val ) ;
 
		soma[pos] = soma[pos<<1] + soma[pos<<1|1] ;
		bestLeft[pos] = max(bestLeft[pos<<1|1] , soma[pos<<1|1] + bestLeft[pos<<1] ) ;
		bestRight[pos] = max(bestRight[pos<<1], soma[pos<<1] + bestRight[pos<<1|1] ) ;
		bestSum[pos] = bestLeft[pos<<1] + bestRight[pos<<1|1] ;
 
		bestSum[pos] = max(bestSum[pos<<1] ,max(bestSum[pos] , bestSum[pos<<1|1] ) ) ;
 
	}
 
	long long getAns() { return bestSum[1] ; }
 
} seg ;
 
int n ;
int pos[MAXN] ;
long long x[MAXN], y[MAXN] ;
long long w[MAXN] ;
 
int getFakeQuadrant(int i, int j )
{
	
	if( x[i] > x[j] ) return 0 ;
	if(x[i] == x[j] ) return 1 ;
	return 2 ;
 
}
 
struct Event
{
 
	int i , j ;
 
	Event(int i = 0 , int j = 0 ) : i(i) , j(j) {}
	
	bool operator < (Event other) const 
	{
		
		/* Tenho certeza que:
			i) y[i] > y[j]
			ii) y[i] == y[j] && x[i] > x[j]
 
			O numerador sempre vai ser positivo ou zero
		*/
 
		pair<long long,long long> mySlope = make_pair(y[i]-y[j], x[i]-x[j] ) ;
		pair<long long,long long> otherSlope = make_pair(y[other.i]-y[other.j], x[other.i]-x[other.j] ) ;
 
		int myFakeQuadrant =  getFakeQuadrant(i,j) ;
		int otherFakeQuadrant = getFakeQuadrant(other.i , other.j ) ;
 
		if(myFakeQuadrant != otherFakeQuadrant ) 
			return myFakeQuadrant < otherFakeQuadrant ;
 
		//Remember the denominators can be negative
		//And, therefore, the comparison will be weird	
 
		if( mySlope.first * otherSlope.second == mySlope.second * otherSlope.first )
		{
			//This will work even if there isn't a slope at all
			return make_pair(y[i], y[j] ) > make_pair(y[other.i],  y[other.j] ) ;
		}
 
		if(mySlope.second < 0 ) 
		{
			mySlope.first = -mySlope.first ;
			mySlope.second = -mySlope.second ;
		}
		if(otherSlope.second < 0 )
		{
			otherSlope.first = -otherSlope.first ;
			otherSlope.second = -otherSlope.second ;
		}
 
		return mySlope.first * otherSlope.second < mySlope.second * otherSlope.first ;
		
	}
} ;
 
bool isEqual(Event a, Event b )
{
 
	pair<long long,long long> slope_a = make_pair( y[a.i] - y[a.j] , x[a.i] - x[a.j] ) ;
	pair<long long,long long> slope_b = make_pair( y[b.i] - y[b.j], x[b.i] - x[b.j] ) ;
 
	if( getFakeQuadrant(a.i, a.j ) != getFakeQuadrant(b.i, b.j ) )
		return false ;
 
	return slope_a.first * slope_b.second == slope_a.second * slope_b.first ;
 
}
     
int main()
{
	scanf("%d", &n ) ;
	for(int i = 1 ; i <= n ; i++ ) scanf("%lld %lld %lld", &x[i], &y[i] , &w[i] ) ;
 
	vector<int> initialSort(n) ;
	iota(all(initialSort),1) ;
	sort(all(initialSort), [&](int i, int j ) { 
	
		if( y[i] != y[j] ) return y[i] > y[j] ;
		return x[i] > x[j] ; 
 
	} ) ;
 
 //	for(int i = 0 ; i < n ; i++ ) cout << char(initialSort[i] + 'A' - 1 ) << " " ;
// 	cout << endl ;
 
	vector<Event> sweep ;
	
	for(int i = 0 ; i < n ; i++ ) pos[ initialSort[i] ] = i+1 ;
	
	for(int i = 0 ; i < n ; i++ )
		for(int j = i+1 ; j < n ; j++ )
			sweep.push_back(Event(initialSort[i],initialSort[j]) ) ;
 
	sort(all(sweep));
	//Depois eu crio a seg, vamos tentar entender os eventos primeiro
 
//	for(auto e : sweep ) cout << char(e.i + 'A'-1) << " " << char(e.j+'A'-1) << endl ;
 
	for(int i = 1 ; i <= n ; i++ ) seg.upd(1,1,n, pos[i] , w[i] ) ;
	
	long long ans = 0LL ;
	ans = max(ans, seg.getAns() ) ;
 
	for(int i = 0 ; i < sz(sweep ) ;  )
	{
		//initiating batch process
		int j = i ;
 
		while( j < sz(sweep) && isEqual(sweep[i], sweep[j] ) )
		{
			int u = sweep[j].i ;
			int v = sweep[j].j ;
 
			seg.upd(1 , 1 , n , pos[u] , w[v] ) ;
			seg.upd(1,1,n, pos[v], w[u] ) ;
 
			swap(pos[u], pos[v] ) ;
 
			j++ ;
		}		
 
		i = j ;
 
		/*cout << "After some lines"<< endl ;
		seg.print(1,1,n) ;
		cout << endl ; */
 
		ans = max(ans, seg.getAns() ) ;
	}
 
	printf("%lld\n", ans ) ;
 
}

Compilation message

bulldozer.cpp: In function 'int main()':
bulldozer.cpp:137:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  137 |  scanf("%d", &n ) ;
      |  ~~~~~^~~~~~~~~~~
bulldozer.cpp:138:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  138 |  for(int i = 1 ; i <= n ; i++ ) scanf("%lld %lld %lld", &x[i], &y[i] , &w[i] ) ;
      |                                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Incorrect 2 ms 512 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 3 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 3 ms 512 KB Output is correct
33 Correct 1062 ms 17128 KB Output is correct
34 Correct 1049 ms 16992 KB Output is correct
35 Correct 1042 ms 16992 KB Output is correct
36 Correct 1083 ms 17000 KB Output is correct
37 Correct 1039 ms 17000 KB Output is correct
38 Correct 1085 ms 17000 KB Output is correct
39 Correct 1031 ms 17128 KB Output is correct
40 Correct 1071 ms 17000 KB Output is correct
41 Correct 1038 ms 17000 KB Output is correct
42 Correct 1060 ms 17000 KB Output is correct
43 Correct 1014 ms 17000 KB Output is correct
44 Correct 1012 ms 16992 KB Output is correct
45 Correct 1010 ms 17000 KB Output is correct
46 Correct 1016 ms 17004 KB Output is correct
47 Correct 1006 ms 17000 KB Output is correct
48 Correct 1022 ms 17000 KB Output is correct
49 Correct 1039 ms 17000 KB Output is correct
50 Correct 1036 ms 17000 KB Output is correct
51 Correct 1045 ms 17000 KB Output is correct
52 Correct 1041 ms 17000 KB Output is correct
53 Correct 1034 ms 16992 KB Output is correct
54 Correct 1070 ms 17000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 2 ms 512 KB Output is correct
22 Correct 2 ms 512 KB Output is correct
23 Correct 2 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 2 ms 512 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 2 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 3 ms 512 KB Output is correct
33 Correct 1062 ms 17128 KB Output is correct
34 Correct 1049 ms 16992 KB Output is correct
35 Correct 1042 ms 16992 KB Output is correct
36 Correct 1083 ms 17000 KB Output is correct
37 Correct 1039 ms 17000 KB Output is correct
38 Correct 1085 ms 17000 KB Output is correct
39 Correct 1031 ms 17128 KB Output is correct
40 Correct 1071 ms 17000 KB Output is correct
41 Correct 1038 ms 17000 KB Output is correct
42 Correct 1060 ms 17000 KB Output is correct
43 Correct 1014 ms 17000 KB Output is correct
44 Correct 1012 ms 16992 KB Output is correct
45 Correct 1010 ms 17000 KB Output is correct
46 Correct 1016 ms 17004 KB Output is correct
47 Correct 1006 ms 17000 KB Output is correct
48 Correct 1022 ms 17000 KB Output is correct
49 Correct 1039 ms 17000 KB Output is correct
50 Correct 1036 ms 17000 KB Output is correct
51 Correct 1045 ms 17000 KB Output is correct
52 Correct 1041 ms 17000 KB Output is correct
53 Correct 1034 ms 16992 KB Output is correct
54 Correct 1070 ms 17000 KB Output is correct
55 Correct 1041 ms 17000 KB Output is correct
56 Correct 1052 ms 17000 KB Output is correct
57 Correct 1040 ms 17000 KB Output is correct
58 Correct 1049 ms 17000 KB Output is correct
59 Correct 1064 ms 17032 KB Output is correct
60 Correct 1036 ms 17128 KB Output is correct
61 Correct 1057 ms 17000 KB Output is correct
62 Correct 1056 ms 17000 KB Output is correct
63 Correct 1072 ms 17000 KB Output is correct
64 Correct 1047 ms 17000 KB Output is correct
65 Correct 1054 ms 17000 KB Output is correct
66 Correct 1044 ms 17000 KB Output is correct
67 Correct 1051 ms 17000 KB Output is correct
68 Correct 1039 ms 17200 KB Output is correct
69 Correct 1026 ms 17000 KB Output is correct
70 Correct 1043 ms 17000 KB Output is correct
71 Correct 1052 ms 16992 KB Output is correct
72 Correct 1039 ms 17000 KB Output is correct
73 Correct 1034 ms 16992 KB Output is correct
74 Correct 1033 ms 17000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Incorrect 2 ms 512 KB Output isn't correct
7 Halted 0 ms 0 KB -