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; 
    }
    

      

  • 相关阅读:
    float转varchar
    我的优化经验:内链是SEO的基础
    转:2008年微软Windows硬件工程(WinHEC)大会
    sql语句去掉前面的0(前导零,零前缀)
    去掉ID重复的数据
    蛙蛙推荐:蛙蛙牌firefox插件
    每日阅读20081127
    网赚经验之谈:成为高手之路
    (chinaz)巧妙选购付费链接
    把某个表的数据导出成insert语句(数据导出 insert语句)
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/8977063.html
Copyright © 2011-2022 走看看