想到了从小到大依次填,但想到可能有重复元素,那是就会有分支,就不知怎样办了,最后才发现它是用随机数来调整排列,所以没有重复元素,唉。。。。。
1 /************************************************************** 2 Problem: 3671 3 User: idy002 4 Language: C++ 5 Result: Accepted 6 Time:39644 ms 7 Memory:196984 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <algorithm> 12 #define N 5010 13 using namespace std; 14 15 typedef long long dnt; 16 struct Pair { 17 short x, y; 18 int v; 19 bool operator<( const Pair &o ) const { return v<o.v; } 20 }; 21 22 int n, m, tot, q ; 23 int rgt[N], lft[N]; 24 Pair prs[N*N]; 25 int stk[N+N], top; 26 27 void prod_data() { 28 dnt x, a, b, c, d; 29 scanf( "%lld%lld%lld%lld%lld", &x, &a, &b, &c, &d ); 30 scanf( "%d%d%d", &n, &m, &q ); 31 tot = n*m; 32 for( int i=1; i<=tot; i++ ) 33 prs[i].v = i; 34 for( int i=1; i<=tot; i++ ) { 35 x = (a*x*x+b*x+c) % d; 36 swap( prs[i].v, prs[x%i+1].v ); 37 } 38 for( int i=1,l,r; i<=q; i++ ) { 39 scanf( "%d%d", &l, &r ); 40 swap( prs[l].v, prs[r].v ); 41 } 42 int cur = 1; 43 for( int i=1; i<=n; i++ ) { 44 for( int j=1; j<=m; j++ ) { 45 prs[cur].x = i; 46 prs[cur].y = j; 47 cur++; 48 } 49 } 50 sort( prs+1, prs+1+tot ); 51 } 52 int main() { 53 prod_data(); 54 for( int i=1; i<=n; i++ ) 55 lft[i]=1, rgt[i]=m; 56 top = 0; 57 for( int t=1; top<n+m-1; t++ ) { 58 int x=prs[t].x, y=prs[t].y; 59 if( y<lft[x] || y>rgt[x] ) continue; 60 for( int i=1; i<x; i++ ) 61 rgt[i] = min( rgt[i], y ); 62 for( int i=x+1; i<=n; i++ ) 63 lft[i] = max( lft[i], y ); 64 stk[++top] = prs[t].v; 65 } 66 sort( stk+1, stk+1+top ); 67 for( int i=1; i<=top; i++ ) 68 printf( "%d%s", stk[i], i==top ? "" : " " ); 69 }