QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#505123 | #9141. Array Spread | ucup-team2307# | WA | 2ms | 4024kb | C++20 | 10.2kb | 2024-08-04 20:15:27 | 2024-08-04 20:15:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define fi first
#define se second
#define pb push_back
typedef long double ld;
#define double ld
const int N = 6;
const int M = 10;
int a[100];
vector<pair<int, int> > q;
double brute(int i, int n)
{
if (i == n+1)
{
int mx = 0;
int mn = 1e9;
for (auto [l, r] : q)
{
int s = 0;
for (int i=l; i<=r; i++)
s += a[i];
mx = max(mx, s);
mn = min(mn, s);
}
if (mn == 0)
return 1e9;
return mx*1.0/mn;
}
double ans = 1e9;
for (a[i]=0; a[i]<=M; a[i]++)
ans = min(ans, brute(i+1, n));
return ans;
}
double brute2(int n)
{
double ans = 1;
int m = q.size();
for (int i=1; i<(1<<m); i++)
for (int j=1; j<(1<<m); j++)
if ((i&j)==0)
{
vector<int> a(n+1, 0);
int c1 = 0;
int c2 = 0;
for (int r=0; r<m; r++)
if ((j>>r)&1)
{
c2++;
for (int t=q[r].fi; t<=q[r].se; t++)
a[t]--;
}
bool ok = true;
for (int i=1; i<=n; i++)
if (a[i] < -2)
ok = false;
// int mn = 1e9;
// int mx = -1;
// for (int i=1; i<=n; i++)
// if (a[i] == -2)
// mn = min(mn, i), mx = max(mx, i);
// if (mx != -1)
// for (int i=mn; i<=mx; i++)
// a[i] = -2;
for (int r=0; r<m; r++)
if ((i>>r)&1)
{
for (int t=q[r].fi; t<=q[r].se; t++)
a[t]++;
c1++;
}
for (int i=1; i<=n; i++)
if (a[i] < 0)
ok = false;
if (ok)
ans = max<double>(ans, c2*1.0/c1);
}
return ans;
}
/*
2 2
3 3
4 4
5 5
6 6
1 2
2 3
3 4
4 5
5 6
1 4
2 5
3 6
1 1
2 2
3 3
4 4
5 5
6 6
1 2
2 3
3 4
4 5
*/
int cover[12060];
int fit[12060];
int fit1[12060];
void relax(vector<pair<int, int> >& v)
{
vector<pair<int, int> > u;
for (auto [l, r] : v)
if (u.size() == 0 || u.back().se < r)
u.pb({l, r});
v = u;
}
const int mod = 998244353;
int bpow(int a, int b)
{
int r = 1;
while (b > 0)
{
if (b&1)
r = r*a%mod;
a = a*a%mod;
b/=2;
}
return r;
}
int ANS1=0;
vector<int> ANS;
double ask(int n, vector<pair<int, int> >& v, bool FIRST = true)
{
// cout<<v.size()<<endl;
// for (auto [l, r] : v)
// cout<<l<<" "<<r<<endl;
// cout<<endl;
if (v.empty())
return 0;
vector<pair<int, int> > u;
int a = 0;
if (FIRST)
{
relax(v);
for (auto [l, r] : v)
{
int c = 0;
while (true)
{
if (fit[l] > r+1)
break;
l = fit[l];
c++;
}
if (c < a)
continue;
if (c > a)
{
a = c, u.clear();
}
if (l<=r)
u.pb({l, r});
}
for (auto [l, r] : v)
{
int c = 0;
while (true)
{
if (fit1[r+1] < l-1)
break;
r = fit1[r+1];
c++;
}
if (c < a)
continue;
if (c > a)
{
a = c, u.clear();
}
if (l<=r)
u.pb({l, r});
}
// for (auto [l, r] : v)
// cout<<l<<" "<<r<<" ";
// cout<<" -> "<<a<<"\n";
if (a > 0)
{
ANS1 = a;
sort(u.begin(), u.end());
relax(u);
// cout<<v.size()<<" -> "<<u.size()<<endl;
double x = ask(n, u, false);
// cout<<x<<"\n";
// s down, xs up
// 1 down, a up
return a+x;
}
}
// return 0;
vector<vector<int> > add(n+2);
vector<vector<int> > rem(n+2);
for (auto [l, r] : v)
{
add[l].pb(r+1);
rem[r+1].pb(r+1);
}
a = 1e9;
u.clear();
multiset<int> ms;
for (int i=0; i<n; i++)
{
for (int j : add[i])
ms.insert(j);
for (int j : rem[i])
ms.erase(ms.find(j));
if (ms.empty())
cover[i] = -1;
else
cover[i] = *prev(ms.end());
}
for (auto [l, r] : v)
{
// cout<<l<<" "<<r<<" ";
int R = fit[l]-1;
int c = 1;
while (r < R)
{
r = cover[r+1]-1;
if (r == -1)
break;
c++;
}
if (r == -1)
continue;
// cout<<c<<"\n";
if (c > a)
continue;
if (c < a)
{
a = c;
u.clear();
}
if (r > R)
u.pb({R+1, r});
}
add.clear();
rem.clear();
add.resize(n+2);
rem.resize(n+2);
for (auto [l, r] : v)
{
add[r].pb(l-1);
rem[l-1].pb(l-1);
}
a = 1e9;
u.clear();
for (int i=n-1; i>=0; i--)
{
for (int j : add[i])
ms.insert(j);
for (int j : rem[i])
ms.erase(ms.find(j));
if (ms.empty())
cover[i] = 1e9;
else
cover[i] = *ms.begin();
}
for (auto [l, r] : v)
{
// cout<<l<<" "<<r<<" ";
int L = fit1[r+1]+1;
int c = 1;
while (l > L)
{
l = cover[l-1]+1;
if (l > 2*n)
break;
c++;
}
if (l > 2*n)
continue;
// cout<<c<<"\n";
if (c > a)
continue;
if (c < a)
{
a = c;
u.clear();
}
if (l < L)
u.pb({l, L-1});
}
if (a < 1e8)
{
sort(u.begin(), u.end());
relax(u);
double r = (1+ask(n, u))/a;
ANS.pb(a);
return r;
}
else
return 0;
}
double smart()
{
int C = 0;
map<int, int> mp;
{
vector<int> v;
for (auto [l, r] : q)
{
v.pb(l), v.pb(r), v.pb(l-1), v.pb(r+1);
v.pb(l+1), v.pb(r-1);
}
sort(v.begin(), v.end());
for (int i : v)
{
if (mp.count(i))
continue;
mp[i] = C++;
}
}
int n = C;
vector<pair<int, int> > v;
for (auto [l, r] : q)
{
int L = mp[l];
int R = mp[r];
v.pb({L, R});
}
for (int i=0; i<=n; i++)
fit[i] = 1e9;
for (int i=0; i<=n; i++)
fit1[i] = -1e9;
for (auto [l, r] : v)
for (int i=0; i<=l; i++)
fit[i] = min(fit[i], r+1);
for (auto [l, r] : v)
for (int i=r; i<n; i++)
fit1[i+1] = max(fit1[i+1], l-1);
// for(int i=0; i<n; i++)
// cout<<fit[i]<<" ";
// cout<<"\n";
sort(v.begin(), v.end());
return ask(n, v);
// for(int i=0; i<n; i++)
// cout<<cover[i]<<" ";
// cout<<"\n";
// for(int i=0; i<n; i++)
// cout<<fit[i]<<" ";
// cout<<"\n";
// double ans = 1;
// for (int i=0; i<n; i++)
// {
// int cup = 0;
// int cdown = 0;
// int rup = i;
// int rdown = i;
// while (true)
// {
//// cout<<i<<" "<<rdown<<" "<<rup<<" "<<cdown<<" "<<cup<<"\n";
// if (rup < rdown)
// rup = cover[rup], cup++;
// else
// rdown = fit[rdown], cdown++;
// if (rup < 0 || rdown > n)
// break;
// if (cdown > cup && rup >= rdown)
// ans = max(ans, cdown*1.0/cup);
// }
// }
//
// return ans;
}
mt19937 rng(time(0));
signed main()
{
// cout<<fixed<<setprecision(10);
// int iter = 0;
// while (true)
// {
// iter++;
// int n = rng()%3+4;
// int m = rng()%5+5;
//// int n = 5;
//// int m = rng()%5+1;
// q.clear();
// for (int i=0; i<m; i++)
// {
// int l = rng()%n+1;
// int r = rng()%n+1;
// if (l>r) swap(l, r);
// q.pb({l, r});
// }
//
// double x = smart();
// double y = brute(1, n);
//
// if (abs(x-y) < 1e-6)
// {
//// cout<<"@";
// }
// else
// {
// cout<<"\n";
// cout<<x<<" "<<y<<"\n";
// cout<<n<<" "<<m<<"\n";
// for (int i=0; i<m; i++)
// cout<<q[i].fi<<" "<<q[i].se<<"\n";
// exit(0);
// }
//
// if (iter%100==0)
// cout<<iter<<"\n";
// }
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
// cout<<bpow(2, mod-2)<<"\n";
// return 0;
int t;
cin>>t;
while (t--)
{
int n, m;
cin>>n>>m;
q.resize(m);
for (auto& [l, r] : q)
cin>>l>>r;
ANS1 = 0;
ANS.clear();
// cout<<smart()<<"\n";
// cout<<"ANS "<<ANS1<<" : ";
// for (int i : ANS) cout<<i<<" ";
// cout<<"\n";
smart();
int R = 0;
reverse(ANS.begin(), ANS.end());
if (ANS.size() > 0)
{
for (int i : ANS)
{
R = (R + 1)*bpow(i, mod-2)%mod;
}
}
cout<<(R+ANS1+mod)%mod<<"\n";
}
}
/*
1.0000000000 1.5000000000
4 8
1 3
3 4
2 4
2 3
1 2
1 1
2 4
1 3
1.0000000000 1.5000000000
4 5
1 2
3 4
2 4
1 3
2 3
4
3 3
1 3
2 3
1 2
12 6
2 3
5 7
1 9
4 8
1 2
7 11
4 5
3 4
2 3
1 2
4 4
1 1
6 7
1 3
1 4
2 4
2 5
3 5
3 6
4 6
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3724kb
input:
3 3 3 1 3 2 3 1 2 12 6 2 3 5 7 1 9 4 8 1 2 7 11 4 5 3 4 2 3 1 2 4 4 1 1
output:
1 2 499122178
result:
ok 3 number(s): "1 2 499122178"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3648kb
input:
2000 1000000000 1 259923446 367011266 1000000000 1 882434225 971573327 1000000000 1 41585677 470369580 1000000000 1 371902212 947250194 1000000000 1 787209148 924205796 1000000000 1 259074809 960876164 1000000000 1 148079314 188254573 1000000000 1 940091047 948318624 1000000000 1 40636497 743979446 ...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok 2000 numbers
Test #3:
score: 0
Accepted
time: 2ms
memory: 3952kb
input:
1000 1000000000 5 575330909 661595447 708422488 913945134 658050911 930246647 786571892 904549453 851755566 969150871 1000000000 2 198072104 844159589 8876188 644559580 1000000000 2 740802634 976972118 783909534 898449184 1000000000 2 871819537 941611957 465883854 640988372 1000000000 1 99458969 462...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 ...
result:
ok 1000 numbers
Test #4:
score: 0
Accepted
time: 2ms
memory: 3668kb
input:
500 1000000000 13 964546318 987364574 367845944 907446075 259314137 890312338 458318546 959971971 353677471 522446336 782931403 845199078 514387878 786979588 532634932 793056892 905393511 960628299 747423889 986373313 796099347 833069525 906969434 971335651 574582540 647534593 1000000000 6 987712893...
output:
3 1 3 1 1 1 1 1 1 3 2 1 1 1 3 1 2 1 1 2 1 3 1 1 1 2 1 2 2 1 1 1 1 1 1 1 3 1 1 1 1 2 1 1 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 1 3 1 2 1 1 1 1 2 3 1 1 1 1 1 1 1 3 2 1 3 2 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 3 1 1 1 1 1 1 1 2 1 1 2 1 1 1 2 1 4 1 2 1 4 1 3 1 1 1 1 1 2 1 1 4 1 ...
result:
ok 500 numbers
Test #5:
score: 0
Accepted
time: 2ms
memory: 4024kb
input:
250 1000000000 10 844342043 888135880 127033337 726074967 581308029 893912240 414276384 752837267 565680461 863374082 230362895 477723054 210479116 423381051 325072305 427826920 178306222 756423471 376470949 993759748 1000000000 2 468173597 607783582 266359996 863641680 1000000000 7 206599093 941381...
output:
2 1 2 1 3 3 1 1 1 2 1 2 2 1 3 5 2 1 1 1 2 1 2 1 3 1 2 1 3 499122178 1 1 1 1 3 1 1 1 3 3 3 1 4 1 1 1 1 1 1 1 1 5 1 4 2 1 3 1 1 1 2 5 2 1 2 6 2 2 1 2 1 1 1 5 8 2 1 2 1 1 2 2 2 1 1 5 8 3 1 1 1 8 2 6 1 1 4 2 1 1 1 1 2 2 1 2 1 1 1 1 1 1 2 1 2 1 1 4 1 1 3 1 2 3 3 2 5 1 1 1 3 2 1 1 1 3 1 1 2 1 1 1 1 3 1 1 ...
result:
ok 250 numbers
Test #6:
score: -100
Wrong Answer
time: 2ms
memory: 3996kb
input:
250 1000000000 4 10495745 465086423 465086424 609997778 396956207 663037010 253873206 396956206 1000000000 33 596279983 655818820 226461062 338625457 407323582 423049163 711408063 778512581 220274357 226461061 702491412 711408062 686978949 688730316 369564474 434159428 778512582 787885602 675683057 ...
output:
1 2 1 5 6 499122178 1 10 6 1 499122183 1 4 3 1 3 1 1 2 499122179 10 499122178 1 499122179 4 1 7 1 499122179 2 2 2 1 499122178 1 499122178 2 499122179 5 3 4 17 1 2 2 3 499122180 1 3 1 499122179 2 3 2 2 7 3 499122187 6 499122178 1 2 2 2 2 2 499122178 2 499122183 1 499122179 1 2 1 10 1 499122181 499122...
result:
wrong answer 3rd numbers differ - expected: '748683266', found: '1'