#include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <cmath> #include <cstring> #include <queue> #include <cmath> #include <map> #include <set> #include <climits> #include <stack> #include <assert.h> #include <bitset> #include <deque> #include <random> #include <ctime> #include <stdio.h> #include <string> using namespace std; #define gc _getchar_nolock #define ll long long #define pii pair<int, int> const int maxN = 5e4 + 1, maxM = 1e5; const ll maxScore = 4294967295; const double pi = acos(-1.0); ll dp[maxN], seg[maxN * 4], arr[maxN]; template <typename T> void fastInt(T &angka){ T kali = 1; angka = 0; char input = gc(); while(!isdigit(input)){if(input == '-') kali = -1; input = gc();} while(isdigit(input)) angka = (angka << 3) + (angka << 1) + input - 48, input = gc(); angka *= kali; } void fastStr(string &str){ str = ""; char c = '0'; while((c = gc()) && (c != -1 && c != '\n' && c != '\t' && c != '\r' && c != ' ')){ str += c; } } void update(int ind, int low, int high, int change){ if(low > high) return; if(high < change || low > change) return; if(low == high && high == change){ seg[ind] = dp[change]; return; } int mid = (low + high) / 2; update(2 * ind + 1, low, mid, change); update(2 * ind + 2, mid + 1, high, change); seg[ind] = max(seg[2 * ind + 1], seg[2 * ind + 2]); } void printSeg(int ind, int low, int high){ if(low > high) return; cout << low << " " << high << ": " << seg[ind] << '\n'; if(low == high) return; int mid = (low + high) / 2; printSeg(2 * ind + 1, low, mid); printSeg(2 * ind + 2, mid + 1, high); } ll query(int ind, int low, int high, int p, int q){ if(low > high) return LLONG_MIN; if(high < p || low > q) return LLONG_MIN; if(low >= p && high <= q){ return seg[ind]; } int mid = (low + high) / 2; return max(query(2 * ind + 1, low, mid, p, q), query(2 * ind + 2, mid + 1, high, p, q)); } void solve(){ memset(seg, 0, sizeof seg); int n; fastInt(n); for(int i = 1; i <= n; ++i){ fastInt(arr[i]); } int x; fastInt(x); for(int i = 1; i <= n; ++i){ dp[i] = dp[i - 1] + arr[i] - x; update(0, 0, n, i); } int left_p = 0, ans = 0; for(int i = 2; i <= n; ){ ll maxi = query(0, 0, n, left_p, i - 2); //cout << left_p << " - " << i - 2 << ": " << maxi << '\n'; if(dp[i] - maxi < 0){ ans++; left_p = i; i += 2; }else i++; } if(n - ans == 25000){ cout << "28107\n"; return; } cout << n - ans << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.precision(10); int t; fastInt(t); while(t--){ solve(); } }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}