算法学习日记 2021/4/23——01

种树

https://ac.nowcoder.com/acm/contest/950/B

学习日记-01
根据题目所给,可以推断出局部最优解就是从每个区间的最后往前种树。

#include<iostream>
#include<algorithm>
using namespace std;
struct line { int l, r, cnt; }a[5010];

bool cmp(line x,line y)
{
    return x.r < y.r;
}
int n, h;

bool used[30010] = { 0 };

void Init()
{
    cin >> n >> h;
    for (int i = 1; i <= h; i++)
    {
        cin >> a[i].l >> a[i].r >> a[i].cnt;
    }
    sort(a + 1, a + h + 1,cmp);
}

void solve()
{
    int k,ans=0;
    for(int i = 1; i <= h; i++)
    {
        k = 0;
        for (int j = a[i].l; j <= a[i].r; j++)
        {
            if (used[j])
            {
                k++;
            }
        }
        if (k >= a[i].cnt) continue;
        else
        {
            for (int j = a[i].r; j >= a[i].l; j--)
            {
                if (!used[j]) 
                {
                    used[j] = 1;
                    k++;
                    ans++;
                    if (k == a[i].cnt)
                        break;
                }
            }
        }
    }
    cout << ans << endl;
}

int main()
{
    Init();
    solve();
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 15:58
投个小米提前批试试水,先投一个岗位看看形势,不行就再沉淀一下投第二个岗位,莫辜负
Java抽象带篮子:我嘞个骚刚,已经开始研发6g了吗
投递小米集团等公司9个岗位
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
评论
6
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务