打卡第四天 P1081 [NOIP2012 提高组] 开车旅行

news2024/10/7 5:32:35

今天是我打卡第四天,做个省选/NOI−题吧(#^.^#)

原题链接:[NOIP2012 提高组] 开车旅行 - 洛谷

题目描述

输入格式 

输出格式 

输入输出样例

输入 #1

4 
2 3 1 4 
3 
4 
1 3 
2 3 
3 3 
4 3

输出 #1

1 
1 1 
2 0 
0 0 
0 0 

输入 #2

10 
4 5 6 1 2 3 7 8 9 10 
7 
10 
1 7 
2 7 
3 7 
4 7 
5 7 
6 7 
7 7 
8 7 
9 7 
10 7

输出 #2

2 
3 2 
2 4 
2 1 
2 4 
5 1 
5 1 
2 1 
2 0 
0 0 
0 0

说明/提示

【样例1说明】

 【样例2说明】

【数据范围与约定】

C++

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+100,INF=2e9;
int da[20][N][5],db[20][N][5],f[20][N][5],h[N],s[N],x[N],n,x0,m,la,lb,ansid;
double ans=INF*1.0;
struct City{
	int id,h;
	friend bool operator <(City a,City b){
		return a.h<b.h;
	}
};
multiset<City> q;
void init(){
	h[0]=INF,h[n+1]=-INF;
	City start;
	start.id=0,start.h=INF;
	q.insert(start),q.insert(start);
	start.id=n+1,start.h=-INF;
	q.insert(start),q.insert(start);
	for(int i=n;i;i--){
		City now;
		now.id=i;
		now.h=h[i];
		q.insert(now);
		auto it=q.lower_bound(now);
		it--;
		int lastd=(*it).id,	lasth=(*it).h;
		it++;
		it++; 
		int nextd=(*it).id,nexth=(*it).h;
		it--;
		int ga,gb;
		if(abs(nexth-h[i])>=abs(lasth-h[i]))
		{
			gb=lastd;
			it--,it--;
			if(abs(nexth-h[i])>=abs((*it).h-h[i]))
			{
				ga=(*it).id;
			} 
			else 
			{
				ga=nextd;
			}
		
		} 
		else {
			gb=nextd;
			it++,it++;
			if(abs((*it).h-h[i])>=abs(lasth-h[i])){
				ga=lastd;
			}
			else {
				ga=(*it).id;
			} 
			
		
		}
		f[0][i][0]=ga,f[0][i][1]=gb;
		da[0][i][0]=abs(h[i]-h[ga]);
		db[0][i][1]=abs(h[i]-h[gb]);
	}
}
 
void fun_dp()  
{
	for(int i=1;i<=18;i++)
	for(int j=1;j<=n;j++)
	for(int k=0;k<2;k++)
	{
		if(i==1)f[1][j][k]=f[0][f[0][j][k]][1-k],da[1][j][k]=da[0][j][k]+da[0][f[0][j][k]][1-k],db[1][j][k]=db[0][j][k]+db[0][f[0][j][k]][1-k];
		else f[i][j][k]=f[i-1][f[i-1][j][k]][k],da[i][j][k]=da[i-1][j][k]+da[i-1][f[i-1][j][k]][k],db[i][j][k]=db[i-1][j][k]+db[i-1][f[i-1][j][k]][k];
	}
}
 
void calc(int s,int x){
	int p=s;
	la=0,lb=0;
	for(int i=18;i>=0;i--)if(f[i][p][0]&&la+lb+da[i][p][0]+db[i][p][0]<=x)la+=da[i][p][0],lb+=db[i][p][0],p=f[i][p][0];
} 
void ans1(){
	for(int i=1;i<=n;i++){
		calc(i,x0);  
		double lla=(double)la/(double)lb;
		if(lla<ans){
			ans=lla;
			ansid=i;
		}
		else if(lla==ans)if(h[i]>h[ansid])ansid=i;
	} 
	cout<<ansid<<endl;
}
void ans2(){
	for(int i=1;i<=m;i++){
		calc(s[i],x[i]);
		printf("%d %d\n",la,lb);
	}
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)
	cin>>h[i];
	cin>>x0>>m;
	for(int i=1;i<=m;i++)
	cin>>s[i]>>x[i];
	init();
	fun_dp();
	ans1();
	ans2();
	return 0;
}

