CodeForce 339:A+B+C
A题:水题。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 const int maxn = 1005; 4 char s[ maxn ]; 5 int main(){ 6 //freopen("in.txt","r",stdin); 7 while( scanf("%s",s)!=EOF ){ 8 int len = strlen(s); 9 int c1 = 0,c2 = 0,c3 = 0; 10 for( int i=0;i<len;i++ ){ 11 if( s[i]=='1' ) c1++; 12 if( s[i]=='2' ) c2++; 13 if( s[i]=='3' ) c3++; 14 } 15 int sum = c1+c2+c3; 16 if( sum==1 ){ 17 printf("%s\n",s); 18 continue; 19 } 20 for( int i=0;i<sum;i++ ){ 21 if( i==0 ){ 22 if( c1 ) printf("1"),c1--; 23 else if( c2 ) printf("2"),c2--; 24 else if( c3 ) printf("3"),c3--; 25 } 26 else{ 27 if( c1 ) printf("+1"),c1--; 28 else if( c2 ) printf("+2"),c2--; 29 else if( c3 ) printf("+3"),c3--; 30 } 31 } 32 printf("\n"); 33 } 34 return 0; 35 }
B题:还是水题。。模拟。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 const int maxn = 100005; 3 typedef long long LL; 4 LL a[ maxn ]; 5 int main(){ 6 int n,m; 7 //freopen("in.txt","r",stdin); 8 while( scanf("%d%d",&n,&m)==2 ){ 9 for( int i=1;i<=m;i++ ){ 10 scanf("%I64d",&a[i]); 11 } 12 LL sum = 0; 13 for( int i=1;i<=m;i++ ){ 14 if( i==1 ){ 15 sum += a[i]-1; 16 } 17 else { 18 if( a[i]>=a[i-1] ) sum += (a[i]-a[i-1]); 19 else sum += (n+a[i]-a[i-1]); 20 } 21 } 22 printf("%I64d\n",sum); 23 } 24 return 0; 25 }
C题:简单的DFS,满足两个条件即可。。。
题意别理解错!!!!!!!!!!!!!!!!!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<string.h> 3 const int maxn = 1005; 4 int ans[ maxn ]; 5 //int cnt ; 6 char str[ maxn ]; 7 int m; 8 bool ok; 9 10 void dfs( int L,int R,int sL,int sR,int cnt ){ 11 if( cnt==m&&ok==false ){ 12 ok = true; 13 return ; 14 } 15 if( ok==true ) return ; 16 for( int i=0;str[i]!='\0';i++ ){ 17 if( str[i]=='1' ){ 18 if( cnt%2==0&&((i+1)!=R)&&(sL+i+1>sR) ){ 19 ans[ cnt ] = i+1; 20 dfs( i+1,R,sL+i+1,sR,cnt+1 ); 21 } 22 if( ok==true ) return ; 23 if( cnt%2==1&&((i+1)!=L)&&(sR+i+1>sL) ){ 24 ans[ cnt ] = i+1; 25 dfs( L,i+1,sL,sR+i+1,cnt+1 ); 26 } 27 if( ok==true ) return ; 28 } 29 } 30 return; 31 } 32 33 34 int main(){ 35 //freopen("in.txt","r",stdin); 36 scanf("%s%d",str,&m); 37 ok = false; 38 if( m==1 ){ 39 for( int i=0;str[i]!='\0';i++ ){ 40 if( str[i]=='1' ){ 41 ok = true; 42 printf("YES\n%d\n",i+1); 43 break; 44 } 45 } 46 if( ok==false ) printf("NO\n"); 47 return 0; 48 } 49 ok = false; 50 dfs( 0,0,0,0,0 ); 51 if( ok==true ){ 52 puts("YES"); 53 for( int i=0;i<m;i++ ){ 54 if( i==0 ) printf("%d",ans[i]); 55 else printf(" %d",ans[i]); 56 } 57 printf("\n"); 58 } 59 else puts("NO"); 60 61 return 0; 62 }
keep moving...