49612023-04-07 20:49:29SleepyOverlordMI bróker (50 pont)cpp17Futási hiba 0/504ms4904 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

template <class T> ostream& operator <<(ostream & os, const vector<T> &vec)
{
	for (int i = 0; i < vec.size() - 1; ++i) os << vec[i] << ' ';
	return os << vec[vec.size() - 1];
}

template <class T1, class T2> ostream& operator <<(ostream & os, const pair<T1, T2> &p)
{
	return os << p.X << " " << p.Y;
}

template <typename T>
void pr(T var1)
{	
	cout << var1 << '\n';
}
template <typename T, typename... Types>
void pr(T var1, Types... var2)
{
	cout << var1;
	pr(var2...);
}

#define MAX 150

int n, q;
VI a, buy;
VVI sol, pos;
MII sell;

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
	#endif

	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));
	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;
		int profit = -a[buy[0]];
		sell.CL;
		int ind = 0;
		FORD(j, MAX, i + 1)
		{
			for(int x : pos[j])
			{
				if (sell.CNT(ind))
				{
					int y = sell[ind];
					if (x < y)
					{
						profit -= a[y] - a[x];
						sell[ind] = x;
						x = y;
					}
					if (ind + 1 <= buy.SZ - 1 && x > buy[ind + 1])
					{
						++ind;
						sell[ind] = x;
						profit += a[x] - a[buy[ind]];
					}
				}
				else
				{
					if (ind) profit += a[x] - a[buy[ind]];
					else profit += a[x];
					sell[ind] = x;
				}
			}
			sol[i][j] = profit;
			if (sell.CNT(ind))
			{
				int aux = upper_bound(ALL(buy), sell[ind]) - buy.BG;
				if (aux <= buy.SZ - 1) sol[i][j] -= a[buy[aux]];
			}
		}
	}

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

	return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base0/50
1Elfogadva0/03ms2124 KiB
2Futási hiba0/03ms2408 KiB
3Futási hiba0/13ms2676 KiB
4Futási hiba0/13ms2696 KiB
5Futási hiba0/23ms2696 KiB
6Futási hiba0/23ms3056 KiB
7Futási hiba0/23ms3308 KiB
8Futási hiba0/13ms3272 KiB
9Futási hiba0/13ms3532 KiB
10Futási hiba0/23ms3916 KiB
11Futási hiba0/23ms3884 KiB
12Futási hiba0/23ms4232 KiB
13Futási hiba0/23ms4356 KiB
14Futási hiba0/23ms4216 KiB
15Futási hiba0/33ms4108 KiB
16Futási hiba0/33ms4368 KiB
17Futási hiba0/33ms4604 KiB
18Futási hiba0/33ms4632 KiB
19Futási hiba0/33ms4572 KiB
20Futási hiba0/34ms4904 KiB
21Futási hiba0/33ms4788 KiB
22Futási hiba0/33ms4864 KiB
23Futási hiba0/33ms4748 KiB
24Futási hiba0/33ms4740 KiB