Java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Main {
	public static int MAXN = 100001;
	public static int MAXP = 20;
	public static int[] arr = new int[MAXN];
	public static int[] to1 = new int[MAXN];
	public static int[] dist1 = new int[MAXN];
	public static int[] to2 = new int[MAXN];
	public static int[] dist2 = new int[MAXN];
	public static int[][] rank = new int[MAXN][2];
	public static int[] last = new int[MAXN];
	public static int[] next = new int[MAXN];
	public static int[][] stto = new int[MAXN][MAXP + 1];
	public static int[][] stab = new int[MAXN][MAXP + 1];
	public static int[][] sta = new int[MAXN][MAXP + 1];
	public static int[][] stb = new int[MAXN][MAXP + 1];
	public static int n, m, x0;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StreamTokenizer in = new StreamTokenizer(br);
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		in.nextToken();
		n = (int) in.nval;
		for (int i = 1; i <= n; i++) {
			in.nextToken();
			arr[i] = (int) in.nval;
		}
		near();
		st();
		in.nextToken();
		x0 = (int) in.nval;
		out.println(best());
		in.nextToken();
		m = (int) in.nval;
		for (int i = 1, s, x; i <= m; i++) {
			in.nextToken();
			s = (int) in.nval;
			in.nextToken();
			x = (int) in.nval;
			travel(s, x);
			out.println(a + " " + b);
		}
		out.flush();
		out.close();
		br.close();
	}

	public static void near() {
		for (int i = 1; i <= n; i++) {
			rank[i][0] = i;
			rank[i][1] = arr[i];
		}
		Arrays.sort(rank, 1, n + 1, (a, b) -> a[1] - b[1]);
		for (int i = 1; i <= n; i++) {
			last[rank[i][0]] = i == 1 ? 0 : rank[i - 1][0];
			next[rank[i][0]] = i == n ? 0 : rank[i + 1][0];
		}
		for (int i = 1; i <= n; i++) {
			to1[i] = 0;
			dist1[i] = 0;
			to2[i] = 0;
			dist2[i] = 0;
			update(i, last[i]);
			update(i, last[last[i]]);
			update(i, next[i]);
			update(i, next[next[i]]);
			delete(i);
		}
	}
	public static void update(int i, int r) {
		if (r == 0) {
			return;
		}
		int d = Math.abs(arr[i] - arr[r]);
		if (to1[i] == 0 || d < dist1[i] || (d == dist1[i] && arr[r] < arr[to1[i]])) {
			to2[i] = to1[i];
			dist2[i] = dist1[i];
			to1[i] = r;
			dist1[i] = d;
		} else if (to2[i] == 0 || d < dist2[i] || (d == dist2[i] && arr[r] < arr[to2[i]])) {
			to2[i] = r;
			dist2[i] = d;
		}
	}

	public static void delete(int i) {
		int l = last[i];
		int r = next[i];
		if (l != 0) {
			next[l] = r;
		}
		if (r != 0) {
			last[r] = l;
		}
	}

	public static void st() {
		for (int i = 1; i <= n; i++) {
			stto[i][0] = to1[to2[i]];
			stab[i][0] = dist2[i] + dist1[to2[i]];
			sta[i][0] = dist2[i];
			stb[i][0] = dist1[to2[i]];
		}
		for (int p = 1; p <= MAXP; p++) {
			for (int i = 1; i <= n; i++) {
				stto[i][p] = stto[stto[i][p - 1]][p - 1];
				if (stto[i][p] != 0) {
					stab[i][p] = stab[i][p - 1] + stab[stto[i][p - 1]][p - 1];
					sta[i][p] = sta[i][p - 1] + sta[stto[i][p - 1]][p - 1];
					stb[i][p] = stb[i][p - 1] + stb[stto[i][p - 1]][p - 1];
				}
			}
		}
	}

	public static int best() {
		int ans = 0;
		double min = Double.MAX_VALUE, cur;
		for (int i = 1; i < n; i++) {
			travel(i, x0);
			cur = (double) a / (double) b;
			if (ans == 0 || cur < min || (cur == min && arr[i] > arr[ans])) {
				min = cur;
				ans = i;
			}
		}
		return ans;
	}

	public static int a, b;

	public static void travel(int s, int x) {
		a = 0;
		b = 0;
		for (int p = MAXP; p >= 0; p--) {
			if (stab[s][p] != 0 && x >= stab[s][p]) {
				x -= stab[s][p];
				a += sta[s][p];
				b += stb[s][p];
				s = stto[s][p];
			}
		}
		if (dist2[s] <= x) {
			a += dist2[s];
		}
	}

}

