49682023-04-07 23:07:20SleepyOverlordMI bróker (50 pont)cpp17Elfogadva 50/50953ms7464 KiB
#include <vector>
#include <string> 
#include <set> 
#include <map> 
#include <unordered_set>
#include <unordered_map>
#include <queue> 
#include <bitset> 
#include <stack>
#include <list>

#include <numeric> 
#include <algorithm> 
#include <random>
#include <chrono>

#include <cstdio>
#include <fstream>
#include <iostream> 
#include <sstream> 
#include <iomanip>
#include <climits>

#include <cctype>
#include <cmath> 
#include <ctime>
#include <cassert>

using namespace std;

#define ULL unsigned long long
#define LL long long
#define PII pair <int, int>
#define VB vector <bool>
#define VI vector <int>
#define VLL vector <LL>
#define VD vector <double>
#define VS vector <string>
#define VPII vector <pair <int, int> >
#define VVI vector < VI >
#define VVB vector < VB >
#define SI set < int >
#define USI unordered_set <int>
#define MII map <int, int>
#define UMII unordered_map <int, int>
#define MS multiset
#define US unordered_set
#define UM unordered_map
#define UMS unordered_multiset
#define UMM unordered_multimap

#define FORN(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define SZ size()
#define BG begin() 
#define EN end() 
#define CL clear()
#define X first
#define Y second
#define RS resize
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(), x.end()
#define INS insert
#define ER erase
#define CNT count

#define MAX 500

int n, q, profit;
VI a, buy, sell;
VVI sol, pos;
SI unpaired, paired;

void addNewUnpaired(int sellPos)
{
	auto it = upper_bound(ALL(buy), sellPos);
	if (it != buy.EN && !paired.CNT(*it) && !unpaired.CNT(*it))
	{
		unpaired.INS(*it);
		profit -= a[*it];
	}
}

void addNewPair(int buyPos, int sellPos)
{
	profit += a[sellPos];
	sell[buyPos] = sellPos;
	unpaired.ER(buyPos);
	paired.INS(buyPos);
}

bool pairWithUnpaired(int sellPos)
{
	auto buyPos = unpaired.lower_bound(sellPos);
	if (buyPos != unpaired.BG)
	{
		--buyPos;
		int aux = *buyPos;
		addNewPair(aux, sellPos);
		addNewUnpaired(sellPos);
		return true;
	}
	return false;
}

void pairWithPaired(int sellPos)
{
	auto buyPos = paired.lower_bound(sellPos);
	if (buyPos == paired.BG) return;
	
	--buyPos;
	int aux = *buyPos;
	int oldSellPos = sell[aux];
	if (sellPos > oldSellPos) return;

	profit += a[sellPos] - a[oldSellPos];
	addNewUnpaired(sellPos);
	sell[aux] = sellPos;
						
	buyPos = unpaired.lower_bound(oldSellPos);
	if (buyPos == unpaired.BG) return;

	--buyPos;
	aux = *buyPos;
	if (sell[aux] == -1)
	{
		addNewPair(aux, oldSellPos);
		addNewUnpaired(oldSellPos);
	}
}

int main()
{
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

	cin >> n >> q;
	a.RS(n + 1);
	pos.RS(MAX + 1);
	FOR(i, 1, n) 
	{
		cin >> a[i];
		pos[a[i]].PB(i);
	}

	sol.RS(MAX + 1, VI(MAX + 1));
	sell.RS(n + 1);
	FOR(i, 1, MAX - 1)
	{
		VI res(buy.SZ + pos[i].SZ);
		merge(ALL(buy), ALL(pos[i]), res.BG);
		buy = res;
		if (buy.empty()) continue;
		profit = -a[buy[0]];
		unpaired = {buy[0]}, paired = {};
		fill(ALL(sell), -1);
		FORD(j, MAX, i + 1)
		{
			for(int sellPos : pos[j])
			{
				if (!pairWithUnpaired(sellPos)) pairWithPaired(sellPos);
			}
			sol[i][j] = profit;
		}
	}

	FOR(i, 1, q)
	{
		int x, y;
		cin >> x >> y;
		cout << sol[x][y] << "\n";
	}

	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/04ms3612 KiB
2Elfogadva0/0833ms4912 KiB
3Elfogadva1/13ms4088 KiB
4Elfogadva1/14ms4196 KiB
5Elfogadva2/264ms4732 KiB
6Elfogadva2/2907ms5588 KiB
7Elfogadva2/2917ms5932 KiB
8Elfogadva1/145ms5956 KiB
9Elfogadva1/1187ms5868 KiB
10Elfogadva2/2479ms5908 KiB
11Elfogadva2/2391ms6144 KiB
12Elfogadva2/2426ms6104 KiB
13Elfogadva2/2432ms6372 KiB
14Elfogadva2/2418ms6300 KiB
15Elfogadva3/3950ms6652 KiB
16Elfogadva3/3948ms6700 KiB
17Elfogadva3/3953ms6828 KiB
18Elfogadva3/3939ms7192 KiB
19Elfogadva3/3952ms7224 KiB
20Elfogadva3/3940ms7424 KiB
21Elfogadva3/3940ms7440 KiB
22Elfogadva3/3939ms7464 KiB
23Elfogadva3/3939ms7436 KiB
24Elfogadva3/3953ms7456 KiB