Submission #768955

# Submission time Handle Problem Language Result Execution time Memory
768955 2023-06-29T01:59:50 Z Charizard2021 Schools (IZhO13_school) C++17
100 / 100
225 ms 22840 KB
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);; cin.tie(NULL)
void selfMax(long long& a, long long b){
    a = max(a, b);
}
void selfMin(long long& a, long long b){
    a = min(a, b);
}
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.first >> x.second;}

template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.first << ' ' << x.second;}
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;};
template<typename T> ostream& operator<<(ostream& out, vector<vector<T> >& a) {for(auto &x : a) out << x << '\n'; return out;};
template<typename T1, typename T2> ostream& operator<<(ostream& out, vector<pair<T1, T2> >& a) {for(auto &x : a) out << x << '\n'; return out;};

/*

Use DSU dsu(N);

*/
struct DSU {
	vector<long long> e;
	DSU(long long N) { e = vector<long long>(N, -1); }

	// get representive component (uses path compression)
	long long get(long long x) { return e[x] < 0 ? x : e[x] = get(e[x]); }

	bool same_set(long long a, long long b) { return get(a) == get(b); }

	long long size(long long x) { return -e[get(x)]; }

	bool unite(long long x, long long y) {  // union by size
		x = get(x), y = get(y);
		if (x == y) return false;
		if (e[x] > e[y]) swap(x, y);
		e[x] += e[y]; e[y] = x;
		return true;
	}
};
/*

Run with Bit<long long> BIT

*/
template <class T> class BIT {
    private:
        long long size;
        vector<T> bit;
        vector<T> arr;
    public:
        BIT(long long size) : size(size), bit(size + 1), arr(size) {}
        void set(long long ind, long long val) { add(ind, val - arr[ind]); }
        void add(long long ind, long long val) {
            arr[ind] += val;
            ind++;
            for (; ind <= size; ind += ind & -ind) { bit[ind] += val; }
        }
        T pref_sum(long long ind) {
            ind++;
            T total = 0;
            for (; ind > 0; ind -= ind & -ind) { total += bit[ind]; }
            return total;
        }
};
/*

Change Comb for specific Seg tree probs, but run with Seg<long long> Seg;

*/
template<class T> struct Seg {
	const T ID = 1e18; T comb(T a, T b) { return min(a,b); }
	long long n; vector<T> seg;
	void init(long long _n) { n = _n; seg.assign(2*n,ID); }
	void pull(long long p) { seg[p] = comb(seg[2*p],seg[2*p+1]); }
	void upd(long long p, T val) {
		seg[p += n] = val; for (p /= 2; p; p /= 2) pull(p); }
	T query(long long l, long long r) {
		T ra = ID, rb = ID;
		for (l += n, r += n+1; l < r; l /= 2, r /= 2) {
			if (l&1) ra = comb(ra,seg[l++]);
			if (r&1) rb = comb(seg[--r],rb);
		}
		return comb(ra,rb);
	}
};
bool cmp(const pair<long long, long long>& a, const pair<long long, long long>& b){
    return a.first - a.second < b.first - b.second;
}
vector<long long> solve(vector<long long>& v, long long k) {
	multiset<long long> ms;
	vector<long long> ans;
	ans.push_back(-1e9);
	long long sum = 0;
	for (long long i = 0; i < v.size(); i++){
		if (i <= k - 1){
			ms.insert(v[i]);
			sum += v[i];
		}
        else{
			if (v[i] >= *(ms.begin())){
				sum -= *(ms.begin());
				ms.erase(ms.find(*(ms.begin())));
				sum += v[i];
				ms.insert(v[i]);
			}
		}
        if(i >= k - 1){
            ans.push_back(sum);
        }
        else{
            ans.push_back(-1e9);
        }
	}
	return ans;
}
int main(){
    long long n, m, s;
    cin >> n >> m >> s;
    vector<pair<long long, long long> > v(n);
    for(long long i = 0; i < n; i++){
        cin >> v[i];
    }
    sort(v.begin(), v.end(), cmp);
    vector<long long> music;
    vector<long long> sport;
    for(long long i = 0; i < n; i++){
        music.push_back(v[i].first);
        sport.push_back(v[i].second);
    }
    if(s == 0){
        cout << solve(music, m).back() << "\n";
    }
    else if(m == 0){
        cout << solve(sport, s).back() << "\n";
    }
    else{
        reverse(music.begin(), music.end());
        vector<long long> ans1 = solve(music, m);
        vector<long long> ans2 = solve(sport, s);
        reverse(ans2.begin(), ans2.end());
        long long ans = 0;
        for(long long i = 0; i <= n; i++){
            ans = max(ans, ans1[i] + ans2[i]);
        }
        cout << ans << "\n";
    }
}

Compilation message

school.cpp: In function 'std::vector<long long int> solve(std::vector<long long int>&, long long int)':
school.cpp:96:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |  for (long long i = 0; i < v.size(); i++){
      |                        ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 3 ms 724 KB Output is correct
11 Correct 3 ms 724 KB Output is correct
12 Correct 4 ms 724 KB Output is correct
13 Correct 24 ms 3656 KB Output is correct
14 Correct 45 ms 5720 KB Output is correct
15 Correct 81 ms 10056 KB Output is correct
16 Correct 151 ms 16128 KB Output is correct
17 Correct 167 ms 16556 KB Output is correct
18 Correct 176 ms 16928 KB Output is correct
19 Correct 225 ms 18212 KB Output is correct
20 Correct 225 ms 22840 KB Output is correct