372C - Watching Fireworks is Fun - CodeForces Solution


data structures dp math *2100

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <queue>
#include <cmath>

using namespace std;

typedef long long int LL;

const LL N = 2e5, INF = 1e18;
LL dp[2][N], n;
deque <int> kol;

inline void push(int x) {
	if(kol.empty()) {
		kol.push_back(x);
		return;
	} while(!kol.empty() && dp[0][kol.back()] <= dp[0][x])
		kol.pop_back();
	kol.push_back(x);
}

inline void count_dp(int a, int b, LL dst) {
	for(int i = 0; i <= n; i++) {
		dp[0][i] = dp[1][i];
		dp[1][i] = -INF;
	} for(int i = 1; i < min(n + 1, dst); i++)
		push(i);
	for(int i = 1; i <= n; i++) {
		if(!kol.empty() && kol.front() < i - dst)
			kol.pop_front();
		if(i + dst <= n)
			push(i + dst);
		dp[1][i] = dp[0][kol.front()] + b - abs(a - i);
	} while(!kol.empty())
        kol.pop_back();
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	LL d, m, last = 0, ans = -INF; cin >> n >> m >> d;
	for(int i = 0; i < m; i++) {
		int a, b, t; cin >> a >> b >> t;
		count_dp(a, b, d * (t - last));
		last = t;
	} for(int i = 1; i <= n; i++)
		ans = max(ans, dp[1][i]);
	cout << ans << "\n";
	return 0;
}


Comments

Submit
0 Comments
More Questions

729D - Sea Battle
788A - Functions again
1245B - Restricted RPS
1490D - Permutation Transformation
1087B - Div Times Mod
1213B - Bad Prices
1726B - Mainak and Interesting Sequence
1726D - Edge Split
1726C - Jatayu's Balanced Bracket Sequence
1726A - Mainak and Array
1613C - Poisoned Dagger
475B - Strongly Connected City
652B - z-sort
124B - Permutations
1496C - Diamond Miner
680B - Bear and Finding Criminals
1036E - Covered Points
1015D - Walking Between Houses
155B - Combination
1531A - Зингер | color
1678A - Tokitsukaze and All Zero Sequence
896A - Nephren gives a riddle
761A - Dasha and Stairs
1728B - Best Permutation
1728A - Colored Balls Revisited
276B - Little Girl and Game
1181A - Chunga-Changa
1728C - Digital Logarithm
1728D - Letter Picking
792B - Counting-out Rhyme