[USACO13JAN]画栅栏Painting the Fence 洛谷p2205

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MrTinTin/article/details/83538047

题目描述

Farmer John has devised a brilliant method to paint the long fence next to his barn (think of the fence as a one-dimensional number line). He simply attaches a paint brush to his favorite cow Bessie, and then retires to drink a cold glass of water as Bessie walks back and forth across the fence, applying paint to any segment of the fence that she walks past.

Bessie starts at position 0 on the fence and follows a sequence of N moves (1 <= N <= 100,000). Example moves might be "10 L", meaning Bessie moves 10 units to the left, or "15 R", meaning Bessie moves 15 units to the right. Given a list of all of Bessie's moves, FJ would like to know what area of the fence gets painted with at least K coats of paint. Bessie will move at most 1,000,000,000 units away from the origin during her walk.

Farmer John 想出了一个给牛棚旁的长围墙涂色的好方法。(为了简单起见,我们把围墙看做一维的数轴,每一个单位长度代表一块栅栏)他只是简单的把刷子蘸满颜料,系在他最喜欢的奶牛Bessie上,然后让Bessie来回地经过围墙,自己则在一旁喝一杯冰镇的凉水。(……-_-|||) Bessie 经过的所有围墙都会被涂上一层颜料。Bessie从围墙上的位置0出发,并将会进行N次移动(1 <= N <= 100,000)。比如说,“10 L”的意思就是Bessie向左移动了10个单位。再比如说“15 R”的意思就是Bessie向右移动了15个单位。给出一系列Bessie移动的清单。FJ 想知道有多少块栅栏涂上了至少K层涂料。注意:Bessie最多会移动到离原点1,000,000,000单位远的地方。

输入输出格式

输入格式:

* 第1行: 两个整数: N K

* 第2...N+1 行: 每一行都描述了Bessie的一次移动。 (比如说 “15 L")

输出格式:

* 一个整数:被至少涂上K层涂料的栅栏数

(注意:输出的最后一定要输出换行符!否则会WA)

输入输出样例

输入样例#1: 复制

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

输出样例#1: 复制

6

说明

PS1:来源:usaco jan silver P01 想看原题的请戳http://www.usaco.org/index.php?page=viewproblem2&cpid=226)

PS2:测试数据也可以在在http://www.usaco.org/index.php?page=jan13problems上下载,还可以看到题解(不过是英文的:-D)

PS3:如果有翻译的问题或题目的不理解,可以在问答后面留言的说。

#include<bits/stdc++.h>
#define f(i,l,r) for(i=(l);i<=(r);i++)
using namespace std;
const int MAXN=100005;
string ch;
int a[MAXN],s[MAXN],b[MAXN];
int n,K,ans;
int main()
{
	ios::sync_with_stdio(false);
	int i,j,d;
	cin>>n>>K;
	f(i,2,n+1){
		cin>>d>>ch;
		if(ch=="L") a[i]=a[i-1]-d;
		else a[i]=a[i-1]+d;
		s[i]=a[i];
	}
	sort(s+1,s+2+n);
	int L=unique(s+1,s+2+n)-(s+1);
	f(i,2,n+1){
		int pos1=lower_bound(s+1,s+1+L,a[i])-s;
		int pos2=lower_bound(s+1,s+1+L,a[i-1])-s;
		if(pos1>pos2) swap(pos1,pos2);
		b[pos1]++;
		b[pos2]--;
	}
	f(i,1,L-1){
		b[i]+=b[i-1];
	//	cout<<s[i]<<"GG"<<endl; 
//		cout<<b[i]<<"GGG"<<endl;
		if(b[i]>=K){
			ans+=s[i+1]-s[i];
		}
	}
	cout<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/MrTinTin/article/details/83538047
今日推荐