bzoj 3671 贪心

时间:2023-03-09 03:56:10
bzoj 3671 贪心

想到了从小到大依次填,但想到可能有重复元素,那是就会有分支,就不知怎样办了,最后才发现它是用随机数来调整排列,所以没有重复元素,唉。。。。。

 /**************************************************************
Problem: 3671
User: idy002
Language: C++
Result: Accepted
Time:39644 ms
Memory:196984 kb
****************************************************************/ #include <cstdio>
#include <algorithm>
#define N 5010
using namespace std; typedef long long dnt;
struct Pair {
short x, y;
int v;
bool operator<( const Pair &o ) const { return v<o.v; }
}; int n, m, tot, q ;
int rgt[N], lft[N];
Pair prs[N*N];
int stk[N+N], top; void prod_data() {
dnt x, a, b, c, d;
scanf( "%lld%lld%lld%lld%lld", &x, &a, &b, &c, &d );
scanf( "%d%d%d", &n, &m, &q );
tot = n*m;
for( int i=; i<=tot; i++ )
prs[i].v = i;
for( int i=; i<=tot; i++ ) {
x = (a*x*x+b*x+c) % d;
swap( prs[i].v, prs[x%i+].v );
}
for( int i=,l,r; i<=q; i++ ) {
scanf( "%d%d", &l, &r );
swap( prs[l].v, prs[r].v );
}
int cur = ;
for( int i=; i<=n; i++ ) {
for( int j=; j<=m; j++ ) {
prs[cur].x = i;
prs[cur].y = j;
cur++;
}
}
sort( prs+, prs++tot );
}
int main() {
prod_data();
for( int i=; i<=n; i++ )
lft[i]=, rgt[i]=m;
top = ;
for( int t=; top<n+m-; t++ ) {
int x=prs[t].x, y=prs[t].y;
if( y<lft[x] || y>rgt[x] ) continue;
for( int i=; i<x; i++ )
rgt[i] = min( rgt[i], y );
for( int i=x+; i<=n; i++ )
lft[i] = max( lft[i], y );
stk[++top] = prs[t].v;
}
sort( stk+, stk++top );
for( int i=; i<=top; i++ )
printf( "%d%s", stk[i], i==top ? "" : " " );
}