贪心的算法

目录

P2240 【深基12.例1】部分背包问题

P1223 排队接水

 P1803 凌乱的yyy / 线段覆盖

P1208 [USACO1.3] 混合牛奶 Mixing Milk

P3817 小A的糖果

P5019 [NOIP2018 提高组] 铺设道路

P1094 [NOIP2007 普及组] 纪念品分组


P2240 【深基12.例1】部分背包问题

const int N = 1010;
struct node
{
    double w, v, p;
} a[110];
bool cmp(node a, node b)
{
    return a.p > b.p;
}
inline void solve()
{
    int n;
    double t, sum;
    cin >> n >> t;
    ff(i, n)
    {
        cin >> a[i].w >> a[i].v;
        a[i].p = a[i].v / a[i].w;
    }
    sort(a + 1, a + n + 1, cmp);
    ff(i, n)
    {
        if (t >= a[i].w)
        {
            t -= a[i].w;
            sum += a[i].v;
        }
        else
        {
            sum += t * a[i].p;
            break;
        }
    }
    printf("%.2f", sum);
}

P1223 排队接水

const int N = 1010;
struct node
{
    int num, time;
} a[1010];
bool cmp(node a, node b)
{
    if (a.time != b.time)
        return a.time < b.time;
    return a.num < b.num;
}
inline void solve()
{
    int n;
    cin >> n;
    ff(i, n)
    {
        cin >> a[i].time;
        a[i].num = i;
    }
    sort(a + 1, a + n + 1, cmp);
    ff(i, n)
    {
        cout << a[i].num << " ";
    }
    cout << endl;
    double ans = 0;
    ff(i, n) ans += i * a[n - i].time;
    ans /= n;
    printf("%.2f", ans);
}

 P1803 凌乱的yyy / 线段覆盖

const int N = 1e6 + 10;
struct node
{
    int start, end;
} a[N];
bool cmp(node a, node b)
{
    return a.end < b.end;
}
inline void solve()
{
    int n;
    cin >> n;
    ff(i, n)
    {
        cin >> a[i].start >> a[i].end;
    }
    sort(a + 1, a + n + 1, cmp);
    double ans = 0, pos = 0;
    ff(i, n)
    {
        if (pos <= a[i].start)
        {
            pos = a[i].end;
            ans++;
        }
    }
    cout << ans << endl;
}

P1208 [USACO1.3] 混合牛奶 Mixing Milk

const int N = 5010;
struct node
{
    int price, production;
} a[N];

bool cmp(node a, node b)
{
    if (a.price != a.production)
        return a.price < b.price;
    return a.production > b.production;
}

inline void solve()
{
    int n, m;
    cin >> n >> m;
    ff(i, m)
    {
        cin >> a[i].price >> a[i].production;
    }
    sort(a + 1, a + m + 1, cmp);
    int i = 1, ans = 0;
    while (n)
    {
        if (a[i].production != 0)
        {
            a[i].production--;
            ans += a[i].price;
            n--;
        }
        else
            i++;
    }
    cout << ans;
}

P3817 小A的糖果

const int N = 1e5 + 10;
int a[N];
inline void solve()
{
    int n, x;
    cin >> n >> x;
    ff(i, n) cin >> a[i];
    LL ans = 0;
    for (int i = 1; i < n; i++)
    {
        if (a[i] + a[i + 1] > x)
        {
            ans += a[i + 1] - x + a[i];
            a[i + 1] = x - a[i];
        }
    }
    cout << ans;
}

P5019 [NOIP2018 提高组] 铺设道路

const int N = 1e5 + 10;
int a[N];
inline void solve()
{
    int n;
    cin >> n;
    ff(i, n) cin >> a[i];
    int ans = 0;
    for (int i = 2; i <= n; i++)
    {
        if (a[i] > a[i - 1])
            ans += a[i] - a[i - 1];
    }
    cout << ans + a[1];
}

P1094 [NOIP2007 普及组] 纪念品分组

const int N = 1e6 + 10;
int a[N];
inline void solve()
{
    int n, w;
    cin >> w >> n;
    ff(i, n) cin >> a[i];
    sort(a + 1, a + 1 + n);
    int l = 1, r = n;
    int ans = 0;
    while (l <= r)
    {
        if (a[l] + a[r] <= w)
        {
            ans++;
            l++;
            r--;
        }
        else
        {
            r--;
            ans++;
        }
    }
    cout << ans;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值