CF 436(DIV.2)c

C. Bus
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 0 it immediately goes back to the point x = a and so on. Thus, the bus moves from x = 0 to x = a and back. Moving from the point x = 0 to x = a or from the point x = a to x = 0 is called a bus journey. In total, the bus must make k journeys.

The petrol tank of the bus can hold b liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.

There is a gas station in point x = f. This point is between points x = 0 and x = a. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain b liters of gasoline.

What is the minimum number of times the bus needs to refuel at the point x = f to make k journeys? The first journey starts in the point x = 0.

Input

The first line contains four integers a, b, f, k (0 < f < a ≤ 106, 1 ≤ b ≤ 109, 1 ≤ k ≤ 104) — the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys.

Output

Print the minimum number of times the bus needs to refuel to make k journeys. If it is impossible for the bus to make k journeys, print -1.

Examples
Input
Copy
6 9 2 4
Output
Copy
4
Input
Copy
6 10 2 4
Output
Copy
2
Input
Copy
6 5 4 3
Output
Copy
-1

题意:

        给你一段路长度为a,要你走k次,在位置f有一个加油站,车一开始有b个单位的油,问你最少要加多少次油。

思路:

        很明显我们可以把车程分类,从左到右和从右到左,然后再分类,最后一次和不是最后一次,经过这2次分类,答案就很明显了。

code:

        

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<functional>
typedef long long LL;
#define maxn 300005
using namespace std;
vector<int>arr[maxn];
LL n, a, b, k, f,flag,ans,d1,d2,mark,now;
int main()
{
	cin >> a >> b >> f >> k;
	d1 = f, d2 = a - f;
	mark = 1;
	now = b;
	while (k&&!flag)
	{
		if (mark%2)
		{
			if (now < d1)
			{
				flag = 1;
				break;
			}
			else
			{
				now -= d1;
				if (now < 2 * d2&&k!=1)
				{
					if (b < d2)
					{
						flag = 1;
						break;
					}
					else
					{
						now = b - d2;
						ans++;
					}
				}
				else if (k == 1)
				{
					if (now < d2)
					{
						if (b < d2)
						{
							flag = 1;
							break;
						}
						else
						{
							ans++;
						}
					}
				}
				else if (now >= d2*2)
				{
					now -= d2;
				}
			}
		}
		else
		{
			if (now < d2)
			{
				flag = 1;
				break;
			}
			else
			{
				now -= d2;
				if (now < d1 * 2 && k != 1)
				{
					if (b < d1)
					{
						flag = 1;
						break;
					}
					else
					{
						ans++;
						now = b - d1;
					}
				}
				else if (k == 1)
				{
					if (now < d1)
					{
						if (b < d1)
						{
							flag = 1;
							break;
						}
						else
						{
							ans++;
						}
					}
				}
				else if (now >= 2 * d1)
				{
					now -= d1;
				}
			}
		}
		mark++;
		k--;
	}
	if (!flag)
		cout << ans << endl;
	else
		cout << -1 << endl;
	return 0;
}

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>高德地图</title> <script src="https://gitee.com/dcloud/uni-app/raw/dev/dist/uni.webview.1.6.4.js"></script> <style> #map-container { width: 100%; height: 584px; border-radius: 22px; z-index: 4; } .amap-marker img { display: none; /* 隐藏默认图标 */ } </style> </head> <body> <div id="map-container"></div> <script> // 安全配置 window._AMapSecurityConfig = { securityJsCode: 'cf331d54f67e306ca5c0b17f7122d559' }; </script> <script src="https://webapi.amap.com/maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster"></script> <script> // 确保UniApp通信桥接准备就绪 function setupUniBridge() { return new Promise(resolve => { if (window.uni && typeof uni.postMessage === 'function') { return resolve(); } document.addEventListener('UniAppJSBridgeReady', resolve); }); } // 向UniApp发送消息 async function sendMessageToUniApp(data) { await setupUniBridge(); if (window.uni && typeof uni.postMessage === 'function') { uni.postMessage({ data: [JSON.stringify(data)] // 必须包装在数组中 }); } else if (window.parent) { window.parent.postMessage({ type: 'webviewMessage', data: JSON.stringify(data) }, '*'); } } // 解析URL参数 const params = new URLSearchParams(location.search); const points = JSON.parse(params.get('points') || '[]'); const markers = JSON.parse(params.get('markers') || '[]'); // 初始化地图 const map = new AMap.Map('map-container', { zoom: 10, center: points.length ? [points[0].lng, points[0].lat] : [116.397428, 39.90923], showBuildingBlock: true }); // 1. 绘制路线 if (points.length > 0) { const path = points.map(p => [p.lng, p.lat]); new AMap.Polyline({ path: path, strokeColor: "#32CD32", strokeWeight: 5, map: map }); // 添加起点终点标记 points.forEach((point, i) => { new AMap.Marker({ position: [point.lng, point.lat], offset: new AMap.Pixel(-10, -10), map: map }); }); } // 2. 初始化聚合点(使用官方推荐方式) let cluster; // 存储聚合实例 function initCluster() { // 转换数据格式为高德官方要求的格式 const clusterPoints = markers.map(marker => ({ lnglat: [marker.lng, marker.lat], // 注意属性名是lnglat title: marker.title, originalData: marker })); // 销毁旧实例(如果存在) if (cluster) { cluster.setMap(null); } // 创建聚合实例 cluster = new AMap.MarkerCluster(map, clusterPoints, { gridSize: 80, // 聚合网格像素大小 maxZoom: 18, // 最大聚合级别 minClusterSize: 2, // 最小聚合数量 // 自定义聚合点样式(官方推荐方式) renderClusterMarker: function(context) { const count = context.count; const div = document.createElement('div'); div.style.background = '#FF9A44'; div.style.borderRadius = '50%'; div.style.width = '40px'; div.style.height = '40px'; div.style.display = 'flex'; div.style.alignItems = 'center'; div.style.justifyContent = 'center'; div.style.color = 'white'; div.style.fontWeight = 'bold'; div.style.zIndex = '99'; div.innerHTML = count; return div; }, // 自定义非聚合点样式 renderMarker: function(context) { const markerData = context.data[0].originalData; const marker = new AMap.Marker({ position: [markerData.lng, markerData.lat], content: ` <div style="position:relative; text-align:center;z-index:100;"> <img src="https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-1.png" style="width:40px; height:50px; display:block;"> <div style="position:absolute; bottom:-90%; left:50%; transform:translateX(-50%); background: rgba(227, 57, 59, 0.1); padding:6px 0; border-radius: 18px; white-space:nowrap; font-size: 12px;font-weight: bold; margin-top:5px;border: 2px solid #F47C58;color:#E4393C;width:80px;"> ${markerData.title} </div> </div> `, offset: new AMap.Pixel(-20, -40) }); // 绑定点击事件 marker.on('click', async function(e) { await sendMessageToUniApp({ type: 'markerClick', data: markerData }); }); return marker; } }); // 绑定聚合点点击事件 cluster.on('click', async function(e) { await sendMessageToUniApp({ type: 'clusterClick', data: { count: e.clusterData.count, center: e.clusterData.center, markers: e.clusterData.markers } }); }); } // 3. 地图加载完成后初始化聚合点 map.on('complete', function() { if (markers.length > 0) { initCluster(); } }); </script> </body> </html> 聚合点还是默认图标,没有失效自定义样式
最新发布
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值