Zoj1028-Flip and Shift

Source: Internet
Author: User

Q: there are black and white balls in different colors. Each ball can jump two cells each time. Can you move the ball of the same color to a continuous position? analysis: a. When the total number of balls is an odd number, the ball can be moved to A satisfactory position no matter how it moves. B. When the total number of balls is an even number, when the difference between a ball in a color of an odd or even number is less than 2, the ball can also be moved to a satisfied position. Otherwise, the ball cannot.

 #include<iostream>   #include<cstdio>   #include<cstring>   #include<cmath>   #include<algorithm>   #include<bitset>     using namespace std;    int main()  {      int Case , flag , n , num[ 50 ] , eve , odd ;      cin >> Case ;      while( Case-- )      {          cin >> n ;          eve = odd = 0 ;          for( int i = 1 ; i <= n ; ++i )          {              cin >> num[ i ] ;              if( i % 2 == 0  )                  eve += num[ i ] ;              else                  odd += num[ i ] ;          }          if( n % 2 == 1  )              flag = 1 ;          else if( odd == eve || abs( odd - eve ) == 1 )              flag = 1 ;          else              flag = 0 ;          if( flag )              cout << "YES" << endl ;          else               cout << "NO" <<endl ;      }              return 0 ;  }  #include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<bitset>using namespace std;int main(){int Case , flag , n , num[ 50 ] , eve , odd ;cin >> Case ;while( Case-- ){cin >> n ;eve = odd = 0 ;for( int i = 1 ; i <= n ; ++i ){cin >> num[ i ] ;if( i % 2 == 0  )eve += num[ i ] ;elseodd += num[ i ] ;}if( n % 2 == 1  )flag = 1 ;else if( odd == eve || abs( odd - eve ) == 1 )flag = 1 ;elseflag = 0 ;if( flag )cout << "YES" << endl ;else cout << "NO" <<endl ;} return 0 ;}

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.