贪心的思想

803.区间合并

给定 n 个区间 [li,ri],要求合并所有有交集的区间。

注意如果在端点处相交,也算有交集。

输出合并完成后的区间个数。

例如:[1,3] 和 [2,6] 可以合并为一个区间 [1,6]。

输入格式

第一行包含整数 n。

接下来 n 行,每行包含两个整数 l 和 r。

输出格式

共一行,包含一个整数,表示合并区间完成后的区间个数。

数据范围

1≤n≤100000
−10^9≤li≤ri≤10^9

输入样例:

5
1 2
2 4
5 6
7 8
7 9

输出样例:

3

代码 

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

typedef pair<int,int> PII;

const int N = 1e5 + 10;
vector<PII> pos;

int n;
int res = 1;

bool cmp(PII A,PII B){
    return A.first < B.first;
}

int main()
{
    cin >> n;
    for(int i = 0,l,r;i < n;i ++){
        cin >> l >> r;
        pos.push_back({l,r});
    }
    
    sort(pos.begin(),pos.end(),cmp);
    int ist = pos[0].first, ed = pos[0].second;
    for(int i = 1;i < n;i ++){
        auto [l,r] = pos[i];
        // cout << l << " " << r << endl;
        if(l <= ed){
            ed = max(ed,r);
        }else{
            ist = l, ed = r;
            res ++;
        }
    }
    
    cout << res << endl;
    
    return 0;
}

905.区间选点 

给定 N 个闭区间 [ai,bi],请你在数轴上选择尽量少的点,使得每个区间内至少包含一个选出的点。

输出选择的点的最小数量。

位于区间端点上的点也算作区间内。

输入格式

第一行包含整数 N,表示区间数。

接下来 N 行,每行包含两个整数 ai,bi,表示一个区间的两个端点。

输出格式

输出一个整数,表示所需的点的最小数量。

数据范围

1≤N≤10^5,
−10^9≤ai≤bi≤10^9

输入样例:

3
-1 1
2 4
3 5

输出样例:

2

代码 

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e5 + 10;

struct T{
    int l;
    int r;
} a[N];

bool cmp(T A,T B){
    return A.l < B.l;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0;i < n;i ++) cin >> a[i].l >> a[i].r;
    
    sort(a, a + n, cmp);
    
    int res = 0;
    int ist = -2e9 + 10,ed = -2e9 + 10;
    for(int i = 0;i < n;i ++){
        if(a[i].l <= ed){
            ist = max(ist,a[i].l), ed = min(ed,a[i].r);
        }else{
            ist = a[i].l, ed = a[i].r;
            res ++;
        }
    }
    
    cout << res << endl;
    return 0;
}

J. Stacking of Goods 

Problem - J - Codeforces

You are a grocery store owner, and there are nn goods in your store. Each good i has an associated weight wi, initial volume vi, and compression coefficient ci.

You stack all the goods into one pile to keep the store tidy. Because goods can be compressed, assuming that the sum of the weights of the items above the goods i is W (Not including itself), then after stacking, the actual volume of the goods ii will become vi−ci×W.

The space in your store is really limited, so you want to know the minimum possible value of the sum of the actual volumes of the items.

Input

The first line contains a single integer n (1≤n≤105)n (1≤n≤105), representing the number of goods.

For the following n lines, each line contains three integers wi,vi,ci (1≤wi≤10^5,1≤vi≤10^12,0≤ci<vi∑wi), representing the weight, volume, and compression coefficient of the ii-th good.

It's guaranteed that the actual volume of each good will never be compressed into a negative number or zero.

你是一家杂货店的店主,店里有 n 件商品。每种商品 i 都有相应的重量 wi 、初始体积 vi 和压缩系数 ci 。

为了保持商店整洁,你将所有货物堆放在一起。由于货物可以被压缩,假设货物 ii 上面的物品重量总和为 W (不包括其本身),那么堆放后,货物的实际体积 i 将变为 vi−ci×W。

您商店的空间非常有限,因此您想知道商品实际体积之和的最小值。

思路

这道题的思路是需要一定的思考的,首先我们先把v的总和算出来,其次只需要求出min(vi - ci * w),这里v肯定是固定的,那么只需要每次求出ci *w最大值即可

可以先自己思考再看思路,如下

代码

#include <iostream>
#include <tuple>
#include <vector>
#include <algorithm>

#define ll long long

using namespace std;

typedef tuple<int,ll,int> T;

vector<T> a;

int n;
ll res = 0,sum = 0;

bool cmp(T A,T B){
    ll w1 = get<0>(A), w2 = get<0>(B);
    ll c1 = get<2>(A), c2 = get<2>(B);
    return w1 * c2 < w2 * c1;
}

int main()
{
    cin >> n;
    for(ll i = 0,w,v,c;i < n;i ++){
        cin >> w >> v >> c;
        a.push_back({w,v,c});
        res += v, sum += w;
    }

    sort(a.begin(),a.end(),cmp);

    for(int i = 0;i < n;i ++){
        auto [w,v,c] = a[i];
        sum -= w;
        res -= c * sum;
    }

    cout << res << endl;

    return 0;
}

加油 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值