NOIP2012 T3开车旅行 set+倍增
70分做法:
先预处理出所有点的最近和次近(O(n^2)一遍就OK)
然后暴力求出每个解(O(nm))
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 0x3fffffff
using namespace std;
int n,x,rech=0x3fffffff,rec,s,m;
double ans=0x3fffffff;
struct Path{int to,weight,to2,weight2;void init(){weight=inf,weight2=inf,to=-1,to2=-1;}}path[100050];
struct Node{int position,height;}node[100050];
bool cmp(Node a,Node b){return a.height<b.height;}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
node[i].position=i;
scanf("%d",&node[i].height);
}
for(int i=1;i<=n;i++){
path[i].init();
for(int j=i+1;j<=n;j++){
if(path[i].weight2>abs(node[j].height-node[i].height)){
path[i].weight2=abs(node[j].height-node[i].height);
path[i].to2=node[j].position;
}
else if(path[i].weight2==abs(node[j].height-node[i].height)&&node[j].height<node[i].height){
path[i].to2=node[j].position;
}
if(path[i].weight2<path[i].weight){
swap(path[i].weight2,path[i].weight);
swap(path[i].to,path[i].to2);
}
else if(path[i].weight2==path[i].weight&&node[j].height<node[i].height){
swap(path[i].to,path[i].to2);
}
}
}
scanf("%d",&x);
for(int i=1;i<=n;i++){
int wei1=0,wei2=0,f=1;
for(int j=i;;){
if(f){
if(wei1+wei2+path[j].weight2<=x)
wei2+=path[j].weight2;
else break;
j=path[j].to2;
}
else{
if(wei1+wei2+path[j].weight<=x)
wei1+=path[j].weight;
else break;
j=path[j].to;
}
f^=1;
}
if(wei1&&(ans>1.0*wei2/wei1||(ans==1.0*wei2/wei1&&rech<node[i].height))){
ans=1.0*wei2/wei1;
rec=i;
rech=node[i].height;
}
}
printf("%d\n",rec);
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&s,&x);
int wei1=0,wei2=0,f=1;
for(int j=s;;){
if(f){
if(wei1+wei2+path[j].weight2<=x)
wei2+=path[j].weight2;
else break;
j=path[j].to2;
}
else{
if(wei1+wei2+path[j].weight<=x)
wei1+=path[j].weight;
else break;
j=path[j].to;
}
f^=1;
}
printf("%d %d\n",wei2,wei1);
}
}
100分做法:
先用set 从后向前插入 ,取出左边两个点和右边两个点(如果有的话),排个序。
O(nlogn)求出最近和次近。
然后呢 用倍增求距离
g[i][j]表示从i出发走2^j轮到的地方
f[i][j][0]表示从i出发走2^j轮A走了多少
f[i][j][1]表示从i出发走2^j轮B走了多少
预处理出走一轮到哪儿
g[i][0]=edge[edge[i].to2].to;
f[i][0][0]=edge[i].weight2;
f[i][0][1]=edge[edge[i].to2].weight;
倍增就好了…
g[i][j]=g[g[i][j-1]][j-1];
f[i][j][0]=f[g[i][j-1]][j-1][0]+f[i][j-1][0];
f[i][j][1]=f[g[i][j-1]][j-1][1]+f[i][j-1][1];
//By SiriusRen
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
int n,xx,x,m,g[100050][20],f[100050][20][2],rech,rec;
double ans=0x3fffffff;
struct Node{int height,position;}node[100050];
struct Node2{int height,Short,position;}jy;
struct Edge{int to,to2,weight,weight2;void init(){weight=weight2=0x3fffffff;}}edge[100050];
set<Node>s;set<Node2>p;
bool operator < (Node a,Node b){return a.height<b.height;}
bool operator < (Node2 a,Node2 b){
if(a.Short!=b.Short)return a.Short<b.Short;
else return a.height<b.height;
}
void init(){
for(int i=n;i>=1;i--){
p.clear();edge[i].init();
s.insert(node[i]);
set<Node>::iterator it=s.find(node[i]),it2=it;
if((++it)!=s.end()){
jy.height=(*it).height;
jy.position=(*it).position;
jy.Short=abs((*it).height-node[i].height);
p.insert(jy);
}
else goto deal1;
if((++it)!=s.end()){
jy.height=(*it).height;
jy.position=(*it).position;
jy.Short=abs((*it).height-node[i].height);
p.insert(jy);
}
deal1:if(it2==s.begin())goto deal;
if((--it2)==s.begin()){
jy.height=(*it2).height;
jy.position=(*it2).position;
jy.Short=abs((*it2).height-node[i].height);
p.insert(jy);
goto deal;
}
else
{
jy.height=(*it2).height;
jy.position=(*it2).position;
jy.Short=abs((*it2).height-node[i].height);
p.insert(jy);
}
it2--;
jy.height=(*it2).height;
jy.position=(*it2).position;
jy.Short=abs((*it2).height-node[i].height);
p.insert(jy);
deal:set<Node2>::iterator itp=p.begin();
if(itp!=p.end()){
edge[i].weight=(*itp).Short;
edge[i].to=(*itp).position;
if((++itp)!=p.end()){
edge[i].weight2=(*itp).Short;
edge[i].to2=(*itp).position;
}
}
}
}
signed main(){
scanf("%lld",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&node[i].height);
node[i].position=i;
}
init();
for(int i=1;i<=n;i++){
g[i][0]=edge[edge[i].to2].to;
f[i][0][0]=edge[i].weight2;
f[i][0][1]=edge[edge[i].to2].weight;
}
for(int j=1;j<=18;j++){
for(int i=1;i<=n;i++){
g[i][j]=g[g[i][j-1]][j-1];
f[i][j][0]=f[g[i][j-1]][j-1][0]+f[i][j-1][0];
f[i][j][1]=f[g[i][j-1]][j-1][1]+f[i][j-1][1];
}
}
scanf("%lld%lld",&x,&m);
for(int i=1;i<=n;i++){
int wei1=0,wei2=0,S=i,temp=x;
for(int j=17;j>=0;j--){
if(temp>=f[S][j][0]+f[S][j][1]&&g[S][j]){
wei1+=f[S][j][0];
wei2+=f[S][j][1];
temp=temp-(f[S][j][0]+f[S][j][1]);
S=g[S][j];
}
}
if(temp>=edge[S].weight2&&edge[S].to2){
temp-=edge[S].weight2;
wei1+=edge[S].weight2;
}
if(wei2&&(ans>1.0*wei1/wei2||(ans==1.0*wei1/wei2&&rech<node[i].height))){
rech=node[i].height;
rec=i;
ans=1.0*wei1/wei2;
}
}
printf("%lld\n",rec);
for(int i=1;i<=m;i++){
scanf("%lld%lld",&xx,&x);
int wei1=0,wei2=0,S=xx,temp=x;
for(int j=17;j>=0;j--){
if(temp>=f[S][j][0]+f[S][j][1]&&g[S][j]){
wei1+=f[S][j][0];
wei2+=f[S][j][1];
temp=temp-(f[S][j][0]+f[S][j][1]);
S=g[S][j];
}
}
if(temp>=edge[S].weight2&&edge[S].to2){
temp-=edge[S].weight2;
wei1+=edge[S].weight2;
}
printf("%lld %lld\n",wei1,wei2);
}
}
NOIP2012 T3开车旅行 set+倍增的更多相关文章
- 【NOIP2012】开车旅行(倍增)
题面 Description 小A 和小B决定利用假期外出旅行,他们将想去的城市从1到N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i的海拔高度为Hi,城市 ...
- Luogu 1081 【NOIP2012】开车旅行 (链表,倍增)
Luogu 1081 [NOIP2012]开车旅行 (链表,倍增) Description 小A 和小B决定利用假期外出旅行,他们将想去的城市从1到N 编号,且编号较小的城市在编号较大的城市的西边,已 ...
- 2012Noip提高组Day1 T3 开车旅行
题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的 城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为 Hi,城市 ...
- 【vijos1780】【NOIP2012】开车旅行 倍增
题目描述 有\(n\)个城市,第\(i\)个城市的海拔为\(h_i\)且这\(n\)个城市的海拔互不相同.编号比较大的城市在东边.两个城市\(i,j\)之间的距离为\(|h_i-h_j|\) 小A和小 ...
- NOIP2012开车旅行 【倍增】
题目 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为Hi,城市 i 和城 ...
- $Noip2012\ Luogu1081$ 开车旅行 倍增优化$ DP$
Luogu Description Sol 1.发现对于每个城市,小A和小B的选择是固定的,可以预处理出来,分别记为ga[],gb[] 2.并且,只要知道了出发城市和出发天数,那么当前城市和小A,小B ...
- vijos P1780 【NOIP2012】 开车旅行
描述 小\(A\)和小\(B\)决定利用假期外出旅行,他们将想去的城市从\(1\)到\(N\)编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市\(i\)的海拔高度为 ...
- noip2012 P1081 开车旅行
小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为Hi,城市 i 和城市 j ...
- luogu1081 开车旅行 树上倍增
题目大意 小A和小B决定利用假期外出旅行,他们将想去的城市从1到N编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市i 的海拔高度为Hi,城市i 和城市j 之间的距离 ...
随机推荐
- OAuth2建立webapi认证服务供自己的客户端使用--密码模式
场景:你自己实现了一套webApi,想供自己的客户端调用,又想做认证. 第一步:通过vs2015建立web api项目,Startup.cs,这个类将会作为Owin的启动类. 第二步:在webapi. ...
- Hua Wei 机试题目一
一.身份证号码验证 题目描述: 我国公民的身份证号码特点如下:1. 长度为18位:2. 第1-17位只能为数字:3. 第18位可以是数字或者小写英文字母x.4. 身份证号码的第7~14位表示持有人生日 ...
- .net 三大核心对象
.net 三大核心对象 HttpRequest 现在总算轮到第一个核心对象出场了.MSDN给它作了一个简短的解释:“使 ASP.NET 能够读取客户端在 Web 请求期间发送的 HTTP 值.”这个解 ...
- SQL where 条件顺序对性能的影响有哪些
经常有人问到oracle中的Where子句的条件书写顺序是否对SQL性能有影响,我的直觉是没有影响,因为如果这个顺序有影响,Oracle应该早就能够做到自动优化,但一直没有关于这方面的确凿证据.在网上 ...
- 401 - Unauthorized: Access is denied due to invalid credentials.
solution:change application pool from ApplicationPoolIdentity to NetworkService.
- iproute2+tc notes
iproute2+tc notes The iproute2+tc package allows access to the variety of neat new networking featur ...
- (转)JobTracker和TaskTracker概述
一 概述: (1)Hadoop MapReduce采用Master/Slave结构. *Master:是整个集群的唯一的全局管理者,功能包括:作业管理.状态监控和任务调度等,即MapReduce中的J ...
- luogu p1004
P1004 题意 类似一个比较小的方格(N<=9),有的点是0,有的点有数, A->B的路径经过的点加上该点代表的数,求两次A->B的最大解(最优解) 一个令人恼的问题是两条路径如果 ...
- [读书笔记] R语言实战 (十四) 主成分和因子分析
主成分分析和探索性因子分析是用来探索和简化多变量复杂关系的常用方法,能解决信息过度复杂的多变量数据问题. 主成分分析PCA:一种数据降维技巧,将大量相关变量转化为一组很少的不相关变量,这些无关变量称为 ...
- input只能输入数字或两位小数
/** * [只能输入数字和两位小数] * 举例:<input type="text" onkeyup="num(this)" size="10 ...