貪心::poj1328 radar installation && poj2109 Power of Cryptography && poj2586 Y2K Accounting Bug

POJ1328解题报告 每日一看 Radar Installation Time Limit:1000MS Memory Limit:10000K Total Submissions:40799 Accepted:9034 Description Assume the coasting is an infinite straight line. Land is ... 阅读详情

貪心,就是步步為贏。

這是ACM知識表里基礎算法中的貪心部分,屬於水題範疇。

1. poj1328 radar installation

題目:照抄了。

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.
We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.

分析:根據d計算出每個島在x軸上的取值範圍(用區間表示)。按左端點對區間從小到大進行排序。先令第一個雷達在第一個區間的右端點,(畫個圖就清楚了),然後順序掃描剩下的各個區間,如果下一個區間的左端點大於當前雷達位置,則要增加雷達在下一個區間的右端點,如果雷達位置在下一個區間[p[i].l,p[i].r]裡面,則令雷達位置右移到p[i].r,此時不用增加雷達而能覆蓋下一個島!這是Greedy的算法。

貌似所有的math.h中的算法都是double的~~

時間複雜度為O(nlgn)為sort的快排!

 

#include "stdafx.h"      // 280K 16MS 
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef struct{
     double l, r;
}interval; // 区间
bool cmp(interval a,interval b){return a.l<=b.l;}
int _tmain(int argc, _TCHAR* argv[])
{ 
     int n, d, i, x, y, cnt = 1;
     double pre; 
     interval p[1000]; 
     while(1){ 
         cin>>n>>d; 
         if(n == 0 && d == 0) break; 
         bool no = 0; 
         for(i = 0; i < n; i++){  
              cin>>x>>y; 
              if(d >= y && !no){
                   p[i].l = double(x) - sqrt(double(d * d - y * y)); 
                   p[i].r = double(x) + sqrt(double(d * d - y * y));
              }  
              else
                   no = 1;       //不知為何,在這裡break的話就不行!
           } 
         if(no){  
              cout<<"Case "<<cnt++<<": -1"<<endl; 
              continue; 
         } 
         sort(p,p+n,cmp);
         int ans = 1; 
         pre = p[0].r; 
         for(i = 1; i < n; i++){  
              if(p[i].l > pre){   
                   ans++;   
                   pre = p[i].r;
              }  
              else if(p[i].r <= pre) 
                       pre = p[i].r; 
         }  
         cout<<"Case "<<cnt++<<": "<<ans<<endl;;
     } 
     return 0;
}

2.  poj2109 Power of Cryptography

題目:已知整數n和p,求整數k使得k..^n=p成立。

分析:雖說是貪心的目的,但稍微想一下就知道,直接開根號就行了,用double的精度是足夠的。math.h中的double pow(double,double)真是強大。

時間複雜度:就看pow函數了,不知道它的原理,應該是牛頓迭代法吧~

#include "stdafx.h"
#include <iostream>    // 280K 0MS 
#include <math.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	double n,p;  //注意double的取值範圍
	while(cin>>n>>p){
		cout<<pow(p,1/n)<<endl;
	}
	return 0;
}


3. poj2586 Y2K Accounting Bug

題目:巨難懂!!一家奇怪的公司每個月要么盈利s要么盈利d。公司是每連續5個月結算一次,這樣一年就有8次結算,都是虧的。問公司能否盈利,若能求最大盈利,否則輸出Deficit.

分析:也就是對s和d組成的長度為12的序列進行判斷,因為每次結算都是虧的,所以只有五種情況(盈利最大情形)!

時間複雜度O(n)

#include "stdafx.h"
#include <iostream>    //256K 16MS 
using namespace std;

int benif(int s, int d){
	if(4*s-d<0) return 10*s-2*d;   //ssssdssssdss
	if(3*s-2*d<0) return 8*s-4*d;  //sssddsssddss
	if(2*s-3*d<0) return 6*s-6*d;  //ssdddssdddss
	if(s-4*d<0) return 3*s-9*d;    //sddddsddddsd
	return -1;    //dddddddddddd
}

int _tmain(int argc, _TCHAR* argv[])
{
	int s,d;
	while(cin>>s>>d){
		if(benif(s,d)>=0) cout<<benif(s,d)<<endl;
		else cout<<"Deficit" << endl;
	}
	return 0;
}



 

欧姆龙485通讯示例程序_【精品实验】PLC学习神器与温湿度变送器的通讯 BFH点击蓝字关注我们↑↑↑↑  PLC学习神器提供RS485接口,可支持无协议RS指令、无协议RS2指令、Modbus通讯功能,可通过设置通讯格式改变通讯协议,RS485对应的通讯格式地址是D8400;  本案例以RS485接口与温湿度变送器通讯,通讯格式如下一温湿度变送器的说明书介绍1.1主要技术参数1.2 通讯协议 1、所有通讯回路都应遵照主/从方式。依照主从方式,数据可以在一个主... 阅读详情

相关推荐

从mseed到SAC格式转换全攻略:mseed2sac工具安装与批量处理技巧

本文详细介绍了如何将mseed格式的地震数据高效转换为SAC格式,涵盖mseed2sac工具的安装、单文件转换技巧、批量处理自动化实现以及元数据高级处理策略。通过具体命令和脚本示例,帮助地震学研究者快速掌握数据预处理的关键技术,提升波形数据分析效率。

ruby5的博客 927

Radar Installation(雷达安装)c语言

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the