Pascal

type
        ar=array[1..3]of int64;
var
        h,down1,down2,z,zz,l,r:array[0..100000]of longint;
        f:array[1..100000,1..20]of ar;
        n,m,i,j,x0,a,b:longint;
        x:ar;
function edge(x,y:longint):longint;
begin
        exit(abs(h[x]-h[y]));
end;
function jump(x,y:longint):ar;
var
        i:longint;
begin
        fillchar(jump,sizeof(jump),0);
        jump[3]:=x;
        for i:=20 downto 1 do
                if(f[jump[3],i,3]>0)and(f[jump[3],i,1]+f[jump[3],i,2]+jump[1]+jump[2]<=y)then
                begin
                        inc(jump[1],f[jump[3],i,1]);
                        inc(jump[2],f[jump[3],i,2]);
                        jump[3]:=f[jump[3],i,3];
                end;
        if f[jump[3],1,1]+jump[1]+jump[2]<=y then
        begin
                inc(jump[1],f[jump[3],1,1]);
                jump[3]:=down2[jump[3]];
        end;
end;
procedure kp(l,r:longint);
var
        i,j,k,t:longint;
begin
        i:=l;
        j:=r;
        k:=h[z[(l+r)div 2]];
        repeat
                while h[z[i]]<k do
                        inc(i);
                while h[z[j]]>k do
                        dec(j);
                if i<=j then
                begin
                        t:=z[i];
                        z[i]:=z[j];
                        z[j]:=t;
                        inc(i);
                        dec(j);
                end;
        until i>j;
        if i<r then
                kp(i,r);
        if j>l then
                kp(l,j);
end;
procedure build(x:longint);
var
        ll,rr:longint;
begin
        ll:=x;
        rr:=x;
        while(ll>0)and(ll<=x)do
                ll:=l[ll];
        while(rr<n)and(rr<=x)do
                rr:=r[rr];
        if ll=0 then
                down1[x]:=rr
        else
                if rr>n then
                        down1[x]:=ll
        else
                if edge(x,ll)<=edge(x,rr) then
                        down1[x]:=ll
        else
                down1[x]:=rr;
        r[ll]:=rr;l[rr]:=ll;
        if(down1[x]=ll)and(ll>0)then
        begin
                ll:=l[ll];
                while(ll>0)and(ll<=x)do
                        ll:=l[ll];
        end
        else
                if rr<=n then
                begin
                        rr:=r[rr];
                        while(rr<n)and(rr<=x)do
                                rr:=r[rr];
                end;
        if ll=0 then down2[x]:=rr
        else
                if rr>n then
                        down2[x]:=ll
        else
                if edge(x,ll)<=edge(x,rr) then
                        down2[x]:=ll
        else
                down2[x]:=rr;
        if(down1[x]<1)or(down1[x]>n)then
                down1[x]:=0;
        if(down2[x]<1)or(down2[x]>n)then
                down2[x]:=0;
end;
begin
        readln(n);
        for i:=1 to n do
                read(h[i]);
        readln;
        for i:=0 to n+1 do
                z[i]:=i;
        kp(1,n);
        for i:=1 to n do
        begin
                l[z[i]]:=z[i-1];
                r[z[i]]:=z[i+1];
        end;
        for i:=1 to n do
                build(i);
        for i:=1 to n do
                if down2[i]>0 then
                begin
                        f[i,1,1]:=edge(i,down2[i]);
                        f[i,1,3]:=down2[i];
                        if down1[down2[i]]>0 then
                        begin
                                f[i,1,2]:=edge(down2[i],down1[down2[i]]);
                                f[i,1,3]:=down1[down2[i]];
                        end;
                end;
        for j:=2 to 20 do
                for i:=1 to n do
                        if(f[i,j-1,3]>0)and(f[f[i,j-1,3],j-1,3]>0)then
                        begin
                                f[i,j,1]:=f[i,j-1,1]+f[f[i,j-1,3],j-1,1];
                                f[i,j,2]:=f[i,j-1,2]+f[f[i,j-1,3],j-1,2];
                                f[i,j,3]:=f[f[i,j-1,3],j-1,3];
                        end;
        readln(x0);
        a:=0;
        b:=0;
        j:=0;
        for i:=1 to n do
        begin
                x:=jump(i,x0);
                if(a=0)and(b=0)then
                begin
                        j:=i;
                        a:=x[1];
                        b:=x[2];
                end
                else
                if(b=0)and(x[2]=0)and(h[i]>h[j])then
                begin
                        j:=i;
                        a:=x[1];
                        b:=x[2];
                end
                else
                if(x[2]<>0)and((b=0)or(x[1]/x[2]<a/b))then
                begin
                        j:=i;
                        a:=x[1];
                        b:=x[2];
                end;
        end;
        writeln(j);
        readln(m);
        for i:=1 to m do
        begin
                readln(a,b);
                x:=jump(a,b);
                writeln(x[1],' ',x[2]);
        end;
