每日一题●1月10日 AcWing 1987. 粉刷栅栏

1987. 粉刷栅栏

农夫约翰发明了一种绝妙的方法来粉刷牛棚旁边的长栅栏(把栅栏想象成一维的数轴)。

他只需要在他最喜欢的奶牛贝茜身上挂一个刷子,然后在一旁悠闲的喝凉水就行了。

贝茜沿着栅栏来回走动时,会将她走过的栅栏部分涂上油漆。

贝茜从栅栏上的位置 0 处开始,共进行 N 次移动。

移动可能形如 10 L,表示向左移动 10 单位距离,也可能形如 15 R,表示向右移动 15 单位距离。

给定贝茜的 N 次移动列表,约翰想知道至少被涂抹了 2 层油漆的区域的总长度。

整个行进过程中,贝茜距离出发地的距离不会超过 109。

输入格式

第一行包含一个整数 N。

接下来 N 行,每一行包含一个行动指令,诸如 10 L 或 15 R。

输出格式

输出至少被涂抹了 2 层油漆的区域的总长度。

数据范围

1≤N≤105
整个行进过程中,贝茜距离出发地的距离不会超过 109。
每次指令移动距离的取值范围是 [1,2×109]。

输入样例:

6
2 R
6 L
1 R
8 L
1 R
2 R

输出样例:

6

样例解释

共有 6 单位长度的区域至少被涂抹 2 层油漆。

这些区域为 (−11,−8),(−4,−3),(0,2)

code
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
map<int,int> m;
int main()
{
	char op;
	int d,n;
	cin>>n;
	int now=0;
	while(n--){
		cin>>d>>op;
		if(op=='R') m[now]++,now+=d,m[now]--;
		else m[now]--,now-=d,m[now]++;
	}
	int last=-1,s=0,res=0;
	for(auto &pos:m){
		if(s>1) res+=pos.first-last;//上次的距离 
		last=pos.first;
		s+=pos.second;//累加的值就是其粉刷次数  
	}
	cout<<res; 
}

仍然是差分+离散化,用map来离散化要方便得多。这里有点绕的地方是,粉刷的是区域,而走的地方是一个点,map[a]>0表示的是[a,a+i)被粉刷。画图会更容易理解。
请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
)分析: 1. 计算柱子表面积:柱子的表面积可以分成底部的椭圆形面积和侧面的矩形面积,通过数学公式计算。 2. 计算需要的油漆桶数:根据每桶油漆可以粉刷的面积和柱子表面积计算需要的油漆桶数。 3. 最终输出结果。 代码如下: EllipticalCylinder.java ```java public class EllipticalCylinder { private double longRadius; // 长半径 private double shortRadius; // 短半径 private double height; // 高 public EllipticalCylinder(double longRadius, double shortRadius, double height) { this.longRadius = longRadius; this.shortRadius = shortRadius; this.height = height; } // 计算底部椭圆形面积 public double getBottomArea() { return Math.PI * longRadius * shortRadius; } // 计算侧面矩形面积 public double getSideArea() { return 2 * Math.PI * shortRadius * height + 2 * Math.PI * longRadius * height; } // 计算柱子表面积 public double getTotalArea() { return getBottomArea() + getSideArea(); } } ``` PaintCalculator.java ```java public class PaintCalculator { private static final double AREA_PER_BUCKET = 70; // 每桶油漆可粉刷的面积 public static double calculatePaintBuckets(EllipticalCylinder cylinder) { double totalArea = cylinder.getTotalArea(); // 计算柱子表面积 double buckets = totalArea / AREA_PER_BUCKET; // 计算需要的油漆桶数 return buckets; } } ``` Main.java ```java public class Main { public static void main(String[] args) { EllipticalCylinder cylinder = new EllipticalCylinder(4, 3, 10); double buckets = PaintCalculator.calculatePaintBuckets(cylinder); System.out.printf("需要油漆 %.1f 桶", buckets); } } ``` 输出结果: ``` 需要油漆 10.2 桶 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值