답안 #791649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
791649 2023-07-24T08:31:34 Z SteGG Feast (NOI19_feast) C++17
100 / 100
105 ms 8060 KB
//Judges with GCC >= 12 only needs Ofast
//#pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
//MLE optimization
//#pragma GCC optimize("conserve-stack")
//Old judges
//#pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
//New judges. Test with assert(__builtin_cpu_supports("avx2"));
//#pragma GCC target("arch=skylake")
//Atcoder
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

//write variables and debug
#define bugf cout << "Here" << endl
#define bug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define bugarr(arr,...) cout << #arr; _dbg_arr(arr, __VA_ARGS__)

template<class T, class U>
ostream& operator<<(ostream& output, const pair<T, U> &A){
	output << "(" << A.first << ", " << A.second <<  ")";
	return output;
}

template <class T>
ostream& operator<<(ostream& output, const vector<T> &arr){
	int n = arr.size();
	output << "[";
	for(int i = 0; i < n; i++){
		output << arr[i];
		if(i != n - 1) output << ", ";
	}
	output << "]";
	return output;
}

template<class T>
ostream& operator<<(ostream& output, const set<T> &s){
	output << "{";
	for(auto it = s.begin(); it != s.end(); it++){
		if(it != s.begin()) output << ", ";
		output << (*it);
	}
	output << "}";
	return output;
}

template<class T, class U>
ostream& operator<<(ostream& output, const map<T, U> &m){
	output << "{";
	for(auto it = m.begin(); it != m.end(); it++){
		if(it != m.begin()) output << ", ";
		output << "(" << it->first << " : " << it->second << ")";
	}
	output << "}";
	return output;
}

template<class TH>
void _dbg(const char *sdbg, TH h){
	cout << sdbg << " = " << h << endl;
}

template<class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
	while(*sdbg != ',')cout << *sdbg++;
	cout << " = " << h << "\n";
	_dbg(sdbg+1, a...);
}

template<class _Arr, class _Index>
void _dbg_arr(_Arr arr, _Index index){
	cout << '[' << index << "] = " << arr[index] << endl;
}

template<class _Arr, class _Index, class... TA>
void _dbg_arr(_Arr &arr, _Index index, TA... args){
	cout << '[' << index << ']';
	_dbg_arr(arr[index], args...);
}

//open file
#define taskname "" //name input and output file

#ifdef SteGG
void open(){
	if(fopen("input.inp", "r")){
		freopen("input.inp", "r", stdin);
		freopen("output.out", "w", stdout);
	}
}
#else
void open(){
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
		freopen(taskname".out", "w", stdout);
	}
}
#endif

//initialize
#define testcase \
	long long test; \
	cin >> test; \
	for(int tst = 1; tst <= test; tst++)
#define int long long
#define MOD 1000000007
#define FFTMOD (119 << 23 | 1)
#define EPSILON 1e-9
#define setpre(x) fixed << setprecision(x)
#define cd complex<double>
#define all(x) x.begin(), x.end()

const long long INF = 3e18L + 5;
const int inf = 1e9 + 7;
const double infd = 1e60;
const double PI = acos(-1);
const int maxn = 3e5 + 5;
//const int base = ;
int n, k;
int arr[maxn];
int prefix[maxn];

//others struct or class

//others function

//others init

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	open();
	cin >> n >> k;

	int cnt = 0;

	for(int i = 1; i <= n; i++){
		cin >> arr[i];
		prefix[i] = prefix[i - 1] + arr[i];
		if(arr[i] < 0) cnt++;
	}


	if(k == 1){
		int best = 0;
		int cur = 0;
		for(int i = 1; i <= n; i++){
			cur += arr[i];
			if(cur > best){
				best = cur;
			}

			if(cur < 0) cur = 0;
		}

		cout << best << endl;
	}else if(cnt == 0){
		int result = 0;
		for(int i = 1; i <= n; i++){
			result += arr[i];
		}

		cout << result << endl;
	}else if(cnt == 1){
		int result = 0;
		for(int i = 1; i <= n; i++){
			if(arr[i] >= 0) result += arr[i];
		}

		cout << result << endl;
	}else{

		auto solve = [&](int alpha){
			pair<int, int> dp_alpha = {0, 0}, best = {0, 0};
			for(int i = 1; i <= n; i++){
				dp_alpha = max(dp_alpha, {best.first + prefix[i] - alpha, best.second - 1});
				best = max(best, {dp_alpha.first - prefix[i], dp_alpha.second});
			}

			dp_alpha.second *= -1;

			return dp_alpha;
		};

		int l = 0;
		int r = 3e14;
		int result = 0;
		while(l <= r){
			int mid = (l + r) >> 1;
			pair<int, int> cur = solve(mid);

			if(cur.second <= k){
				r = mid - 1;
				result = cur.first + k * mid;
			}else{
				l = mid + 1;
			}
		}

		cout << result << endl;
	}



	return 0;
}

