#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<set> usingnamespace std; #define MAXN 5006 int n; int A[MAXN], B[MAXN], res[MAXN]; multiset<int> S; intmain() <% scanf("%d", &n); for( int i = 1 ; i <= n ; ++ i ) scanf("%d",& A[i]); for( int i = 1 ; i <= n ; ++ i ) scanf("%d",& B[i]); for (int i = 1; i <= n; i++) S.insert(B[i]); for (int i = n; i; i--) { auto it = S.upper_bound(A[i]); it = ( it == S.end() ? S.begin() : it ); res[i] = *it; S.erase(it); } for (int i = 1; i <= n; i++) { int pos = -1; for (int j = i + 1; j <= n; j++) { if (res[i] >= res[j]) continue; bool fuck = 0 , f1 = A[i] < res[i] , f2 = A[j] < res[j]; fuck = ( ( !f1 ) and ( f2 && A[i] < res[j] ) ) or ( ( f1 ) and ( f2 && A[j] < res[i] ) ) or ( f1 and !f2 ) or ( !f1 and !f2 ); if ( fuck && (pos == -1 || res[pos] < res[j]) ) pos = j; } if (pos != -1) swap(res[i], res[pos]); } for( int i = 1 ; i <= n ; ++ i ) printf("%d ", res[i]); %>
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> usingnamespace std; #define int long long #define P 1000000007 #define MAXN 506 #define MAXK 1006 int n , k; int s[MAXN] , dir[MAXN]; longlong dp[2][MAXN][MAXK]; signedmain(){ cin >> n >> k; for( int i = 1 ; i <= n ; ++ i ) scanf("%lld",&s[i]); sort( s + 1 , s + 1 + n ); for( int i = 1 ; i <= n ; ++ i ) dir[i] = s[i] - s[i - 1]; dp[0][0][0] = 1; int cur = 0 , bac = 1; for( int i = 1 ; i <= n ; ++ i ) { cur ^= 1 , bac ^= 1; for( int j = 0 ; j <= i ; ++ j ) { for( int ss = 0 ; ss <= k ; ++ ss ) { int& x = dp[cur][j][ss]; x = 0; if( ss >= j * dir[i] ) x += 1ll * ( j + 1 ) * dp[bac][j][ss - j * dir[i]] , x %= P; if( ss >= ( j - 1 ) * dir[i] && j ) x += dp[bac][j - 1][ss - ( j - 1 ) * dir[i]] , x %= P; if( ss >= ( j + 1 ) * dir[i] ) x += 1ll * ( j + 1 ) * dp[bac][j + 1][ss - ( j + 1 ) * dir[i]] , x %= P; } } } int res = 0; for( int i = 0 ; i <= k ; ++ i ) res += dp[cur][0][i] , res %= P; cout << res << endl; }
int n; int head[MAXN] , to[MAXN << 1] , nex[MAXN << 1] , wto[MAXN << 1] , ecn; voidade( int u , int v , int w ){ to[++ecn] = v , wto[ecn] = w , nex[ecn] = head[u] , head[u] = ecn; } int S[MAXN]; voiddfs( int u , int fa , int wfa ){ S[u] = wfa; for( int i = head[u] ; i ; i = nex[i] ) { int v = to[i]; if( v == fa ) continue; S[u] ^= wto[i]; dfs( v , u , wto[i] ); } } int num[16]; int dp[1 << 16]; intJust_DOIT( int st ){ if( dp[st] != 0x3f3f3f3f ) return dp[st]; if( !st ) return0; for( int i = 1 ; i < 16 ; ++ i ) if( st & ( 1 << i ) ) { int curst = ( st ^ ( 1 << i ) ); dp[st] = min( dp[st] , Just_DOIT( curst ) + 1 ); for( int j = 1 ; j < 16 ; ++ j ) if( j != i && ( st & ( 1 << j ) ) ) { int cures = Just_DOIT( curst ^ ( 1 << j ) ^ ( 1 << ( j ^ i ) ) ) + ( ( st & ( 1 << ( j ^ i ) ) ) ? 2 : 1 ); dp[st] = min( dp[st] , cures ); } } return dp[st]; } intmain(){ // freopen("xor3.in","r",stdin); cin >> n; for( int i = 1 , u , v , w ; i < n ; ++ i ) { scanf("%d%d%d",&u,&v,&w); ade( u , v , w ) , ade( v , u , w ); } dfs( 1 , 1 , 0 ); for( int i = 2 ; i <= n ; ++ i ) ++ num[S[i]]; int res = 0; memset( dp , 0x3f , sizeof dp ); for( int i = 1 ; i < 16 ; ++ i ) res += num[i] / 2 , num[i] %= 2; int sta = 0; for( int i = 1 ; i < 16 ; ++ i ) if( num[i] ) sta |= ( 1 << i ); cout << res + Just_DOIT( sta ) << endl; }