当前位置: 首页 > news >正文

奶牛问题

Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cluster of cows was in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport each cow back to its own barn.

Each cow i is at a location that is Ti minutes (1 ≤ Ti ≤ 2,000,000) away from its own barn. Furthermore, while waiting for transport, she destroys Di (1 ≤ Di ≤ 100) flowers per minute. No matter how hard he tries, FJ can only transport one cow at a time back to her barn. Moving cow i to its barn requires 2 × Ti minutes (Ti to get there and Ti to return). FJ starts at the flower patch, transports the cow to its barn, and then walks back to the flowers, taking no extra time to get to the next cow that needs transport.

Write a program to determine the order in which FJ should pick up the cows so that the total number of flowers destroyed is minimized.

Input
Line 1: A single integer N
Lines 2.. N+1: Each line contains two space-separated integers, Ti and Di, that describe a single cow's characteristics
Output
Line 1: A single integer that is the minimum number of destroyed flowers
Sample Input
6
3 1
2 5
2 3
3 2
4 1
1 6
Sample Output
86
Hint
FJ returns the cows in the following order: 6, 2, 3, 4, 1, 5. While he is transporting cow 6 to the barn, the others destroy 24 flowers; next he will take cow 2, losing 28 more of his beautiful flora. For the cows 3, 4, 1 he loses 16, 12, and 6 flowers respectively. When he picks cow 5 there are no more cows damaging the flowers, so the loss for that cow is zero. The total flowers lost this way is 24 + 28 + 16 + 12 + 6 = 86.
 
 
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
int t,d;
};
bool cmp(node a,node b)
{
    return a.d*b.t>a.t*b.d;
}
node a[100];

int main()
{
    int n;
    cin>>n;
    int i;

    for(i=0;i<n;i++)
    {
        cin>>a[i].t>>a[i].d;
    }
    sort(a,a+n,cmp);
    i=1;
    int sum=0;
    for(i=0;i<n;i++)
    {
        int tt=2*a[i].t;
        int j;
        for(j=i+1;j<n;j++)
        {
            sum+=tt*a[j].d;
        }
    }
    cout<<sum<<endl;
    return 0;
}

 

 

 

 

 

#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
struct node{
int t,d;
};
bool cmp(node a,node b)
{
    return a.d*b.t>a.t*b.d;
}
node a[100010];

int main()
{
    int n;
    ll sum, num;
    while(cin>>n){
            int i;
             sum = 0;

            for(i=0;i<n;i++)
            {
                cin>>a[i].t>>a[i].d;
                sum+=a[i].d;
            }
            sort(a,a+n,cmp);
            num = 0;
            for(i=0;i<n;i++)
            {
                    sum -= a[i].d;
                num += sum * (a[i].t * 2);
            }
            cout<<num<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zsy831143/p/9016900.html

相关文章:

  • 理解MapReduce计算构架
  • 腾讯云SSL证书管理
  • 清除浮动最有效的css写法
  • 基于Docker搭建MySQL主从复制
  • 脑洞篇之我们生活在9维世界
  • Python time 的应用
  • 【剑指offer】面试题 2. 实现 Singleton 模式
  • Go-变量-var
  • 复习mysql
  • 【转载】C/C++内存对齐
  • linux运维、架构之路-MHA高可用方案
  • 线索二叉树实例(前序创建,中序遍历)--2018.5.15
  • vuex填坑记录
  • 多版本并发控制
  • Unity4-用户输入
  • Date型的使用
  • javascript 总结(常用工具类的封装)
  • Java新版本的开发已正式进入轨道,版本号18.3
  • Less 日常用法
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • overflow: hidden IE7无效
  • Redux系列x:源码分析
  • SpiderData 2019年2月13日 DApp数据排行榜
  • SpringBoot几种定时任务的实现方式
  • 从零开始学习部署
  • 给Prometheus造假数据的方法
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 学习笔记DL002:AI、机器学习、表示学习、深度学习,第一次大衰退
  • 一文看透浏览器架构
  • ​2021半年盘点,不想你错过的重磅新书
  • #我与虚拟机的故事#连载20:周志明虚拟机第 3 版:到底值不值得买?
  • (1)常见O(n^2)排序算法解析
  • (1)虚拟机的安装与使用,linux系统安装
  • (30)数组元素和与数字和的绝对差
  • (5)STL算法之复制
  • (libusb) usb口自动刷新
  • (pojstep1.1.2)2654(直叙式模拟)
  • (八)c52学习之旅-中断实验
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (三十五)大数据实战——Superset可视化平台搭建
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)Scala的“=”符号简介
  • .equals()到底是什么意思?
  • .net 7 上传文件踩坑
  • .NET CLR Hosting 简介
  • .NET 读取 JSON格式的数据
  • .net操作Excel出错解决
  • [Android]Android开发入门之HelloWorld
  • [Apio2012]dispatching 左偏树
  • [C++]运行时,如何确保一个对象是只读的
  • [cb]UIGrid+UIStretch的自适应