Compilation message

feast.cpp: In function 'void open()':
feast.cpp:101:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |   freopen(taskname".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:102:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |   freopen(taskname".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 4872 KB Output is correct
2 Correct 24 ms 4896 KB Output is correct
3 Correct 23 ms 4940 KB Output is correct
4 Correct 23 ms 4976 KB Output is correct
5 Correct 28 ms 4844 KB Output is correct
6 Correct 23 ms 4740 KB Output is correct
7 Correct 22 ms 4820 KB Output is correct
8 Correct 24 ms 4916 KB Output is correct
9 Correct 25 ms 4780 KB Output is correct
10 Correct 23 ms 4840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 4908 KB Output is correct
2 Correct 16 ms 4968 KB Output is correct
3 Correct 15 ms 4820 KB Output is correct
4 Correct 21 ms 4928 KB Output is correct
5 Correct 23 ms 4844 KB Output is correct
6 Correct 16 ms 4848 KB Output is correct
7 Correct 18 ms 4972 KB Output is correct
8 Correct 23 ms 4948 KB Output is correct
9 Correct 23 ms 4764 KB Output is correct
10 Correct 15 ms 4940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 4948 KB Output is correct
2 Correct 25 ms 4780 KB Output is correct
3 Correct 30 ms 4888 KB Output is correct
4 Correct 25 ms 4788 KB Output is correct
5 Correct 25 ms 4948 KB Output is correct
6 Correct 25 ms 4940 KB Output is correct
7 Correct 25 ms 4900 KB Output is correct
8 Correct 25 ms 4932 KB Output is correct
9 Correct 29 ms 4908 KB Output is correct
10 Correct 25 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 4872 KB Output is correct
2 Correct 24 ms 4896 KB Output is correct
3 Correct 23 ms 4940 KB Output is correct
4 Correct 23 ms 4976 KB Output is correct
5 Correct 28 ms 4844 KB Output is correct
6 Correct 23 ms 4740 KB Output is correct
7 Correct 22 ms 4820 KB Output is correct
8 Correct 24 ms 4916 KB Output is correct
9 Correct 25 ms 4780 KB Output is correct
10 Correct 23 ms 4840 KB Output is correct
11 Correct 21 ms 4908 KB Output is correct
12 Correct 16 ms 4968 KB Output is correct
13 Correct 15 ms 4820 KB Output is correct
14 Correct 21 ms 4928 KB Output is correct
15 Correct 23 ms 4844 KB Output is correct
16 Correct 16 ms 4848 KB Output is correct
17 Correct 18 ms 4972 KB Output is correct
18 Correct 23 ms 4948 KB Output is correct
19 Correct 23 ms 4764 KB Output is correct
20 Correct 15 ms 4940 KB Output is correct
21 Correct 30 ms 4948 KB Output is correct
22 Correct 25 ms 4780 KB Output is correct
23 Correct 30 ms 4888 KB Output is correct
24 Correct 25 ms 4788 KB Output is correct
25 Correct 25 ms 4948 KB Output is correct
26 Correct 25 ms 4940 KB Output is correct
27 Correct 25 ms 4900 KB Output is correct
28 Correct 25 ms 4932 KB Output is correct
29 Correct 29 ms 4908 KB Output is correct
30 Correct 25 ms 4948 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 344 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 95 ms 7864 KB Output is correct
62 Correct 88 ms 8052 KB Output is correct
63 Correct 76 ms 7848 KB Output is correct
64 Correct 92 ms 7924 KB Output is correct
65 Correct 95 ms 7984 KB Output is correct
66 Correct 101 ms 8060 KB Output is correct
67 Correct 105 ms 7860 KB Output is correct
68 Correct 64 ms 8012 KB Output is correct
69 Correct 62 ms 7776 KB Output is correct
70 Correct 61 ms 7748 KB Output is correct