2 条题解
-
1Sky1231 (sky1231) LV 10 @ 2017-06-23 18:49:18
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <algorithm> #include <vector> #include <deque> #include <limits> #include <string> #include <sstream> using namespace std; int n; vector<int> l; vector<int> t; vector<int> ans_v; vector<vector<int> > a; vector<bool> v; bool cmp_1(int x,int y) { return x>y; } int find_1(int now) { for (int i=a[now].size()-1;i>=0;i--) if (v[a[now][i]]==0) { int temp=l[a[now][i]]; l[a[now][i]]=now,v[a[now][i]]=1; if (temp>=0) { if (find_1(temp)==1) { ans_v[now]=a[now][i]; return 1; } } else { ans_v[now]=a[now][i]; return 1; } l[a[now][i]]=temp; } return 0; } int work_1() { int ans=0; l.resize(0); l.resize(n,-1); ans_v.resize(0); ans_v.resize(n,0); for (int i=n-1;i>=0;i--) { v.resize(0); v.resize(n,0); ans+=find_1(i); } return ans; } int main() { while (~scanf("%d",&n)) { t.resize(0); t.resize(n,0); a.resize(0); a.resize(n); for (int i=0;i<n;i++) { scanf("%d",&t[i]); a[i].resize(0); int t_1=((i+t[i]<n)?(i+t[i]):(i+t[i]-n)),t_2=((i-t[i]>=0)?(i-t[i]):(i-t[i]+n)); a[i].push_back(t_1); a[i].push_back(t_2); sort(a[i].begin(),a[i].end(),cmp_1); } if (work_1()==n) { for (int i=0;i<n;i++) printf("%d ",ans_v[i]); printf("\n"); } else printf("No Answer\n"); } }
-
02014-03-03 14:32:45@
网络流+贪心找环水过:
编译成功测试数据 #0: Accepted, time = 15 ms, mem = 740 KiB, score = 10
测试数据 #1: Accepted, time = 15 ms, mem = 740 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 740 KiB, score = 10
测试数据 #3: Accepted, time = 15 ms, mem = 872 KiB, score = 10
测试数据 #4: Accepted, time = 7 ms, mem = 872 KiB, score = 10
测试数据 #5: Accepted, time = 15 ms, mem = 876 KiB, score = 10
测试数据 #6: Accepted, time = 249 ms, mem = 3864 KiB, score = 10
测试数据 #7: Accepted, time = 265 ms, mem = 3868 KiB, score = 10
测试数据 #8: Accepted, time = 265 ms, mem = 3872 KiB, score = 10
测试数据 #9: Accepted, time = 234 ms, mem = 3856 KiB, score = 10
Accepted, time = 1080 ms, mem = 3872 KiB, score = 100
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <bitset>using namespace std ;
#define inf 0x7fffffff
#define maxn 20010struct edge {
edge *next , *pair ;
int t , f ;
} *head[ maxn ] ;void Add( int s , int t , int f ) {
edge *p = new( edge ) ;
p -> t = t , p -> f = f , p -> next = head[ s ] ;
head[ s ] = p ;
}void AddEdge( int s , int t , int f ) {
Add( s , t , f ) , Add( t , s , 0 ) ;
head[ s ] -> pair = head[ t ] , head[ t ] -> pair = head[ s ] ;
}int h[ maxn ] , gap[ maxn ] , S , T ;
edge *d[ maxn ] ;int sap( int v , int flow ) {
if ( v == T ) return flow ;
int rec = 0 ;
for ( edge *p = d[ v ] ; p ; p = p -> next ) if ( p -> f && h[ v ] == h[ p -> t ] + 1 ) {
int ret = sap( p -> t , min( flow - rec , p -> f ) ) ;
p -> f -= ret , p -> pair -> f += ret , d[ v ] = p ;
if ( ( rec += ret ) == flow ) return flow ;
}
if ( ! ( -- gap[ h[ v ] ] ) ) h[ S ] = T ;
gap[ ++ h[ v ] ] ++ , d[ v ] = head[ v ] ;
return rec ;
}int maxflow( ) {
memset( gap , 0 , sizeof( gap ) ) ;
memset( h , 0 , sizeof( h ) ) ;
gap[ S ] = T ;
for ( int i = 0 ; i ++ < T ; ) d[ i ] = head[ i ] ;
int flow = 0 ;
for ( ; h[ S ] < T ; flow += sap( S , inf ) ) ;
return flow ;
}int n , ans[ maxn ] ;
bool flag ;
edge *P[ maxn ] ;
bitset < maxn > f , vi ;void dfs( int v , int u ) {
f[ v ] = true ;
for ( P[ v ] = head[ v ] ; P[ v ] ; P[ v ] = P[ v ] -> next ) if ( P[ v ] -> f && ! vi[ P[ v ] -> t ] ) {
if ( ! f[ P[ v ] -> t ] ) dfs( P[ v ] -> t , u ) ;
else if ( P[ v ] -> t == u ) flag = true ;
if ( flag ) {
P[ v ] -> f ^= 1 , P[ v ] -> pair -> f ^= 1 ;
break ;
}
}
f[ v ] = false , vi[ v ] = true ;
}int main( ) {
scanf( "%d" , &n ) ;
S = 2 * n + 1 ; T = S + 1 ;
memset( head , 0 , sizeof( head ) ) ;
for ( int i = 0 ; i ++ < n ; ) {
int x ; scanf( "%d" , &x ) ;
int l = i - x , r = i + x ;
if ( r > n ) r -= n ;
if ( l <= 0 ) l += n ;
AddEdge( S , i , 1 ) , AddEdge( n + i , T , 1 ) ;
AddEdge( i , n + l , 1 ) , AddEdge( i , n + r , 1 ) ;
}
if ( maxflow( ) == n ) {
f.reset( ) ;
for ( int i = 0 ; i ++ < n ; ) {
edge *x = head[ i ] , *y = head[ i ] -> next ;
if ( x -> t > y -> t ) swap( x , y ) ;
if ( ! x -> f ) {
x -> pair -> f = 0 , ans[ i ] = x -> t - n - 1 ;
} else {
flag = false ;
vi.reset( ) ; vi[ S ] = true ;
dfs( i , i ) ;
if ( flag ) {
ans[ i ] = x -> t - n - 1 , x -> pair -> f = 0 ;
} else {
ans[ i ] = y -> t - n - 1 , y -> pair -> f = 0 ;
}
}
}
for ( int i = 0 ; i ++ < n - 1 ; ) printf( "%d " , ans[ i ] ) ;
printf( "%d\n" , ans[ n ] ) ;
} else printf( "No Answer\n" ) ;
return 0 ;
}
- 1
信息
- ID
- 1820
- 难度
- 4
- 分类
- (无)
- 标签
- 递交数
- 133
- 已通过
- 58
- 通过率
- 44%
- 被复制
- 2
- 上传者