zoukankan      html  css  js  c++  java
  • hihocoder-Week200-Shorteniring Sequence

    hihocoder-Week200-Shorteniring Sequence

    题目1 : Shortening Sequence

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    There is an integer array A1, A2 ...AN. Each round you may choose two adjacent integers. If their sum is an odd number, the two adjacent integers can be deleted.

    Can you work out the minimum length of the final array after elaborate deletions?

    输入

    The first line contains one integer N, indicating the length of the initial array.

    The second line contains N integers, indicating A1, A2 ...AN.

    For 30% of the data:1 ≤ N ≤ 10

    For 60% of the data:1 ≤ N ≤ 1000

    For 100% of the data:1 ≤ N ≤ 1000000, 0 ≤ Ai ≤ 1000000000

    输出

    One line with an integer indicating the minimum length of the final array.

    样例提示

    (1,2) (3,4) (4,5) are deleted.

    样例输入
    7
    1 1 2 3 4 4 5
    样例输出
    1

    简单的dp问题。

    已知长度为n的数组删除元素到最后,一定是两种类型,全是偶数,or 全是奇数。

    全是偶数,那么n+1位置添加进来一个奇数,则长度减一,否则加一。

    找出这条规律,可以进行dp迭代操作。

    #include <cstdio> 
    #include <cstdlib> 
    const int MAXN = 1000000 + 10; 
    
    #define fabs(a) (a)>0?(a):(-a) 
    
    int n, ans, num[MAXN], dp[MAXN]; 
    
    int main(){
    	freopen("in.txt", "r", stdin); 
    
    	while(scanf("%d", &n) != EOF)
    	{
    		for(int i=0; i<n; ++i )
    		{
    			scanf("%d", &num[i]); 
    		} 
    
    		for(int i=0; i<n; ++i)
    		{
    			if(i == 0)
    			{
    				if(num[i] %2 == 0)
    				{
    					dp[i] = 1; 
    				}
    				else
    				{
    					dp[i] = -1; 
    				}
    			}else{
    				if(num[i]%2 == 0)
    				{
    					dp[i] = dp[i-1] + 1; 
    				}
    				else
    				{
    					dp[i] = dp[i-1] - 1; 
    				}
    			}
    		}
    
    		ans = fabs( dp[n-1] ); 
    
    		printf("%d
    ",  ans );
    	}
    	return 0; 
    }
    

      

  • 相关阅读:
    面向对象进阶
    20191011作业
    2019.10.10作业
    类的继承
    面向对象
    2019.10.09作业
    pandas模块
    [BZOJ 2190][SDOI2008]仪仗队(欧拉函数)
    [BZOJ 2729][HNOI2012]排队(组合数学+高精)
    [BZOJ 1491][NOI2007]社交网络(Floyd)
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/8977063.html
Copyright © 2011-2022 走看看