cf #310 E. Case of Chocolate (upper_bound())
2015-12-19 15:10
337 查看
题目:http://codeforces.com/contest/556/problem/E
题意:一块只有左上角的巧克力,每次从对角线位置向左或者向上吃,并且只能连续地吃。有q吃查询,问每次能吃的长度。
E. Case of Chocolate
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 ≤ n, xi + 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
output
input
output
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.
分析:由于x+y=n+1,对于一次向上吃,它能吃的长度能由上方吃过的离它最近的那一次得到。向坐吃同理。分情况讨论就行了。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 1E9+9;
struct node
{
int s,t,dir; //1代表上,0代表左
node(int a=0,int b=0,int c=0):
s(a),t(b),dir(c){}
bool operator < (const node &t)const
{
return s<t.s;
}
};
set <node> st;
set <int> visit;
int main()
{
typedef set <node>::iterator its;
int n,q,x,y;
char op[2];
scanf("%d%d",&n,&q);
st.insert(node(0,1,1));
st.insert(node(n+1,1,0));
while(q--)
{
scanf("%d%d%s",&x,&y,op);
if(visit.find(x)!=visit.end())
{
puts("0");
continue ;
}
visit.insert(x);
if(op[0]=='U')
{
its it=st.upper_bound(node(x));
if(it->dir==1)
{
printf("%d\n",it->t-x+1);
st.insert(node(x,it->t,1));
}
else
{
printf("%d\n",it->s-x);
st.insert(node(x,it->s-1,1));
}
// printf("info : %d %d %d\n",it->s,it->t,it->dir);
}
else
{
its it=--st.upper_bound(node(x));
if(it->dir==1)
{
printf("%d\n",x-it->s);
st.insert(node(x,it->s+1,0));
}
else
{
printf("%d\n",x-it->t+1);
st.insert(node(x,it->t,0));
}
// printf("info : %d %d %d\n",it->s,it->t,it->dir);
}
}
return 0;
}
题意:一块只有左上角的巧克力,每次从对角线位置向左或者向上吃,并且只能连续地吃。有q吃查询,问每次能吃的长度。
E. Case of Chocolate
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 ≤ n, xi + 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.
分析:由于x+y=n+1,对于一次向上吃,它能吃的长度能由上方吃过的离它最近的那一次得到。向坐吃同理。分情况讨论就行了。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 1E9+9;
struct node
{
int s,t,dir; //1代表上,0代表左
node(int a=0,int b=0,int c=0):
s(a),t(b),dir(c){}
bool operator < (const node &t)const
{
return s<t.s;
}
};
set <node> st;
set <int> visit;
int main()
{
typedef set <node>::iterator its;
int n,q,x,y;
char op[2];
scanf("%d%d",&n,&q);
st.insert(node(0,1,1));
st.insert(node(n+1,1,0));
while(q--)
{
scanf("%d%d%s",&x,&y,op);
if(visit.find(x)!=visit.end())
{
puts("0");
continue ;
}
visit.insert(x);
if(op[0]=='U')
{
its it=st.upper_bound(node(x));
if(it->dir==1)
{
printf("%d\n",it->t-x+1);
st.insert(node(x,it->t,1));
}
else
{
printf("%d\n",it->s-x);
st.insert(node(x,it->s-1,1));
}
// printf("info : %d %d %d\n",it->s,it->t,it->dir);
}
else
{
its it=--st.upper_bound(node(x));
if(it->dir==1)
{
printf("%d\n",x-it->s);
st.insert(node(x,it->s+1,0));
}
else
{
printf("%d\n",x-it->t+1);
st.insert(node(x,it->t,0));
}
// printf("info : %d %d %d\n",it->s,it->t,it->dir);
}
}
return 0;
}
相关文章推荐
- 浅析STL中的常用算法
- STL区间成员函数及区间算法总结
- c++ STL容器总结之:vertor与list的应用
- C++在成员函数中使用STL的find_if函数实例
- 关于STL中list容器的一些总结
- 关于STL中的map容器的一些总结
- 浅析stl序列容器(map和set)的仿函数排序
- STL list链表的用法详细解析
- stl容器set,map,vector之erase用法与返回值详细解析
- STl中的排序算法详细解析
- 关于STL中vector容器的一些总结
- 关于STL中set容器的一些总结
- 简单说说STL的内存管理
- STL与泛型编程(1)---模板
- CppUtest发现的STL容器内存泄漏问题
- STL中算法
- STL简单应用
- vector-list-deque
- 三十分钟掌握STL
- 1.sort()