QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#796867 | #7319. Order-Preserving Partition | ainta | WA | 80ms | 12136kb | C++23 | 6.1kb | 2024-12-02 08:06:49 | 2024-12-02 08:06:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rng(i,a,b) for(int i=int(a);i<=int(b);i++)
#define rep(i,b) rng(i,0,b-1)
#define gnr(i,b,a) for(int i=int(b);i>=int(a);i--)
#define per(i,b) gnr(i,b-1,0)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
typedef long long ll;
using pii=pair<int,int>;
using vi=vc<int>;
using uint=unsigned;
using ull=unsigned long long;
using pil=pair<int,ll>;
using pli=pair<ll,int>;
using pll=pair<ll,ll>;
using t3=tuple<int,int,int>;
ll rand_int(ll l, ll r) { //[l, r]
#ifdef LOCAL
static mt19937_64 gen;
#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
return uniform_int_distribution<ll>(l, r)(gen);
}
template <uint MD> struct ModInt {
using M = ModInt;
///const static M G;
uint v;
ModInt(ll _v = 0) { set_v(_v % MD + MD); }
M& set_v(uint _v) {
v = (_v < MD) ? _v : _v - MD;
return *this;
}
explicit operator bool() const { return v != 0; }
M operator-() const { return M() - *this; }
M operator+(const M& r) const { return M().set_v(v + r.v); }
M operator-(const M& r) const { return M().set_v(v + MD - r.v); }
M operator*(const M& r) const { return M().set_v(ull(v) * r.v % MD); }
M operator/(const M& r) const { return *this * r.inv(); }
M& operator+=(const M& r) { return *this = *this + r; }
M& operator-=(const M& r) { return *this = *this - r; }
M& operator*=(const M& r) { return *this = *this * r; }
M& operator/=(const M& r) { return *this = *this / r; }
bool operator==(const M& r) const { return v == r.v; }
M pow(ll n) const {
M x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
M inv() const { return pow(MD - 2); }
friend ostream& operator<<(ostream& os, const M& r) { return os << r.v; }
};
using Mint = ModInt<998244353>;
using t4 = tuple<int,int,int,int>;
using t5 = tuple<int,int,int,int,int>;
//template<> const Mint Mint::G = Mint(3);
#define N_ 1010000
const int SZ = (1<<20);
int n, w[N_], Loc[N_];
int Mn[N_], Mx[N_], UF[N_];
/*void Calc(int b, int e){
}*/
bool Block(int b, int e){
if(b>e||b<1||e>n)return false;
int Mn=n+1,Mx=0;
rng(i,b,e)Mx=max(Mx,w[i]),Mn=min(Mn,w[i]);
return Mx-Mn==e-b;
}
int Find(int a){
if(a==UF[a])return a;
return UF[a]=Find(UF[a]);
}
int GetA(int b, int e){
Mx[b]=w[b]; rng(i,b+1,e)Mx[i]=max(Mx[i-1],w[i]);
Mn[e]=w[e]; gnr(i,e-1,b)Mn[i]=min(Mn[i+1],w[i]);
int s=0;
if(Mx[e]-Mn[b]==e-b){
rng(i,b,e-1)if(Mx[i]<Mn[i+1])s++;
}
return s;
}
int GetB(int b, int e){
Mn[b]=w[b]; rng(i,b+1,e)Mn[i]=min(Mn[i-1],w[i]);
Mx[e]=w[e]; gnr(i,e-1,b)Mx[i]=max(Mx[i+1],w[i]);
int s=0;
if(Mx[b]-Mn[e]==e-b){
rng(i,b,e-1)if(Mn[i]>Mx[i+1])s++;
}
return s;
}
void Solve(){
rng(i,1,n){
scanf("%d",&w[i]);
}
int P[4];
rep(i,4)scanf("%d",&P[i]);
ll res=0;
if(P[1]==1 || P[2]==1){
rng(i,1,n)w[i]=n+1-w[i];
rep(i,4)P[i]=5-P[i];
}
if(P[1]==4 || P[2]==4){
if(P[2]==4){
reverse(P,P+4);
reverse(w+1,w+n+1);
}
rng(i,1,n) Loc[w[i]]=i;
if(P[2]==1){
if(P[0]==2){//2413
int l = Loc[1], r = Loc[n];
int p1 = 1; while(p1<=n && w[p1] <= w[n])p1++;
int p2 = n; while(p2>=1 && w[p2] >= w[1])p2--;
if(p1 <= r && r < l && r <= p2){
int p3 = p1; while(p3<=n && w[p3]>=w[n])p3++;
if(Block(1,p1-1) && Block(p1,p3-1) && Block(p3,p2) && Block(p2+1,n))res++;
}
}
else{//3412
int l = Loc[1], r = Loc[n];
int p1 = r; while(p1<=n && w[p1] >= w[n])p1++;
//~p1-1 , p1~
if(r<p1 && p1<=l && Block(1,p1-1) && Block(p1,n)){
ll c1 = GetA(1,p1-1), c2 = GetA(p1,n);
res += c1*c2;
}
}
}
else if(P[3]==1){
if(P[0]==2){//2431
int p1 = n, r = Loc[n];
while(p1>=1 && w[p1]<=w[1])p1--;
if(r < p1){//[ ][ r ][ ,p1][p1+1,n]
int p2 = 1; while(p2<=n && w[p2]<=w[p1])p2++; //[1,p2-1]
if(p2<=r && Block(1,p2-1) && Block(p1+1,n) && Block(p2,p1)){
res = GetB(p2,p1);
}
}
}
else{ //3421
int p1 = n, r = Loc[n];
while(p1>=1 && w[p1]<=w[1])p1--;
if(r <= p1){//[ r ][p1+1,n]
if(Block(1,p1) && Block(p1+1,n)) res = GetA(1,p1)*GetB(p1+1,n);
}
}
}
else{
if(P[2]==2){//1423
int p1 = 1; while(p1<=n && w[p1]<=w[n])p1++;
int p2 = n; while(p2>=1 && w[p2]!=p1)p2--;
while(p2>=1 && w[p2] < w[n])p2--;
//[1,p1-1] [p1, p2],
if(Block(1,p1-1) && Block(p1,p2) && p1 <= Loc[n] && Loc[n]<=p2) res = GetA(p2+1,n);
}
else{//1432
int p1 = 1; while(p1<=n && w[p1]<=w[n])p1++;
if(Block(1,p1-1)){
int Mx = 0, cur=p1-1, cc=0;
gnr(i,n,p1+1){
cur++;
Mx=max(Mx,w[i]);
if(Mx == cur){
res+=cc;
cc++;
}
}
}
}
}
}
else{
if(P[0]==4){
rng(i,1,n)w[i]=n+1-w[i];
rep(i,4)P[i]=5-P[i];
}
rng(i,1,n) Loc[w[i]]=i;
//rep(i,4)printf("%d ",P[i]);
assert(P[0]==1 && P[3]==4);
rng(i,1,n){
UF[i]=i;
Mx[i]=max(Mx[i-1],w[i]);
}
Mn[n]=w[n];
gnr(i,n-1,1)Mn[i]=min(Mn[i+1],w[i]);
if(P[1]==3){ // 1324
int l =n, r=1;
rng(i,1,n){
int x = Loc[i];
UF[x]=x-1;
l=min(l,x);r=max(r,x);
int z = Find(r);
int c2 = r-z;
if(l==1 && z>1){
int c1 = i-c2;
if(Mx[c1] == c1 && Mn[r+1] == r+1)res++;
}
}
}
else{
ll c=0;
rng(i,1,n-1){
if(Mx[i]==i){
res += c*(c-1)/2;
c++;
}
}
}
}
printf("%lld\n",res);
}
int main(){
/* vi T(4);
rep(i,4)T[i]=i;
do{
vi Z(5);
rep(i,5)Z[i]=i;
do{
printf("%d\n",5);
rep(i,5)printf("%d ",Z[i]+1);
puts("");
rep(i,4)printf("%d ",T[i]+1);
puts("");
}while(next_permutation(all(Z)));
}while(next_permutation(all(T)));
return 0;*/
while(scanf("%d",&n)==1){
Solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 9952kb
input:
10 2 1 4 3 10 9 8 7 5 6 2 4 1 3 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4
output:
0 84
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 75ms
memory: 10008kb
input:
10 4 1 8 2 3 10 9 7 5 6 1 2 3 4 10 1 2 7 10 6 4 3 9 8 5 1 2 3 4 10 7 8 4 6 1 3 2 9 10 5 1 2 3 4 10 6 5 1 10 7 8 9 3 4 2 1 2 3 4 10 8 7 10 6 3 5 9 4 2 1 1 2 3 4 10 9 4 5 8 7 3 10 6 1 2 1 2 3 4 10 4 7 6 10 1 3 9 8 5 2 1 2 3 4 10 1 10 5 4 6 7 9 3 2 8 1 2 3 4 10 4 8 2 5 1 3 10 7 9 6 1 2 3 4 10 2 8 4 7 6...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 100000 lines
Test #3:
score: 0
Accepted
time: 67ms
memory: 12124kb
input:
10 5 10 1 9 4 8 3 7 6 2 1 2 4 3 10 1 7 4 8 9 5 10 2 6 3 1 2 4 3 10 3 4 6 5 9 7 10 1 8 2 1 2 4 3 10 8 3 4 7 5 10 9 2 6 1 1 2 4 3 10 3 5 10 6 2 8 9 7 4 1 1 2 4 3 10 2 1 8 4 9 10 7 6 3 5 1 2 4 3 10 6 5 4 8 2 1 7 3 10 9 1 2 4 3 10 2 8 1 10 5 3 9 6 4 7 1 2 4 3 10 3 6 5 4 10 9 1 2 8 7 1 2 4 3 10 3 4 9 1 6...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 100000 lines
Test #4:
score: 0
Accepted
time: 80ms
memory: 10004kb
input:
10 9 1 3 5 8 6 2 10 4 7 1 3 2 4 10 4 1 7 2 9 8 10 5 6 3 1 3 2 4 10 1 2 7 9 10 3 6 5 8 4 1 3 2 4 10 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0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 100000 lines
Test #6:
score: 0
Accepted
time: 67ms
memory: 12108kb
input:
10 9 8 6 10 5 3 7 1 2 4 1 4 2 3 10 9 2 6 10 5 8 7 1 4 3 1 4 2 3 10 8 6 9 7 2 10 4 3 5 1 1 4 2 3 10 2 1 10 7 4 3 6 8 9 5 1 4 2 3 10 9 10 4 6 5 2 3 8 1 7 1 4 2 3 10 3 1 5 8 4 2 7 9 6 10 1 4 2 3 10 5 3 9 7 2 10 4 6 1 8 1 4 2 3 10 7 2 1 6 3 10 4 8 5 9 1 4 2 3 10 9 2 5 8 10 6 7 4 3 1 1 4 2 3 10 9 5 6 8 2...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 100000 lines
Test #7:
score: -100
Wrong Answer
time: 60ms
memory: 8076kb
input:
10 2 7 5 6 3 8 9 4 1 10 1 4 3 2 10 5 8 4 3 1 2 10 6 7 9 1 4 3 2 10 6 5 8 10 3 1 4 7 9 2 1 4 3 2 10 9 2 8 3 10 4 1 7 6 5 1 4 3 2 10 10 5 9 6 8 7 2 4 3 1 1 4 3 2 10 7 9 8 1 2 5 4 6 10 3 1 4 3 2 10 10 8 2 6 9 3 4 5 1 7 1 4 3 2 10 6 4 2 3 1 7 9 8 10 5 1 4 3 2 10 1 7 10 6 2 3 9 8 5 4 1 4 3 2 10 8 5 6 9 2...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 86th lines differ - expected: '0', found: '1'