poj 3262 Protecting the Flowers

POJ3262 Protecting the Flowers ——菜鸟的ACM之旅 POJ3262 Protecting the Flowers 题目大意 我们的老朋友约翰发现他的牛在吃他的花,每头牛每分钟吃Di朵花,他要把他们送到牛棚里,每次只能送一只,每头牛距离牛棚路程为Ti分钟,约翰每次送牛来回得花 2*Ti 分钟,求牛送完后,牛吃的花总数最少是多少 思路 第一时间想到的就是,每次送回 (Sum(剩下牛每分钟吃花的总数) - Di) * Ti 最少的牛,但是这样有个问题,Sum要换一头牛就要更新,那么每头牛也得现找。我们最理想的状况就是,一个sort直接排序,不用再动了,sort 阅读详情
Protecting the Flowers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4544 Accepted: 1820

Description

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 11 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
注意定义sum_J和sum时要用长整形,一直就错在这里

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100000
__int64 sum_J[MAX+10],sum;
struct Cow
{
	int T;
	int D;
	double P;
}num[MAX+10];
int cmp(const void *a,const void *b)
{
	struct Cow *c=(Cow *)a;
	struct Cow *d=(Cow *)b;
	if(c->P!=d->P)
	return d->P>c->P?1:-1;
}
int main()
{
	int N,i,j,k;
	while(scanf("%d\n",&N)!=EOF)
	{
		for(i=0;i<N;i++)
		{
			scanf("%d %d",&num[i].T,&num[i].D);
			num[i].P=(num[i].D*1.0)/(num[i].T*1.0);
		}
		qsort(num,N,sizeof(num[0]),cmp);
		
		memset(sum_J,0,sizeof(sum_J));
		for(i=N-2,sum_J[N-1]=num[N-1].D;i>=0;i--)
		sum_J[i]=sum_J[i+1]+num[i].D;
//		for(i=0;i<N;i++)
//		printf("%d#\n",sum_J[i]);
		
		for(i=0,sum=0;i<N-1;i++)
		{
//			for(j=i+1,S=0;j<N;j++)
//			S+=num[j].D;
			
			sum+=(sum_J[i+1]*num[i].T);
		}
		printf("%I64d\n",2*sum);//前边定义sum要用长整形,否则会错。(这里一直错) 
		//for(i=0;i<N;i++)
		//printf("%d %d %lf\n",num[i].T,num[i].D,num[i].P);
		
	}
	return 0;
}



田忌赛马: POJ 2287(贪心解法) NULL 博文链接:https://128kj.iteye.com/blog/1759266 立即下载

相关推荐

详解modbus TCP通讯,并利用其实现欧姆龙PLC和西门子PLC的通讯

大家好,之前需要更新的MQTT通讯【2】因为硬件不足还不能发布出来,但最近在项目中涉及到西门子和欧姆龙之间的modbus TCP通讯,想到modbus TCP通讯是个非常常见并广泛使用的通讯协议,今天就结合最近做的测试给大家好好讲解一下,先提示一下,这边只给大家讲解如何实现,至于报文是什么,如何用socket来编写功能块或dll这边不会讲解,这块我会根据私信的小伙伴数量来考虑要不要出一篇如何用ST语言来编写modbus TCP功能块的博客。

dy186187的博客 1万+

Protecting the Flowers 牛客题解

Protecting the Flowers 题目链接:传送门 链接:https://ac.nowcoder.com/acm/problem/25043 来源:牛客网 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

sleepingartist的博客 513

核回归(Kernel Regression)

本文讲解了核函数,并且实战通过核函数进行了预测,并附python代码

a2333333_的博客 6969

【题解】POJ 3262 Protecting the Flowers(贪心)

POJ 3262 Protecting the Flowers 原题 https://vjudge.net/problem/POJ-3262 农夫约翰去砍柴,像往常一样留下了N头(2≤N≤10万头)牛在吃草。当他回来时,他惊恐地发现那群牛正在他的花园里吃他美丽的花。为了尽量减少后续损失,FJ决定立即采取行动,将每头牛运回各自的谷仓。 每头牛i在距离它自己的谷仓Ti分钟(1≤Ti≤2,000,000)的位置。此外,在等待运输时,她每分钟会破坏Di(1≤Di≤100)朵花。无论他如何努力,FJ每次只能将一

bu_shushu的博客 1703

Verilog 并行 FIR 滤波器设计

FIR(Finite Impulse Response)滤波器是一种有限长单位冲激响应滤波器,又称为非递归型滤波器。FIR 滤波器具有严格的线性相频特性,同时其单位响应是有限长的,因而是稳定的系统,在数字通信、图像处理等领域都有着广泛的应用。

m0_61687959的博客 1608

电机控制中ADC采样时序的优化策略与实践

本文深入探讨了电机控制中ADC采样时序的优化策略与实践,重点分析了BLDC和FOC控制方案中的核心挑战与解决方案。通过硬件布局优化、软件滤波技术及动态时序补偿等方法,有效提升电流采样精度和系统稳定性,为电机控制系统的性能优化提供实用指导。

weixin_42519985的博客 395

牛客网 【每日一题】5月28日题目精讲 Protecting the Flowers

链接: 文章目录题目描述题意:题解:代码: 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 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 o

qq_35975367的博客 761

AD9361 FIR 滤波器设计

matlab 得到滤波器系数

weixin_44630490的博客 1万+

【贪心算法】POJ-3262

一、题目 Description 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 eat...

weixin_30950607的博客 488

新手也能懂的Simulink汽车二自由度建模:从公式推导到模型搭建全流程

