Thursday, February 16, 2017

LightOJ - 1217 - Neighbor House (II)

A soap company wants to advertise their product in a local area. In this area, there are n houses and the houses are placed in circular fashion, such that house 1 has two neighbors: house 2 and n. House 5 has two neighbors: house 4 and 6. House n has two neighbors, house n-1 and 1.
Now the soap company has an estimation of the number of soaps they can sell on each house. But for their advertising policy, if they sell soaps to a house, they can't sell soaps to its two neighboring houses. No your task is to find the maximum number of estimated soaps they can sell in that area.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing an integer n (2 ≤ n ≤ 1000). The next line contains n space separated integers, where the ith integer denotes the estimated number of soaps that can be sold to the ith house. Each of these integers will lie in the range [1, 1000].

Output

For each case, print the case number and the maximum number of estimated soaps that can be sold in that area.

Sample Input

Output for Sample Input

3
2
10 100
3
10 2 11
4
8 9 2 8
Case 1: 100
Case 2: 11
Case 3: 17


Solution:

  1. #include <bits/stdc++.h>
  2. #define pb push_back
  3. using namespace std;
  4. typedef long long LL;
  5. const int RNG = 1003;
  6. const int MOD = 1e9+7;
  7. const double pi = 2 * acos( 0.0 );
  8.  
  9. inline int _Int(){ int x; scanf( "%d"&); return x; }
  10. inline LL _LLi(){ LL x; scanf( "%lld"&); return x; }
  11. void pruts(){ puts( "NO" ); exit( EXIT_SUCCESS ); }
  12.  
  13. int dirX[] = { 10-101-11-1 };
  14. int dirY[] = { 010-11-1-11 };
  15. int rX[] = { 1122-1-1-2-2 };
  16. int rY[] = { 2-21-12-21-1 };
  17.  
  18. template < class T > T tri_Area( T x1, T y1, T x2, T y2, T x3, T y3 ){
  19.         return abs( x1*( y2-y3 ) - y1*( x2-x3 ) + ( x2*y3-x3*y2 ) );
  20. };
  21. template < class T > T Distance( T x1, T y1, T x2, T y2 ){
  22.         return sqrt( ( x1-x2 ) * ( x1-x2 ) + ( y1-y2 ) * ( y1-y2 ) );
  23. };
  24. template < class T > T bigMod( T n, T p, T m ){
  25.         if( p == 0 )return 1; if( p&1 )return ( n*bigMod( n, p-1, m ) )%m; T x = bigMod( n, p/2, m ); return ( x*)%m;
  26. };
  27. int Case = 0;
  28. int sset(int N, int pos){return N=N|(1<<pos);}
  29. bool check(int N, int pos){return (bool)(N&(1<<pos));}
  30. int reset(int N, int pos){return N=N&~(1<<pos);}
  31. /**************************************###########################################***************************************
  32. ***************************************##    MD. YA-SEEN ARAFAT(MuradTaakla)    ##***************************************
  33. ***************************************##    CSE, University of Asia Pacific    ##***************************************
  34. ***************************************###########################################***************************************/
  35.  
  36. int A[ RNG ], n;
  37. LL dp[ RNG ][ 2 ];
  38.  
  39. LL NII( int ind, bool Ok ){
  40.         if( Ok && ind == n-1 )return 0;
  41.         if( ind >= n )return 0;
  42.         LL &= dp[ ind ][ Ok ];
  43.         if( R != -1LL )return R;
  44.         R = 0LL;
  45.         R = max( NII( ind+1, Ok ), A[ ind ]+NII( ind+2, Ok|( !ind ) ) );
  46.         return R;
  47. }
  48.  
  49. void Hunger(){
  50.         int t = _Int();
  51.         while( t-- ){
  52.                 memset( dp, -1LL, sizeof( dp ) );
  53.                 n = _Int();
  54.                 for( int i = 0; i < n; i++ )A[ i ] = _Int();
  55.                 LL ans = NII( 00 );
  56.                 printf( "Case %d: %lld\n"++Case, ans );
  57.         }
  58. }
  59.  
  60. int main(){
  61.         #ifndef ONLINE_JUDGE
  62.         //        freopen("in.txt", "r", stdin);
  63.         //        freopen("n.txt", "w", stdout);
  64.         #endif
  65.         Hunger();
  66.         return 0;
  67. }

No comments:

Post a Comment