zoukankan      html  css  js  c++  java
  • Cutting Codeforces Round #493 (Div. 2)

     

    Cutting

    There are a lot of things which could be cut — trees, paper, “the rope”. In this problem you are going to cut a sequence of integers.

    There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.

    Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4,1,2,3,4,5,4,4,5,5]
    → two cuts → [4,1|2,3,4,5|4,4,5,5]

    . On each segment the number of even elements should be equal to the number of odd elements.

    The cost of the cut between x
    and y numbers is |x−y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.

    Input

    First line of the input contains an integer n(2≤n≤100) and an integer B (1≤B≤100) — the number of elements in the sequence and the number of bitcoins you have.
    Second line contains n integers: a1, a2, …, an (1≤ai≤100) — elements of the sequence, which contains the equal number of even and odd numbers

    Output

    Print the maximum possible number of cuts which can be made while spending no more than Bbitcoins.
    Input

    6 4
    1 2 5 10 15 20

    Output

    1

    Input

    4 10
    1 3 2 4

    Output

    0

    Input

    6 100
    1 2 3 4 5 6

    Output

    2
    123456789101112131415161718192021222324
    一道很简单的暴力题,但是一直没有抓住关键。
    问题的性质:每个切点之间没有关系,即某个切点切还是不切不影响其他的切点,需要看出来他们之间的不关联性。
    附ac码

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int a[105];		//记录原数组
    int b[105];		//记录奇数个数的前缀和
    int c[105];		//记录所有的切点
    int n,s,tmp;
    int cnt=0,ans;
    
    int main()
    {
    	while(~scanf("%d%d",&n,&s))
    	{
    		memset(a,0,sizeof(a));
    		memset(b,0,sizeof(b));
    		memset(c,0,sizeof(c));
    		ans=0;
    		cnt=0;
    		for(int i=1;i<=n;i++)
    		{
    			scanf("%d",&a[i]);
    			b[i]=a[i]%2+b[i-1];
    		}
    		if(n%2==0 && b[n]==n/2)		//如果奇数个数字或者整个数列中奇数和偶数的个数不相等显然切不成
    		{
    			for(int i=2;i<n;i+=2)
    			{
    				if(b[i]==i/2)
    				c[cnt++]=abs(a[i+1]-a[i]);
    			}
    			sort(c,c+cnt);
    			tmp=0;
    			for(int i=0;i<cnt;i++)
    			{
    				if(tmp+c[i]<=s)
    				{
    					tmp+=c[i];
    					ans++;
    				}
    				else
    				{
    					break;
    				}
    			}
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    IOS开发 网络发展史(NSURLProtocol)
    IOS开发 网络发展史(NSURLCach)
    IOS 开发 网络发展史(URLConnection)
    ios 网络开发(CFNetwork)
    MAC安装mysql
    conda 安装常用包
    从必应上拉取图片
    旋转数组中的二分查找
    东北大学 python模拟登录校园网(2019年6月启用新登录模式后)
    Ubuntu 安装Codeblocks
  • 原文地址:https://www.cnblogs.com/qgmzbry/p/10662096.html
Copyright © 2011-2022 走看看