贪心的算法

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

目录

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;
}

你可能感兴趣的:(洛谷,算法,c++)