本文为新手提供了在Simulink中搭建汽车二自由度模型的完整实战指南。文章从模型核心概念讲起,详细讲解了如何将车辆动力学方程转化为状态空间模型,并逐步指导在Simulink中实现从参数设置、模块搭建到仿真验证的全过程。通过阶跃转向测试和参数影响分析,帮助读者直观理解车辆转向特性,并提供了实用的模型扩展思路与常见问题排查技巧。

weixin_29047919的博客 218

WblockCloneObjects克隆一个图纸内的实体包括块参照,同名块参照的处理

public static void HeBingTu2(double lieJu, List<string> filePaths) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; using (DocumentLock docLock = doc.Lock...

1115

POJ3262 贪心

题目: 农夫有n头牛跑到花坪上吃草,农夫要把它们送回自己的牛舍,所化的时间分别为 t_i,(单程时间为t_i),每头牛留在华坪上每单位时间内吃花量分别为d_i。华坪上花最少被破坏的数量为多少? Sample Input 6 3 1 2 5 2 3 3 2 4 1 1 6 Sample Output 86 题解: 很久没做贪心的题,开始就瞎做了。。。以T小或者D的排序都不对。。然后...

不平凡的博客 545

knowhere | 第一课:项目导论与环境搭建

摘要 本课程是Knowhere项目的第一课,重点介绍项目整体架构和开发环境搭建。Knowhere定位为AI Agent的文档记忆基础设施,通过解析多格式文档并保留结构化信息(如章节层级、表格语义等),为Agent提供可导航的文档知识。项目采用Python monorepo结构,核心包括: apps/api:FastAPI服务,处理HTTP请求和流程调用 apps/worker:Celery服务,执行文档解析等耗时任务 packages/shared-python:共享基础设施层 课程明确了各层职责边界,并推

weixin_43674738的博客 156

POJ3262 Protecting the Flowers

POJ3262 Protecting the Flowers(贪心的水题) Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3575 Accepted: 1465 Description Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When

wangjunjie_123的博客 259

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

POJ 3262 Protecting the Flowers

POJ 3262题目大意如下: 还是关于Fermer John 和他的奶牛的故事,FJ有一个花园,里面种了很多花。每天FJ会带牛在外面吃草,牛群回来之后,在FJ不注意的时候会吃掉花园里的花,所以FJ需要把牛群运到它们的牛棚里去。每次只能运送一头牛,给出了运送每头牛所花的时间和每头牛能够每单位时间吃掉的花的数量,现在要求给出一个最优方案,使得FJ损失的花数量最少。 一开始想的太简单,看到题目给出

kornberg_fresnel的博客 509

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

POJ 3262 Protecting the Flowers 贪心 巧妙 G++

<span title="Description ">描述 农夫约翰去砍一些木头,留下N(2≤N≤100,000)牛吃草,像往常一样。当他回来时,他发现他的恐惧,群牛在他的花园里吃他美丽的花。<span title="Wanting to minimize the subsequent damage, FJ decided to take immediate action and t

woniupengpeng的专栏 317

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

POJ - 3262 Protecting the Flowers 贪心

传送门:POJ3262 题意:有n个牛在花园,每个牛在被赶走之前每秒吃Di个花朵。赶它回去要花的时间是Ti * 2。问最小损失花朵是多少。 思路:这个样例真是骚,想了好几种贪心方法都能过样例。。然而都是结果都是一样的wa。。 正解:考虑比率 Ti / Di,越小的越先赶走。 证明: 假设序列都由二元组组成,二元组是由D和T组成,那么对一个序列有相邻的两头牛是这样的 ....

WA是一笔财富 342

POJ3262 Protecting the Flowers 【贪心】

Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4418   Accepted: 1785 Description Farmer John went to cut some wood and left N (2 ≤

长风的专栏 2314

【POJ3262】Protecting the Flowers【贪心】

样例就是很直观的贪心... 但是直接按D再T的简单排序就WA了。 正确姿势如下: 考虑两头牛a和b,并且b的下标比a大,时间为Ta、Tb,每分钟吃的花为Da,Db。 若先a再b,则贡献为: W1 = Db * Ta * 2 若先b再a,则贡献为: W2 = Da * Tb * 2 要使第二种方案最优,则 W2 Da * Tb 这样排序下来就得到了拿走牛的顺序。

BraketBN 555

POJ-3262-Protecting the Flowers

思路: 这个题的关键在于如何排序,不能单独只以时间排序或者食花量排序,而是应该综合二者进行排序! 可以根据T*1.0/D进行升序排序,或者根据D*1.0/T进行降序排序,亦或根据T*D进行升序排序!为什么酱紫排呢,因为这样能保证每次做出的选择都是损失最小的! 证明: 二个羊中 A,B,属性分别为分别为eatA,timeA,eatB,timeB 选A的时候损失timeA*eatB 选B的时候损失timeB*eatA 双方同除以eatA*eatB. 令time/eat为一个羊的比率x 可以证明x小的那个

差不多先灬森 1730

POJ 3262 Protecting the Flowers 贪心

题意很简单。 有n个牛在FJ的花园乱吃。 所以FJ要赶他们回牛棚。 每个牛在被赶走之前每秒吃Di个花朵。赶它回去FJ要花的时间是Ti。在被赶走的过程中牛就不能乱吃了 那么其实我第一感觉就是跟比率有关。 然后简略的证了一下。 假设序列都由二元组组成,二元组是由D和T组成,那么对一个序列有相邻的两头牛是这样的 ..........(a, b) ,(c, d)...........

水 3209
上一篇: 丑数总结
下一篇: hdoj 1015 Safecracker
SimonCoder
博客等级 码龄13年 1413粉丝 231原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址