답안 #1056647

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1056647 2024-08-13T10:27:11 Z dozer 메기 농장 (IOI22_fish) C++17
컴파일 오류
0 ms 0 KB
#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define pii pair<int, int>
#define st first
#define nd second
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define ll long long
#define LL node * 2
#define RR node * 2 + 1
#define MAXN 300005

const int modulo = 1e9 + 7;
const ll INF = 2e18 + 7;


long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
	int n = N, m = M;
    vector<array<int, 3>> v;
    for (int i = 0; i < m; i++) v.pb({X[i], Y[i], W[i]});
    sort(v.begin(), v.end());
    vector<int> x(m, 0), y(m, 0), w(m, 0);
    for (int i = 0; i < m; i++){
    	x[i] = v[i][0], y[i] = v[i][1], w[i] = v[i][2];
    }

    vector<vector<ll>> dp(2, vector<ll> (m + 5, 0));
    vector<vector<ll>> start(2, vector<ll> (n + 5, 0));
    vector<vector<pii>> fish(n + 5);
    vector<ll> pre(m + 5, 0);
    
    pre[0] = w[0];
    for (int i = 1; i < m; i++) pre[i] = pre[i - 1] + w[i];


    auto sum = [&](int l, int r){
    	ll ans = pre[r];
    	if (l > 0) ans -= pre[l - 1];
    	return ans;
    };


    for (int i = 0; i < m; i++) fish[x[i]].pb({y[i], i});
   	int it = n - 1;
    for (int i = m - 1; i >= 0; i--){
    	while(it >= x[i]){
    		//cout<<it + 1<<" : "<<start[0][it + 1]<<sp<<start[1][it + 1]<<endl;
    		start[0][it] = start[0][it + 1];
    		start[1][it] = start[1][it + 1];
    		it--;
    	}

    	for (int j = 0; j < 2; j++){
    		int a = x[i], b = y[i];
    		if (j == 0){ // increasing
    			//todo : continue increasing
    			
    			int k = lower_bound(fish[a + 1].begin(), fish[a + 1].end(), make_pair(b - 1, 0)) - fish[a + 1].begin();

    			for (int l = k; l < fish[a + 1].size(); l++){
    				int curr = fish[a + 1][l].st, to = fish[a + 1][l].nd;
    				int pos = lower_bound(fish[a].begin(), fish[a].end(), make_pair(curr, 0)) - fish[a].end();
    				if (pos > 0){
    					pos--;
    					ll c = sum(i, pre[fish[a][pos].nd]);
    					dp[j][i] = max(dp[j][i], dp[j][to] + c);
    				}
    			}

    			// todo : make the next column full and then decrease or //todo : make the next column full and the other one 0

    			ll c = sum(i, fish[a].back().nd);
    			ll summ = 0;
    			for (int A = a + 2; A < n; A += 2){
    				for (auto k : fish[A]){
	    				ll c2 = sum(k.nd, fish[A].back().nd);
	    				dp[j][i] = max(dp[j][i], dp[1][k.nd] + summ + c + c2);
	    			}
	    			if (!fish[A].empty()){
	    				summ += sum(fish[A].front().nd, fish[A].back().nd);
	    			}
    			}	

    			if (a + 1 == n - 1){
    				dp[j][i] = max(dp[j][i], c);
    			}

    			

    			// todo: make the next 2 columns full
    			if (a + 3 < n) dp[j][i] = max(dp[j][i], start[1][a + 3] + c);

    			ll pref = 0;
    			if (a > 0){
    				int k = lower_bound(fish[a - 1].begin(), fish[a - 1].end(), make_pair(b, 0)) - fish[a - 1].begin();
    				k--;
    				if (k >= 0){
    					pref = sum(fish[a - 1].front().nd, fish[a - 1][k].nd);
    				}
    			}
    			start[0][a] = max(start[0][a], dp[j][i] + pref);
    		}

    		else{ // decreasing
    			//todo : continue decreasing
    			int k = lower_bound(fish[a + 1].begin(), fish[a + 1].end(), make_pair(b, 0)) - fish[a + 1].end();
    			k--;

    			for (int l = 0; l <= k; l++){
    				int curr = fish[a + 1][l].st, to = fish[a + 1][l].nd;
    				
					ll c = sum(to, fish[a + 1][k].nd);
					dp[j][i] = max(dp[j][i], dp[j][to] + c);
    			}

    			

    			//todo : decrease to 0 and then increase
    			//or decrease to 0 and make the other column full

    			for (int A = a + 2; A < n; A += 2){
    				for (auto l : fish[a + 2]){
	    				int maks = max(l.st, b);
	    				int k = lower_bound(fish[a + 1].begin(), fish[a + 1].end(), make_pair(maks, 0)) - fish[a + 1].end();
	    				k--;
	    				if (k >= 0){
	    					ll c = sum(fish[a + 1][0].nd, fish[a + 1][k].nd);
	    					dp[j][i] = max(dp[j][i], dp[0][l.nd] + c);
	    				}
	    			}	
    			}
    			if (a + 2 < n){
	    			for (auto l : fish[a + 2]){
	    				int maks = max(l.st, b);
	    				int k = lower_bound(fish[a + 1].begin(), fish[a + 1].end(), make_pair(maks, 0)) - fish[a + 1].end();
	    				k--;
	    				if (k >= 0){
	    					c = sum(fish[a + 1][0].nd, fish[a + 1][k].nd);
	    					dp[j][i] = max(dp[j][i], dp[0][l.nd] + c);
	    				}
	    			}	
    			}

    			ll c = 0;
    			if (k >= 0) c = sum(fish[a + 1][0].nd, fish[a + 1][k].nd);

    			if (a + 1 == n - 1) {
    				dp[j][i] = max(dp[j][i], c);
    			}

    			//todo make the next 2 columns 0
    			if (a + 3 < n){
    				dp[j][i] = max(dp[j][i], c + start[0][a + 3]);
    			}

    			ll suf = sum(i, fish[a].back().nd);

    			start[1][a] = max(start[1][a], dp[j][i] + suf);
    		}
    		//cout<<i<<sp<<j<<sp<<" : "<<dp[j][i]<<endl;
    	}

    }

    while(it >= 0){
    	//cout<<it + 1<<" : "<<start[0][it + 1]<<sp<<start[1][it + 1]<<endl;
		start[0][it] = start[0][it + 1];
		start[1][it] = start[1][it + 1];
		it--;
	}
    return start[0][0];
}
/*
int main() {
	fileio();
	int N, M;
	assert(2 == scanf("%d %d", &N, &M));

	std::vector<int> X(M), Y(M), W(M);
	for (int i = 0; i < M; ++i) {
		assert(3 == scanf("%d %d %d", &X[i], &Y[i], &W[i]));
	}

	long long result = max_weights(N, M, X, Y, W);
	printf("%lld\n", result);
	return 0;
}*/

Compilation message

fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:64:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |        for (int l = k; l < fish[a + 1].size(); l++){
      |                        ~~^~~~~~~~~~~~~~~~~~~~
fish.cpp:114:13: warning: unused variable 'curr' [-Wunused-variable]
  114 |         int curr = fish[a + 1][l].st, to = fish[a + 1][l].nd;
      |             ^~~~
fish.cpp:142:11: error: 'c' was not declared in this scope
  142 |           c = sum(fish[a + 1][0].nd, fish[a + 1][k].nd);
      |           ^