耍杂技的牛

传送门

题意: 农民约翰的N头奶牛(编号为1…N)计划逃跑并加入马戏团,为此它们决定练习表演杂技。
奶牛们不是非常有创意,只提出了一个杂技表演:
叠罗汉,表演时,奶牛们站在彼此的身上,形成一个高高的垂直堆叠。
奶牛们正在试图找到自己在这个堆叠中应该所处的位置顺序。
这N头奶牛中的每一头都有着自己的重量Wi以及自己的强壮程度Si。
一头牛支撑不住的可能性取决于它头上所有牛的总重量(不包括它自己)减去它的身体强壮程度的值,现在称该数值为风险值,风险值越大,这只牛撑不住的可能性越高。
您的任务是确定奶牛的排序,使得所有奶牛的风险值中的最大值尽可能的小。

输入格式
第一行输入整数N,表示奶牛数量。
接下来N行,每行输入两个整数,表示牛的重量和强壮程度,第i行表示第i头牛的重量Wi以及它的强壮程度Si。

输出格式
输出一个整数,表示最大风险值的最小可能值。

数据范围
1≤N≤50000,
1≤Wi≤10,000,
1≤Si≤1,000,000,000
输入样例:
3
10 3
2 5
3 3
输出样例:
2

思路: 将wi + si按照从小到大排序,最大危险系数就是最小的。

代码实现:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
#define int long long
#define lowbit(x) (x &(-x))
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int  INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double EXP = 1e-8;
const ll   MOD = 1e9 + 7;
const int  N = 2e5 + 5;

int n;
struct node{
    int w, s;
    bool operator< (const node &x)const{
        return w + s < x.w + x.s;
    }
}a[N];

signed main()
{
    IOS;

    cin >> n;
    for(int i = 0; i < n; i ++){
        int w, s;
        cin >> w >> s;
        a[i] = {w, s};
    }

    sort(a, a + n);

    int sum = 0, ans = -2e9;
    for(int i = 0; i < n; i ++){
        ans = max(ans, sum - a[i].s);
        sum += a[i].w;
    }

    cout << ans << endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于贪心算法的C代码,它可以根据给定的奶的重量和强壮程度,找到一种奶的顺序,使得所有奶的风险值中的最大值尽可能的小。 ```c #include <stdio.h> #include <stdlib.h> // 定义奶结构体 struct cow { int weight; // 奶重量 int strength; // 奶强壮程度 int index; // 奶编号 }; // 按奶风险值从小到大排序 int cmp(const void *a, const void *b) { struct cow *c1 = (struct cow*)a; struct cow *c2 = (struct cow*)b; return (c1->weight + c2->strength) - (c2->weight + c1->strength); } int main() { int n; // 奶数量 scanf("%d", &n); // 创建奶数组 struct cow cows[n]; for (int i = 0; i < n; i++) { cows[i].index = i + 1; scanf("%d %d", &cows[i].weight, &cows[i].strength); } // 按奶风险值从小到大排序 qsort(cows, n, sizeof(struct cow), cmp); // 创建结果数组,用于存储最终的奶排序 int result[n]; for (int i = 0; i < n; i++) { result[i] = cows[i].index; } // 输出结果 for (int i = 0; i < n; i++) { printf("%d ", result[i]); } return 0; } ``` 代码中,我们首先定义了一个`cow`结构体,用于存储每头奶的重量、强壮程度和编号。然后,我们使用`scanf`函数输入奶数量和每头奶的重量和强壮程度,并创建一个奶数组,将输入的数据存储到数组中。 接下来,我们使用`qsort`函数对奶数组进行排序,排序的依据是奶的风险值,按照风险值从小到大排序。 最后,我们创建一个结果数组,将最终的奶排序存储到结果数组中,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值