zoukankan      html  css  js  c++  java
  • 考试题目“笨笨的西瓜种植”

    【题目描述】

    笨笨种了一块西瓜地,但这块西瓜地的种植范围是一条直线的…… 笨笨在一番研究过后,得出了m个结论,这m个结论可以使他收获的西瓜最多。 笨笨的结论是这样的: 从西瓜地B处到E处至少要种植T个西瓜,这个范围的收获就可以最大化。 笨笨不想那么辛苦,所以他想种植的西瓜尽量少,而又满足每一个所得的结论。

    【输入】

    第一行两个数n,m0<n<=5000,0<=m<=3000),表示笨笨的西瓜地长n,笨笨得出m个结论。 

    接下来m行表示笨笨的m个结论,每行三个数b,e,t1<=b<=e<=n,0<=t<=e-b+1

    【输出】

     输出笨笨最少需种植多少西瓜。

    【输入样例】

    9 4

    1 4 2

    4 6 2

    8 9 2

    3 5 2

    【输出样例】

    5

     

    这道题必须做到“贪”!

    尽量一个西瓜种的地方满足尽量多的条件

    先sort

    先以右边越小越好,再以左边越小越好

    西瓜尽量种右边

    问题就解决啦

     

    代码如下:

    <span style="font-size:12px;"><span style="BACKGROUND-COLOR: #ffff99">#include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<stack>
    #include<queue>
    using namespace std;
    struct ill{
    	int l,r,g;
    }a[3001];
    bool fuu(ill x,ill y)
    {
    	if(x.r<y.r)
    		return 1;
    	if(x.r==y.r&&x.l<y.l)
    		return 1;
    	return 0;
    }
    int v[5001],k;
    int main()
    {
    	freopen("watermelon.in","r",stdin);
    	freopen("watermelon.out","w",stdout);
    	int n,m,i,j;
    	scanf("%d%d",&n,&m);
    	for(i=0;i<m;i++)
    		scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].g);
    	sort(a,a+m,fuu);
    	for(i=0;i<m;i++)
    	{
    		for(j=a[i].l;j<=a[i].r;j++)
    			if(v[j])
    				a[i].g--;
    		if(a[i].g>0)
    		{
    			for(j=a[i].r ; j>=a[i].l&&a[i].g>0 ; j--)
    			{
    				if(!v[j])
    				{
    					v[j]=1;
    					a[i].g--;
    				}
    			}
    		}
    	}
    	for(i=0;i<=n;i++)
    		if(v[i])
    			k++;
    	printf("%d",k);
    }</span><span style="font-size:14px;"></span></span>

    贪得好开心,一次AC!!(^-^)

     

  • 相关阅读:
    670. Maximum Swap
    653. Two Sum IV
    639. Decode Ways II
    636. Exclusive Time of Functions
    621. Task Scheduler
    572. Subtree of Another Tree
    554. Brick Wall
    543. Diameter of Binary Tree
    535. Encode and Decode TinyURL
    博客园自定义背景图片
  • 原文地址:https://www.cnblogs.com/Darknesses/p/12002582.html
Copyright © 2011-2022 走看看