qq_63683485的博客 1193

Trans论文复现:电池储能调峰和调频:超线性增益的联合优化程序代码!

本程序参考一区SCI期刊《Using Battery Storage for Peak Shaving and Frequency Regulation: Joint Optimization for Superlinear Gains》,程序。与之前将电池储能用于单一应用相比,研究结果表明,如果电池联合提供多种服务,它们可以获得比以前想象的更大的经济效益。欢迎感兴趣的小伙伴关注下方公众号获取完整版代码哦~,小编会继续推送更有质量的学习资料、文章程序代码~限于篇幅,仅列出部分算例的结果!

weixin_48747334的博客 567

题解:Radar Installation(贪心)

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the ...

晚安( ̄o ̄) . z Z 534

自定义串口通讯协议

自定义串口通讯协议的实现方法

qq_44826038的博客 2172

二极管限幅电路和钳位电路

二极管限幅电路和钳位电路学习

HW-BASE的博客 929

区间重叠问题 (贪心)

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the

OJBFOWE的博客 2201

万字长文解密:SAP Fiori 首屏加载缓慢背后的真相

什么是一个健康理想的 Fiori 首次加载状态?FLP 首页不臃肿,用户默认看见的内容不多但足够用缓存作业按规则运行,冷启动不至于每次都从零开始应用首屏只读必要数据,UI5 资源经过 preload,视图异步加载OData 服务字段克制、分页合理、缓存分层清楚后台 CDS 与 HANA 只为当前屏幕做最必要的工作。只要这些标准同时满足,用户第一次打开 Fiori 页面的体感通常会从明显卡顿变成可接受甚至顺滑。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 558

贪心算法解题报告(区间覆盖问题)

题目: Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on ...

No Pain, No Gain . 1601

北斗导航 | 从北一到北二再到北三,你应该知道的北斗导航系统

博主github:https://github.com/MichaelBeechan 博主优快云:https://blog.csdn.net/u011344545 一、引言 我国高度重视北斗系统的建设和发展。自20世纪80年代开始探索适合我们国家国情的卫星导航系统发展道路,形成了“三步走”的发展战略:2000年底,建成北斗一号导航系统,向我国广大用户提供服务;2012年底,建成北斗二号导航系统,...

尘世冰封的专栏 3079

DeerFlow 系列教程 第四篇 | Sandbox 沙箱与 Sub-agent 子智能体协作详解

DeerFlow 系列教程第四篇介绍了两个关键子系统:Sandbox 沙箱系统和Sub-agent子智能体协作机制。沙箱系统通过虚拟路径映射、权限白名单、命令审计和容器隔离四重机制,为Agent提供安全的代码执行环境,使其能在隔离环境中执行文件操作和脚本运行。系统采用分层架构设计,包含应用层、工具层、审计层等,核心是定义标准交互接口的抽象基类。虚拟路径系统将Agent操作的虚拟路径映射到实际路径,确保安全性。这些功能使DeerFlow从对话助手进化为能处理复杂任务的Super Agent平台。

weixin_43674738的博客 285

ACM经典题型,雷达找小岛

题目:Radar Installation (pku 1328) Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea sid...

ChenxiLiylnf的博客 1012

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other.

#include #include #include #include #include using namespace std; struct frame {     double x;     double y; }c[1001]; struct range {     double l, r; }g[1001]; bool cmp(range a

PIANKEzzc的博客 4148

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

计算机控制技术及应用

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

小岛问题

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the ...

qq_43628925的博客 582

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

贪心算法解决雷达站建站问题

问题描述: Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locatin

MSFollower的博客 2172

ACM贪心总结

贪心法是一种解决问题的策略。如果策略正确,那么贪心法往往是易于描述,易于实现的。选择策略最关键的是读懂题,翻译能力和抽象能力。 如果不能提取有用的信息那么往往会将问题复杂化最后那自己绕进去。 贪心再进行操作前多会用sort排序或结构体然后对结构体的一个成员排序 根据其他成员的关系求解。 多机调度问题(上次列举了其中一种情况) n个作业组成的作业集,可由m台相同机器加工处理。要求给出一种作业调度方案...

u013345179的博客 387

Radar Installation POJ - 1328(贪心)

Radar Installation POJ - 1328 Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar ...

rainbowsea_1的博客 382

poj1328解题报告

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54665   Accepted: 12314 Description Assume the coasting is an infinite straight line. Land

u013343013的专栏 540

Radar Installation (贪心!)

Radar Installation   Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar i

Learning_is_endless的博客 389

翻译题9寒假1周赛题

Radar Installation【雷达装置】 Time Limit:1000MS Memory Limit:10000K Total Submissions:152937 Accepted:33893 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small islan...

Abby_yyyyyyy的博客 178

radar

Describe Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locatin...

qq_43569119的博客 230

poj1328 Radar Installation 解题报告

Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, loc

xxm137164869的专栏 2250

贪心思想(区间问题)---雷达安装问题

Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 143567 Accepted: 31703 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in

qq_43637727的博客 736

HPUOJ--2017寒假作业-专题0/Q-Radar Installation

Q - Radar Installation   Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any rad

Greenary的博客 577

贪心算法C题

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in th.

qq_50960478的博客 233

贪心-覆盖问题-圆心位置的判别

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the

weixin_46204099的博客 324
上一篇: DFS.枚舉::poj1753 flip game && poj2965 the Pilots Brothers'refrigerator
初四
博客等级 码龄15年 3粉丝 19原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值