码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 555 C Case of Chocolate

时间:2015-07-19 21:43:05      阅读:438      评论:0      收藏:0      [点我收藏+]

标签:

一开始题目读错了,还以为可以从任意点为起点向上向左吃。
其实是只能从右边的边界为起点吃。
于是很明显,每一个横坐标最多只能出现一次,否则肯定是当前这个起点的巧克力已经被啃食了。
想到这里就更明显了,对于(xi,n+1-xi),若是向上吃,能够影响它的操作(xj,n+1-xj)肯定满足xj>xi,然后又明显一点,最小的xj肯定能影响到它。
我们来考虑操作(xj,n+1-xj),
若它是往左吃,很显然此时操作(xi,n+1-xi)能吃到被(xj,n+1-xj)吃掉的地方为止。
若它是往上吃呢?很显然无法影响到操作(xi,n+1-xi),为了简化问题,我们试试能否改变一些东西让它可以去影响。通过观察可以发现,它能吃到的终点的纵坐标很显然跟(xi,n+1-xi)操作能够吃到的终点的纵坐标一定是一样的。那么我们只需要将yj=n+1-xj,改为yj=(操作j能够吃到的终点的纵坐标)即可。于是,很高兴,我们构造出了统一的描述方法,让问题得到了很大的简化,又可以发现这种作法似乎有点像并查集。


接下来,讨论对于(xi,n+1-xi),若是向左吃的情况,这里根据上面的思路YY就一定可以知道结论。


于是全程维护一个map和两个映射数组就可以解决这个问题。

这两个数组的作用跟并查集的实现有着异曲同工之妙。


还是不懂的话,看看代码很快就能明白了。


#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
map<int,int>hash;
int x[200010],y[200010];
int main()
{
	int n,q;
	cin>>n>>q;
	hash[0]=hash[n+1]=q;
	while(q--)
	{
		char c;
		cin>>x[q]>>y[q]>>c;
		map<int,int>::iterator it=hash.lower_bound(x[q]);
		if(it->first==x[q])
		{
			puts("0");
			continue;
		}
		hash[x[q]]=q;
		if(c=='U')
		{
			printf("%d\n",y[q]-y[it->second]);
			y[q]=y[it->second];
		}
		else
		{
			it--;
			it--;
			printf("%d\n",x[q]-x[it->second]);
			x[q]=x[it->second];
		}
	}
}


time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.

A bar of chocolate can be presented as an n?×?n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let‘s call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction ‘up‘ or ‘left‘, and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.

After each action, he wants to know how many pieces he ate as a result of this action.

Input

The first line contains integers n (1?≤?n?≤?109) and q (1?≤?q?≤?2·105) — the size of the chocolate bar and the number of actions.

Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1?≤?xi,?yi?≤?nxi?+?yi?=?n?+?1) — the numbers of the column and row of the chosen cell and the character that represents the direction (L — left, U — up).

Output

Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.

Sample test(s)
input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
output
4
3
2
1
2
input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
output
9
1
10
6
0
2
Note

Pictures to the sample tests:

技术分享

The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.

In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.




版权声明:本文为博主原创文章,未经博主允许不得转载。

codeforces 555 C Case of Chocolate

标签:

原文地址:http://blog.csdn.net/stl112514/article/details/46958481

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!