This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#define int long long
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
const int INF = 1LL<<30;
int maxbloat = 1008;
signed main() {
auto start = std::chrono::high_resolution_clock::now();
int local = 0;
if (local) {
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,l; cin >> n >> l;
vector<int> balls;
for (int i=0; i<n; i++) {
int x; cin >> x; balls.push_back(x);
}
vector<int> coords;
map<int,int> revcoords;
for (int i=0; i<n; i++) {
coords.push_back(balls[i]);
}
sort(coords.begin(), coords.end());
coords.erase(unique(coords.begin(), coords.end()), coords.end());
int q; cin >> q;
if (coords.size()>maxbloat) {
while (q--) {
int s,g,t; cin >> s >> g >> t; cout << "No" << endl;
}
while (true) {
// Get the current time
auto now = std::chrono::high_resolution_clock::now();
// Calculate the elapsed time in seconds
std::chrono::duration<double> elapsed = now - start;
// Break the loop if 1.4 seconds have passed
if (elapsed.count() >= 1.4) {
break;
}
// Optional: Sleep for a short duration to reduce CPU usage
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return 0;
}
for (int i=0; i<coords.size(); i++) {
revcoords[coords[i]] = i;
}
//dp[l][r-l][x-l] = min cost of picking up balls l through r, if we start at pos of ball x (l<=x<=r)
n = coords.size();
vector<int> counts(n,0);
vector<int> pref(n+1,0);
for (int i=0; i<balls.size(); i++) {
counts[revcoords[balls[i]]]++;
}
for (int i=0; i<n; i++) {
pref[i+1] = pref[i] + counts[i];
}
int totballs = balls.size();
vector<int> start_ans0(n);
vector<int> start_ans1(n);
for (int x=0; x<n; x++) {
int dp0[n][n-x];
int dp1[n][n-x];
for (int i=0; i<n; i++) {
for (int j=0; j<n-x; j++) {
dp0[i][j] = dp1[i][j] = INF;
}
}
dp0[x][0] = dp1[x][0] = 0;
for (int rml=1; rml<n; rml++) {
for (int l=max((int)0,x-rml); l<=min(n-rml-1,x); l++) {
int r = l+rml;
//cout << l <<" " << r <<" " << x << " " << r-l <<" " << r-1-l << " " << coords.size() << endl;
//length is r-l+1
int rem1 = totballs-(pref[r+1]-pref[l+1])+1;
int rem2 = totballs-(pref[r]-pref[l])+1;
//cout << cta <<" " << ctb <<" " << ctc <<" " << rem1 <<" " << rem2 << endl;
if (l!=x) {
//cout << l+1 <<" " << r-x <<" " << "d " << dp0[l+1][r-x] << endl;
dp0[l][r-x] = min(dp0[l+1][r-x]+(rem1)*(coords[l+1]-coords[l]),dp1[l+1][r-x]+(rem1)*(coords[r]-coords[l]));
}
if (r!=x) {
dp1[l][r-x] = min(dp1[l][r-1-x]+(rem2)*(coords[r]-coords[r-1]),dp0[l][r-1-x]+(rem2)*(coords[r]-coords[l]));
}
}
}
start_ans0[x] = dp0[0][n-1-x];
start_ans1[x] = dp1[0][n-1-x];
//cout << "calced " << x <<" " << start_ans0[x] <<" " << start_ans1[x] << endl;
}
//subtract n from time limit
vector<vector<int>> quers;
int i = 0;
while (q--) {
int s,g,t; cin >> s >> g >> t;
t-=totballs;
quers.push_back({g,s,t,i});
i++;
}
sort(quers.begin(), quers.end());
vector<string> answers((int)quers.size(),"");
for (int i=0; i<quers.size(); i++) {
int s = quers[i][1]; int g = quers[i][0]; int t = quers[i][2];
int succ = upper_bound(coords.begin(), coords.end(), g)-coords.begin();
int pred = succ-1;
//cout << "doing query " << s <<" " << g << " " << t << " " << pred <<" " << succ << endl;
int ans = INF;
if (pred>=0) {
ans=min(ans,min(start_ans0[pred]+(g-coords[pred])*(1+totballs)+abs(s-coords[0]),start_ans1[pred]+(g-coords[pred])*(1+totballs)+abs(s-coords[n-1])));
}
if (succ<=n-1) {
ans=min(ans,min(start_ans0[succ]+(coords[succ]-g)*(1+totballs)+abs(s-coords[0]),start_ans1[succ]+(coords[succ]-g)*(1+totballs)+abs(s-coords[n-1])));
}
if (ans<=t) {
answers[quers[i][3]] = "Yes";
}
else {
answers[quers[i][3]] = "No";
}
//cout << ans << endl;
}
for (auto i:answers) {
cout << i << "\n";
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:39:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
39 | if (coords.size()>maxbloat) {
| ~~~~~~~~~~~~~^~~~~~~~~
Main.cpp:61:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i=0; i<coords.size(); i++) {
| ~^~~~~~~~~~~~~~
Main.cpp:68:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i=0; i<balls.size(); i++) {
| ~^~~~~~~~~~~~~
Main.cpp:120:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int i=0; i<quers.size(); i++) {
| ~^~~~~~~~~~~~~
Main.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |