首页 > 其他分享 >P2887 [USACO07NOV] Sunscreen G

P2887 [USACO07NOV] Sunscreen G

时间:2024-03-28 15:24:41浏览次数:28  
标签:node cow USACO07NOV int 线段 Sunscreen sh P2887 升序

原题链接

题解

1.题目可以抽象转化为,若干个点和线段,求问最多有多少点和线段能配对,如果点在线段内

2.我们可以采用贪心的方法,对点升序排序,对线段按右端点升序排序
为什么请看下图

code

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int l,r,chose;
}cow[2505];
struct node2
{
    int x,c;
}sh[2505];
bool cmp1(node a,node b)
{
    if(a.r!=b.r) return a.r<b.r;
    return a.l<b.l;
}

bool cmp2(node2 a,node2 b)
{
    return a.x<b.x;
}
int main()
{
    int c,l;
    cin>>c>>l;
    for(int i=1;i<=c;i++)
    {
        cin>>cow[i].l>>cow[i].r;
        cow[i].chose=0;
    }
    for(int i=1;i<=l;i++) cin>>sh[i].x>>sh[i].c;

    sort(cow+1,cow+1+c,cmp1);
    sort(sh+1,sh+1+l,cmp2);

    int ans=0;

    for(int i=1;i<=l;i++)
    {
        for(int j=1;j<=c;j++)
        {
            int x=sh[i].x;
            if(cow[j].l<=x&&x<=cow[j].r&&sh[i].c&&!cow[j].chose)
            {
                cow[j].chose=1;
                sh[i].c--;
                ans++;
            }
            if(sh[i].c==0) break;
        }
    }


    cout<<ans;
    return 0;
}

标签:node,cow,USACO07NOV,int,线段,Sunscreen,sh,P2887,升序
From: https://www.cnblogs.com/pure4knowledge/p/18101769

相关文章

  • POJ--3614 Sunscreen(贪心)
    记录18:262024-2-15http://poj.org/problem?id=3614贪心法,将minspf从大到小排列,然后选取最大的spf点击查看代码#include<iostream>#include<vector>#include<algorithm>#include<utility>#include<stdio.h>#include<string.h>usingnamespacestd;......
  • 洛谷题解-P2888 [USACO07NOV] Cow Hurdles S (Floyd)
    https://www.luogu.com.cn/problem/P2888题目描述FarmerJohnwantsthecowstoprepareforthecountyjumpingcompetition,soBessieandthegangarepracticingjumpingoverhurdles.Theyaregettingtired,though,sotheywanttobeabletouseaslittleene......
  • [USACO07NOV]牛继电器Cow Relays
    https://www.luogu.org/problem/P2886题目描述:给出一张无向连通图,求\(S\)到\(E\)经过\(k\)条边的最短路。对于一类\(S\)到\(E\)走指定数量的边数,求它的最短路或条数,......
  • 题解 洛谷 P2885 [USACO07NOV]Telephone Wire G
    1.题面描述题目链接给出\(n\)棵树的高度。你可以进行任意次操作,每次操作形如:把某棵树增高\(x\),代价为\(x^2\)(注意:不能将一棵树的高度减少)。在操作完之后,一个状态......
  • P2467 地精部落 P2885 [USACO07NOV]Telephone Wire G
    P2467SDOI2010地精部落称满足条件的序列为"波动序列"性质1:如果一个波动序列中i和i+1不相邻,则交换这两个数后仍然是波动序列性质2:将一个波动序列反转,翻转后......