Submission #1041082

# Submission time Handle Problem Language Result Execution time Memory
1041082 2024-08-01T15:12:36 Z Alihan_8 Distributing Candies (IOI21_candies) C++17
3 / 100
5000 ms 8576 KB
#include "candies.h"

#include <vector>

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
//#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

const i64 inf = 1e15;

std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
                                    std::vector<int> r, std::vector<int> v) {
    
    int q = l.size(), n = c.size();
    
    vector <int> ans(n);
    
    for ( int i = 0; i < n; i++ ){
		vector <int> p;
		
		for ( int j = 0; j < q; j++ ){
			if ( l[j] <= i && r[j] >= i ){
				p.pb(j);
			}
		}
		
		int m = p.size(); 
		
		vector <i64> pf(m + 1);
		
		int lst = -1, t = -1;
		
		stack <int> s1, s2; 
		s1.push(-1), s2.push(-1);
		
		vector <int> mn, mx;
		
		mn = mx = {0};
		
		for ( int j = 1; j <= m; j++ ){
			int u = p[j - 1];
			
			pf[j] = pf[j - 1] + v[u];
			
			while ( s1.size() > 1 && pf[s1.top()] >= pf[j] ) s1.pop();
			
			while ( s2.size() > 1 && pf[s2.top()] <= pf[j] ) s2.pop();
			
			{ // min case
				int it = lower_bound(all(mx), s1.top() + 1) - mx.begin();
				
				if ( it < (int)mx.size() && pf[mx[it]] - pf[j] >= c[i]  ){
					lst = j, t = 0;
				}
			}
			
			{ // max case
				int it = lower_bound(all(mn), s2.top() + 1) - mn.begin();
				
				if ( it < (int)mn.size() && pf[j] - pf[mn[it]] >= c[i] ){
					lst = j, t = 1;
				}
			}
			
			s1.push(j), s2.push(j);
			
			while ( !mn.empty() && pf[mn.back()] >= pf[j] ){
				mn.pop_back();
			}
			
			while ( !mx.empty() && pf[mx.back()] <= pf[j] ){
				mx.pop_back();
			}
			
			mn.pb(j), mx.pb(j);
		}
		
		if ( lst == -1 ){ // no touch
			i64 cnt = 0;
			
			for ( auto &j: p ){
				cnt += v[j];
			}
			
			cnt -= *min_element(all(pf));
			
			ans[i] = cnt;
			
		} else{
			i64 cnt = 0;
			
			for ( int k = lst; k < m; k++ ){
				cnt += v[p[k]];
			}
			
			cnt += t * c[i];
			
			ans[i] = cnt;
		}
	}
	
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 44 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5066 ms 8240 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Execution timed out 5085 ms 6880 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Execution timed out 5087 ms 8576 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 44 ms 348 KB Output is correct
6 Execution timed out 5066 ms 8240 KB Time limit exceeded
7 Halted 0 ms 0 KB -