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")
int INF = 1LL<<30;
int maxbloat = 1008;
signed main() {
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);
}
#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#endif //fast IO
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;
}
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 cta = counts[l];
int ctb = pref[r]-pref[l+1];
int ctc = counts[r];
int rem1 = totballs-ctb-ctc+1;
int rem2 = totballs-ctb-cta+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
while (q--) {
int s,g,t; cin >> s >> g >> t;
t-=totballs;
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) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
//cout << ans << endl;
}
}
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:46:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i=0; i<coords.size(); i++) {
| ~^~~~~~~~~~~~~~
Main.cpp:53:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int i=0; i<balls.size(); i++) {
| ~^~~~~~~~~~~~~
Main.cpp:16:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
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("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... |