QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#110516 | #6542. Optimal Quadratic Function | hjroh0315 | TL | 4347ms | 4472kb | C++20 | 2.9kb | 2023-06-02 18:12:31 | 2023-06-02 18:12:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using lf=__float128;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef __float128 T; // long double, Rational, double + mod<P>...
typedef vector<T> vd;
typedef vector<vd> vvd;
lf abso(lf a){return a<0?-a:a;}
const T eps = lf(1)/1e18, inf = 1/.0;
#define MP make_pair
#define ltj(X) if(s == -1 || MP(X[j],N[j]) < MP(X[s],N[s])) s=j
struct LPSolver {
int m, n;
vi N, B;
vvd D;
LPSolver(const vvd& A, const vd& b, const vd& c) :
m(sz(b)), n(sz(c)), N(n+1), B(m), D(m+2, vd(n+2)) {
rep(i,0,m) rep(j,0,n) D[i][j] = A[i][j];
rep(i,0,m) { B[i] = n+i; D[i][n] = -1; D[i][n+1] = b[i];}
rep(j,0,n) { N[j] = j; D[m][j] = -c[j]; }
N[n] = -1; D[m+1][n] = 1;
}
void pivot(int r, int s) {
T *a = D[r].data(), inv = 1 / a[s];
rep(i,0,m+2) if (i != r && abso(D[i][s]) > eps) {
T *b = D[i].data(), inv2 = b[s] * inv;
rep(j,0,n+2) b[j] -= a[j] * inv2;
b[s] = a[s] * inv2;
}
rep(j,0,n+2) if (j != s) D[r][j] *= inv;
rep(i,0,m+2) if (i != r) D[i][s] *= -inv;
D[r][s] = inv;
swap(B[r], N[s]);
}
bool simplex(int phase) {
int x = m + phase - 1;
for (;;) {
int s = -1;
rep(j,0,n+1) if (N[j] != -phase) ltj(D[x]);
if (D[x][s] >= -eps) return true;
int r = -1;
rep(i,0,m) {
if (D[i][s] <= eps) continue;
if (r == -1 || MP(D[i][n+1] / D[i][s], B[i])
< MP(D[r][n+1] / D[r][s], B[r])) r = i;
}
if (r == -1) return false;
pivot(r, s);
}
}
T solve(vd &x) {
int r = 0;
rep(i,1,m) if (D[i][n+1] < D[r][n+1]) r = i;
if (D[r][n+1] < -eps) {
pivot(r, n);
if (!simplex(2) || D[m+1][n+1] < -eps) return -inf;
rep(i,0,m) if (B[i] == -1) {
int s = 0;
rep(j,1,n+1) ltj(D[i]);
pivot(i, s);
}
}
bool ok = simplex(1); x = vd(n);
rep(i,0,m) if (B[i] < n) x[B[i]] = D[i][n+1];
return ok ? D[m][n+1] : inf;
}
};
int main()
{
cin.tie(0)->sync_with_stdio(0);
// simplex with 7 dimensions, each being r, a1, a2, b1, b2, c1, c2 (1 and 2 exist due to bypassing nonnegativity constraints)
// max -r
// s.t. -r - a1*x^2 + a2*x^2 - b1*x + b2*x - c1 + c2 <= -y
// -r + a1*x^2 - a2*x^2 + b1*x - b2*x + c1 - c2 <= y
vector<lf>c{-1,0,0,0,0,0,0};
int q;cin>>q;
while(q--)
{
vector<vector<lf>>A;
vector<lf>B;
vector<lf>x;
int n;cin>>n;
while(n--)
{
double x,y;
cin>>x>>y;
B.push_back(-y);
A.push_back({-1,-x*x,x*x,-x,x,-1,1});
B.push_back(y);
A.push_back({-1,x*x,-x*x,x,-x,1,-1});
}
lf ans=LPSolver(A,B,c).solve(x);
cout<<setprecision(12)<<fixed<<abs(double(ans*ans))<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3684kb
input:
1 4 0 0 1 3 2 9 3 0
output:
5.062500000000
result:
ok found '5.0625000', expected '5.0625000', error '0.0000000'
Test #2:
score: 0
Accepted
time: 186ms
memory: 4472kb
input:
60 1 1000 -990 2 171 -638 949 -99 2 633 227 -257 -602 3 634 -994 633 999 -374 995 3 445 -110 586 -121 462 29 9 -995 -224 -458 -833 691 -670 456 -259 -376 55 -563 -12 834 827 -826 -220 299 744 17 997 991 997 976 997 988 998 -986 999 -982 999 -980 999 -996 998 -988 998 -991 997 987 1000 996 999 -1000 ...
output:
0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 543160.125996398157 121.000000000000 0.832006181212 412780.607179428393 12.250000000000 15750.250000000000 118751.380086098419 880245.505473519559 1.000000000000 15.363373360329 854986.713164986693 20.250000000000 24567.66738...
result:
ok 60 numbers
Test #3:
score: 0
Accepted
time: 5ms
memory: 3740kb
input:
1000 1 -585478 527569 1 152984 679945 1 -174472 172630 1 235983 471538 1 -250372 650998 1 521028 -109032 1 121457 989514 1 916700 -223410 1 25908 939341 1 999032 369442 1 249207 -874185 1 -921949 719467 1 -692065 -756006 1 580461 644861 1 -382986 975568 1 644060 -113069 1 -588888 717169 1 2947 -3929...
output:
0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 ...
result:
ok 1000 numbers
Test #4:
score: 0
Accepted
time: 13ms
memory: 3736kb
input:
1000 2 578578 -462573 -614596 -50411 2 568651 926188 -15389 -281674 2 -56242 -213116 215036 310015 2 -568452 -743741 -314862 -573269 2 -428037 -926383 -172945 -31965 2 -58020 145819 -69585 116311 2 -629887 -794837 704590 -761914 2 243217 -433618 98814 -457689 2 147490 681479 665176 -119632 2 -851707...
output:
0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 ...
result:
ok 1000 numbers
Test #5:
score: 0
Accepted
time: 28ms
memory: 3604kb
input:
1000 3 -734917 -489090 419510 102330 712427 633246 3 36286 -456156 747264 -743132 260371 -674274 3 429263 14588 352092 -105774 547767 232534 3 -913665 328259 240305 -680653 -295994 -678964 3 597443 -368402 -231672 43641 -590555 396735 3 -603016 904082 -607928 649743 464117 526551 3 350193 -351624 33...
output:
0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 0.000000000000 ...
result:
ok 1000 numbers
Test #6:
score: 0
Accepted
time: 45ms
memory: 3688kb
input:
1000 4 -48411 -514672 165369 -349660 -281244 -842990 50473 -422110 4 -487482 -318709 861670 -709796 -491931 -335068 -523699 -455262 4 -817575 -338873 869501 905839 -717462 -668516 841972 769497 4 530706 615905 128991 -871809 82920 -948448 -317630 -725769 4 144451 772470 -923425 791489 513030 193835 ...
output:
0.001635252927 0.441888893679 0.095087558884 3655702.651955105364 769858.665807108628 876.964418129029 2.383378035147 0.003314331357 0.046074289956 0.414087095747 31534.752837082106 0.011533310762 1322109.793439251138 2475705.141855127644 5494047.076382468455 0.604758111540 36615.982224410414 15433....
result:
ok 1000 numbers
Test #7:
score: 0
Accepted
time: 56ms
memory: 3616kb
input:
1000 5 -425128 981633 -381689 946206 957441 996145 84010 712860 -8814 738024 5 235841 662950 864929 -477349 823444 -108225 714735 661226 300163 983888 5 -972539 106077 -856485 556296 -951397 193386 -207377 778279 -862794 555900 5 -877483 818676 537271 -193411 341352 408858 167065 819835 451709 87895...
output:
162.217423560921 24899.703037345877 111166879.556401953101 440.668043947199 45502620.026469051838 0.908165720214 48119370.938288599253 1331743.107360073598 1145.041044423147 11911.409089635788 898995.931063926313 0.281486398633 7.835265455787 0.770599173806 1167.305707531013 40318098119.960334777832...
result:
ok 1000 numbers
Test #8:
score: 0
Accepted
time: 116ms
memory: 3640kb
input:
1000 10 860001 272235 -30508 220967 711207 504388 77794 647164 303746 959200 592742 534104 57277 254211 266565 968002 919148 568676 991753 -20796 10 95213 204518 35283 198770 69842 203724 -316246 248661 -319918 245804 -923990 767251 -689125 503455 175418 229272 90053 206083 -815528 637145 10 -808164...
output:
52855287328.797470092773 4736213.545027937740 325.276604038309 11692527.619325693697 61306.467095067797 24947264304.330532073975 492734951022.492126464844 0.965345707591 1913.495504072973 385.474663045163 6.448445177198 42078421804.716278076172 5867468.015624117106 0.606672064838 24254772.5148242600...
result:
ok 1000 numbers
Test #9:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
1 4 -1000000 -1000000 -999999 1000000 999999 1000000 1000000 -1000000
output:
0.000000000000
result:
ok found '0.0000000', expected '0.0000000', error '-0.0000000'
Test #10:
score: 0
Accepted
time: 2ms
memory: 3756kb
input:
1 4 -1000000 -1000000 -999999 1000000 -999998 1000000 -999997 -100000
output:
12656250000.000000000000
result:
ok found '12656250000.0000000', expected '12656250000.0000000', error '0.0000000'
Test #11:
score: 0
Accepted
time: 2ms
memory: 3612kb
input:
1 4 -1000000 -1000000 -999999 1000000 -999998 1000000 -999997 -1000000
output:
0.000000000000
result:
ok found '0.0000000', expected '0.0000000', error '-0.0000000'
Test #12:
score: 0
Accepted
time: 2ms
memory: 3748kb
input:
1 4 -1000000 -300000 -999999 300000 -999998 300000 -999997 -300000
output:
0.000000000000
result:
ok found '0.0000000', expected '0.0000000', error '-0.0000000'
Test #13:
score: 0
Accepted
time: 2ms
memory: 3600kb
input:
1 4 -1000000 -999999 -999999 999998 -999998 999996 -999997 -999999
output:
0.562500000000
result:
ok found '0.5625000', expected '0.5625000', error '0.0000000'
Test #14:
score: 0
Accepted
time: 2ms
memory: 3604kb
input:
8 4 -1000000 -1000000 -999999 1000000 999999 999977 1000000 -1000000 4 -1000000 -1000000 -999999 1000000 999999 999770 1000000 -1000000 4 -1000000 -1000000 -999999 1000000 999999 997770 1000000 -1000000 4 -1000000 -1000000 -999999 1000000 999999 977770 1000000 -1000000 4 -1000000 -1000000 -999999 10...
output:
33.062533062525 3306.253306252480 310806.560806483089 30885837.135829415172 62499875000.000000000000 561097185.448957085609 0.563062922156 2770157895.491001129150
result:
ok 8 numbers
Test #15:
score: 0
Accepted
time: 10ms
memory: 3836kb
input:
50 20 -78760 901241 -290160 346799 -100100 886312 -400033 -7842 -128289 858428 -443380 -236792 -204313 613533 870820 96059 812309 226162 -35539 980448 797663 345545 -445875 -256648 -460410 -299719 627726 793426 832862 169452 656272 795052 -339551 196857 -34433 992148 -388395 11457 -255059 482328 20 ...
output:
1995515551.235050678253 81676587.162761464715 15097.239959080309 23.395512429438 579359934.002431511879 3853.663650178120 50.381382962813 2611489.365440987982 131464690.062370106578 2137.820036269479 284.143965129583 564775635.752647280693 39822891364.848457336426 909.273877028092 16588.063569248843...
result:
ok 50 numbers
Test #16:
score: 0
Accepted
time: 4347ms
memory: 4048kb
input:
500 300 -574218 -271807 -443150 -83950 15479 867073 -467689 -121944 -318587 129168 -24306 766466 -968754 -612160 -814705 -519500 -60831 677156 -195474 372912 -44244 717366 -134450 505915 -523893 -204101 -179966 405956 -732527 -448979 -886997 -569400 -190507 383431 -538163 -223837 -885831 -568677 -60...
output:
223.573986308187 11176.342872447976 1192.744752752307 453187006.523357152939 554031869.361881732941 9.206159011827 126.713163644150 2.791225143125 7790357819.290426254272 13298746917.732570648193 138873066385.756835937500 4982989809.283447265625 67327474.877917289734 4313.350012823093 2045627.963990...
result:
ok 500 numbers
Test #17:
score: -100
Time Limit Exceeded
input:
2 100000 856014 -110712 -748941 799207 -390374 -391739 448342 -991095 -64136 -981770 583018 -785726 -94728 -935377 768587 -365471 -102072 -963217 -547043 88834 -57865 -990529 -569447 175470 -331771 -501999 -123570 -924764 -86739 -946110 -481573 -114452 -143293 -909698 -188793 -835029 -368557 -415082...