end.

下一期要我讲什么题?
打在评论区哦,随机挑选一条评论(#^.^#),看过我的文章之后,能不能用您珍贵的小手手点赞吧,求求┭┮﹏┭┮ 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2193573.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

k8s 中存储之 hostPath 卷

目录 1 hostPath 卷介绍 2 hostPath 卷实际应用操作 2.1 创建 pod 资源类型 2.2 修改清单文件增加 hostPath 对应的参数配置 2.3 查看是否创建 卷 和 pod 2.4 创建发布文件测试是否正常访问 1 hostPath 卷介绍 EmptyDir中数据不会被持久化&#xff0c;它会随着Pod的结束而销…

每日一题|2187. 完成旅途的最少时间|二分法、计数器

本题的一个思路是从小到大遍历全部可能的t&#xff0c;并分别计算当前每一辆车所能够行驶的最多trips数量。 但是如果从1开始&#xff0c;结束在最不理想的情况是max(time) * totalTrip&#xff0c;在数据很大的时候很容易time out。 所以不妨对每一个可能的t重新思考性质。随…

STL的位图:bitset

引言 在C标准模板库&#xff08;STL&#xff09;中&#xff0c;bitset是一种用于表示固定大小序列的位集合的容器。每个位&#xff08;bit&#xff09;可以被独立地设置或清除&#xff0c;即它可以单独地表示0或1。bitset在处理二进制数据时非常有用&#xff0c;尤其是在需要节…

linux安装百度网盘

版本20.04 下载 deb&#xff1a;debian系列&#xff0c;Ubuntu下载这个 rpm&#xff1a;redhat系列 安装 在Downloads中找到&#xff0c;打开终端&#xff0c;使用命令安装 sudo dpkg -i 名称 //dpkg:Debian package在所有文件中找到。

Linux相关概念和易错知识点(12)(命令行参数、环境变量、本地变量)

1.命令行参数 &#xff08;1&#xff09;main函数的参数int argc和char* argv[]是什么&#xff1f; main函数可以带参数&#xff0c;即int main(int argc, char* argv[])&#xff0c;(int argc, char* argv[])叫做命令行参数列表&#xff0c;int argc叫参数的个数&a…

【YOLO学习】YOLOv3详解

文章目录 1. 网络结构1.1 结构介绍1.2 改进 2. 训练与测试过程3. 总结 1. 网络结构 1.1 结构介绍 1. 与 YOLOv2 不同的是&#xff0c;YOLOv3 在 Darknet-19 里加入了 ResNet 残差连接&#xff0c;改进之后的模型叫 Darknet-53。在 ImageNet上 实验发现 Darknet-53 相对于 ResN…

VSCODE驯服日记(三):配置C++环境

1. 下载mingw64&#xff0c;解压后把bin并添加到环境变量 1>编译器介绍 mingw&#xff1a;专为windowsgcc&#xff1a;多平台msvc &#xff1a;windows&#xff0c;且配合vs使用更佳 注意与调试器gdb和lldb的区别 2. 安装vscode插件&#xff1a; 安装C/C插件 安装code ru…

力扣之1322.广告效果

题目&#xff1a; sql建表语句&#xff1a; Create table If Not Exists Ads (ad_id int,user_id int,action ENUM (Clicked, Viewed, Ignored) ); Truncate table Ads; insert into Ads (ad_id, user_id, action) values (1, 1, Clicked); insert into Ads (ad_id, use…

Sublime Text 下载地址分享

Sublime Text官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘123云盘为您提供Sublime Text最新版正式版官方版绿色版下载,Sublime Text安卓版手机版apk免费下载安装到手机,支持电脑端一键快捷安装https://www.123684.com/s/kPxoTd-dCnxHSublime Text官方版下载丨最新版下…

双十一可以买什么物品?重磅推荐五款好用品牌!

距离今年的双十一盛典仅剩数十日&#xff0c;您是否已将心爱商品添加至购物车中了呢&#xff1f;还在犹豫未满载的朋友也无需焦虑&#xff0c;特意为您精选了五款好用的宝贝推荐&#xff0c;旨在为您的购物清单增添几分灵感与便捷&#xff0c;期待能为您的双十一购物之旅增添一…

GPU 是否有朝一日可以取代 CPU?

讨论 GPU 是否能够取代 CPU&#xff0c;需要从两者的基本架构、设计目的、性能表现、应用领域等多个方面进行分析。虽然你提到的4060显卡的核心频率接近服务器 CPU 的频率&#xff0c;这看起来似乎有一些相似性&#xff0c;但 GPU 和 CPU 的设计思路和适用场景差异显著&#xf…

18448 最小生成树

### 思路 使用Kruskal算法求解图的最小生成树。Kruskal算法通过对所有边按权值排序&#xff0c;然后逐步选择最小权值的边&#xff0c;确保不会形成环&#xff0c;直到构建出最小生成树。 ### 伪代码 1. 读取输入的结点数n和边数m。 2. 读取每条边的信息&#xff0c;存储在边列…

羊城杯2024WP

羊城杯-2024 web web2 进题信息搜集一下&#xff0c;dirsearch发现了login路由可访问&#xff0c;先随便点一下&#xff0c;发现了一个文件读取&#xff1a; http://139.155.126.78:30148/lyrics?lyricsRain.txt 我尝试了一下&#xff1a; http://139.155.126.78:30148/lyrics…

【教学类-77-02】20241007青花瓷纹理纸(手工)

背景需求&#xff1a; 大班《我是中国人》主题下&#xff0c;有一个“青花瓷”的主题&#xff0c;各种平面绘画 这些青花瓷花瓶、盘子都是平面的&#xff0c;我想能不能做个立体的&#xff0c;所以前期设计了“青花瓷立体卡”【教学类-77-01】20241005青花瓷立体书-CSDN博客文…

构建 10 万卡 GPU 集群的技术挑战

构建 10 万卡 GPU 集群的技术挑战 摘要 揭示AI训练集群关键基础设施挑战&#xff0c;探讨突破现有AI瓶颈的必要性与10万GPU集群&#xff08;如OpenAI、Meta&#xff09;建设所面临挑战与需求。 构建网络拓扑&#xff0c;需权衡多层交换机成本、带宽与维护。本文对比Ethernet与…

JDBC 快速入门

JDBC 快速入门 搭建步骤代码实现数据库java 代码 搭建步骤 准备数据库官网下载数据库连接驱动jar 包。https://downloads.mysql.com/archives/c-j/创建 java 项目&#xff0c;在项目下创建 lib 文件夹&#xff0c;将下载的驱动 jar 包复制到文件夹里选中 lib 文件夹右键 ->…

通信工程学习:什么是ICP网络内容服务商

ICP&#xff1a;网络内容服务商 ICP&#xff0c;全称Internet Content Provider&#xff0c;即网络内容服务商&#xff0c;是指那些通过互联网向用户提供各种类型内容服务的组织或个人。ICP在数字化时代扮演着至关重要的角色&#xff0c;它们不仅是信息的传播者&#xff0c;更是…

微服务获取用户信息和OpenFeign传递用户

问题一&#xff1a; 网关已经完成登录校验并获取登录用户身份信息。但是当网关将请求转发到微服务时&#xff0c;微服务又该如何获取用户身份呢&#xff1f; 由于网关发送请求到微服务依然采用的是Http请求&#xff0c;因此我们可以将用户信息以请求头的方式传递到下游微服务…

机器人技术基础(1-3章坐标变换)

位置矢量的意思是B坐标系的原点O相对于A坐标系的平移变换后的矩阵&#xff1a; 齐次坐标最后一个数表示缩放倍数&#xff1a; 左边的是T形变换矩阵&#xff0c;右边的是需要被变换的矩阵&#xff1a;T形变换矩阵的左上角表示旋转&#xff0c;右上角表示平移&#xff0c;左下角最…

使用 NVIDIA H100 上的 Azure 机密计算释放隐私保护 AI 的潜力

通过 NVIDIA H100 上的 Azure 机密计算释放隐私保护 AI 的潜力 文章目录 前言一、机密计算二、使用 NVIDIA H100 Tensor Core GPU 的 Azure 机密计算1. 安全功能2. 可扩展性和可编程性三、场景1. 模型机密性2. 推理/提示机密性3. 使用私有数据进行微调4. 多方培训结论前言 这是…