Codeforces Round #295 D. Cubes [贪心 set map]
3 seconds
256 megabytes
standard input
standard output
Once Vasya and Petya assembled a figure of m cubes, each of them is associated with a number between 0 and m - 1 (inclusive, each number appeared exactly once). Let's consider a coordinate system such that the OX is the ground, and the OY is directed upwards. Each cube is associated with the coordinates of its lower left corner, these coordinates are integers for each cube.
The figure turned out to be stable. This means that for any cube that is not on the ground, there is at least one cube under it such that those two cubes touch by a side or a corner. More formally, this means that for the cube with coordinates (x, y) either y = 0, or there is a cube with coordinates (x - 1, y - 1), (x, y - 1) or (x + 1, y - 1).
Now the boys want to disassemble the figure and put all the cubes in a row. In one step the cube is removed from the figure and being put to the right of the blocks that have already been laid. The guys remove the cubes in such order that the figure remains stable. To make the process more interesting, the guys decided to play the following game. The guys take out the cubes from the figure in turns. It is easy to see that after the figure is disassembled, the integers written on the cubes form a number, written in the m-ary positional numerical system (possibly, with a leading zero). Vasya wants the resulting number to be maximum possible, and Petya, on the contrary, tries to make it as small as possible. Vasya starts the game.
Your task is to determine what number is formed after the figure is disassembled, if the boys play optimally. Determine the remainder of the answer modulo 109 + 9.
The first line contains number m (2 ≤ m ≤ 105).
The following m lines contain the coordinates of the cubes xi, yi ( - 109 ≤ xi ≤ 109, 0 ≤ yi ≤ 109) in ascending order of numbers written on them. It is guaranteed that the original figure is stable.
No two cubes occupy the same place.
In the only line print the answer to the problem.
3
2 1
1 0
0 1
19
5
0 0
0 1
0 2
0 3
0 4
2930
题意:有m个方块 每个方块有一个值 并且是堆起来稳定的 一个方块可以拿掉当且仅当剩下的还是稳定的 双方轮流拿 从左到右放组成一个m进制的数 (转自 http://blog.csdn.net/u011686226/article/details/44036875)
| 10129550 | 2015-03-03 11:00:30 | njczy2010 | D - Cubes | GNU C++ | Accepted | 499 ms | 32596 KB |
| 10129479 | 2015-03-03 10:52:18 | njczy2010 | D - Cubes | GNU C++ | Wrong answer on test 21 | 421 ms | 21600 KB |
| 10129457 | 2015-03-03 10:49:33 | njczy2010 | D - Cubes | GNU C++ | Wrong answer on test 21 | 436 ms | 21700 KB |
| 10129412 | 2015-03-03 10:44:12 | njczy2010 | D - Cubes | GNU C++ | Wrong answer on test 3 | 0 ms | 41100 KB |
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 100005
#define M 10005
#define mod 1000000007
//#define p 10000007
//#define mod2 1000000009
#define ll long long
#define ull unsigned long long
#define LL long long
#define eps 1e-6
//#define inf 2147483647
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n;
int x[N],y[N];
map<pair<int,int>,int>mp;
int r[N];
set<int>s;
int ans[N];
int vis[N];
ll mod2=; int ok(int i)
{
int nx,ny,te;
nx=x[i]-;ny=y[i]+;
te=mp[ make_pair(nx,ny) ];
if(te!= && r[te-]==){
return ;
}
nx=x[i];
te=mp[ make_pair(nx,ny) ];
if(te!= && r[te-]==){
return ;
}
nx=x[i]+;
te=mp[ make_pair(nx,ny) ];
if(te!= && r[te-]==){
return ;
}
return ;
} void ini()
{
int i;
int nx,ny;
s.clear();
memset(r,,sizeof(r));
memset(vis,,sizeof(vis));
for(i=;i<n;i++){
scanf("%d%d",&x[i],&y[i]);
mp[ make_pair(x[i],y[i]) ]=i+;
}
for(i=;i<n;i++){
nx=x[i]-;ny=y[i]-;
if(mp[ make_pair(nx,ny) ]!=){
r[i]++;
}
nx=x[i];
if(mp[ make_pair(nx,ny) ]!=){
r[i]++;
}
nx=x[i]+;
if(mp[ make_pair(nx,ny) ]!=){
r[i]++;
}
}
for(i=;i<n;i++){
if(ok(i)==){
s.insert(i);
vis[i]=-;
}
}
} void changeerase(int i)
{
int nx,ny,te;
nx=x[i]-;ny=y[i]-;
te=mp[ make_pair(nx,ny) ];
if(te!=){
if(vis[te-]==-){
vis[te-]=;s.erase(te-);
}
return;
}
nx=x[i];
te=mp[ make_pair(nx,ny) ];
if(te!=){
if(vis[te-]==-){
vis[te-]=;s.erase(te-);
}
return;
}
nx=x[i]+;
te=mp[ make_pair(nx,ny) ];
if(te!=){
if(vis[te-]==-){
vis[te-]=;s.erase(te-);
}
return;
}
} void changeadd(int i)
{
int nx,ny,te;
nx=x[i]-;ny=y[i]-;
te=mp[ make_pair(nx,ny) ];
if(te!= && ok(te-)==){
s.insert(te-);
vis[te-]=-;
}
nx=x[i];
te=mp[ make_pair(nx,ny) ];
if(te!= && ok(te-)==){
s.insert(te-);
vis[te-]=-;
}
nx=x[i]+;
te=mp[ make_pair(nx,ny) ];
if(te!= && ok(te-)==){
s.insert(te-);
vis[te-]=-;
}
} void updata(int i)
{
int nx,ny,te;
nx=x[i]-;ny=y[i]+;
te=mp[ make_pair(nx,ny) ];
if(te!=){
r[te-]--;
if(r[te-]==)
changeerase(te-);
}
nx=x[i];
te=mp[ make_pair(nx,ny) ];
if(te!=){
r[te-]--;
if(r[te-]==)
changeerase(te-);
}
nx=x[i]+;
te=mp[ make_pair(nx,ny) ];
if(te!=){
r[te-]--;
if(r[te-]==)
changeerase(te-);
}
} void solve()
{
int i,index;
set<int>::iterator it;
for(i=;i<n;i++){
if(i%==){
it=s.end();
it--;
ans[i]=*it;
}
else{
it=s.begin();
ans[i]=*it;
}
index=*it;
s.erase(*it);
mp[ make_pair(x[index],y[index]) ]=;
vis[index]=;
updata(index);
changeadd(index);
}
} void out()
{
int i;
ll aa=;
/*
for(i=0;i<n;i++){
printf("%d",ans[i]);
}
printf("\n");*/
for(i=;i<n;i++){
aa=(aa*(ll)n)%mod2;
aa=(aa+(ll)ans[i])%mod2;
}
printf("%I64d\n",aa);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
//while(T--)
//scanf("%d%d",&n,&m);
while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
}
return ;
}
Codeforces Round #295 D. Cubes [贪心 set map]的更多相关文章
- Codeforces Round #295 (Div. 2)
水 A. Pangram /* 水题 */ #include <cstdio> #include <iostream> #include <algorithm> # ...
- codeforces 521a//DNA Alignment// Codeforces Round #295(Div. 1)
题意:如题定义的函数,取最大值的数量有多少? 结论只猜对了一半. 首先,如果只有一个元素结果肯定是1.否则.s串中元素数量分别记为a,t,c,g.设另一个串t中数量为a',t',c',g'.那么,固定 ...
- Educational Codeforces Round 11——A. Co-prime Array(map+vector)
A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Educational Codeforces Round 64 -B(贪心)
题目链接:https://codeforces.com/contest/1156/problem/B 题意:给一段字符串,通过变换顺序使得该字符串不包含为位置上相邻且在字母表上也相邻的情况,并输出. ...
- Codeforces Round #295 (Div. 2) B. Two Buttons 520B
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons
题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 #include<iostream> ...
- Codeforces Round #295 (Div. 2)C - DNA Alignment 数学题
C. DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #295 (Div. 2)B - Two Buttons BFS
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #295 (Div. 2)A - Pangram 水题
A. Pangram time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
随机推荐
- 【学习笔记】深入理解js原型和闭包(18)——补充:上下文环境和作用域的关系
本系列用了大量的篇幅讲解了上下文环境和作用域,有些人反映这两个是一回儿事.本文就用一个小例子来说明一下,作用域和上下文环境绝对不是一回事儿. 再说明之前,咱们先用简单的语言来概括一下这两个的区别. 0 ...
- ag-grid-vue 本地删除,不重新刷新数据
// 这是本地删除,不重新刷新数据 that.gridListOptions.api.updateRowData({ remove: [that.submitTransmitData] });
- Android模板制作
本文详细介绍模板相关的知识和如何制作Android模版及使用,便于较少不必要的重复性工作.比如我在工作中如果要创建一个新的模块,就不要需要创建MVP相关的几个类:Model.View.Presente ...
- 如何实现Windows宿主系统和虚拟机ubuntu系统文件互相访问
我的宿主操作系统是Windows 10,使用Oracle的Virtual Box安装了Ubuntu. 因为工作需要我经常得在两个系统之间互相拷贝一些数据,下面是具体步骤,可以实现Windows 10和 ...
- python调用脚本或shell的方式
python调用脚本或shell有下面三种方式: os.system()特点:(1)可以调用脚本.(2)可以判断是否正确执行.(3)满足不了标准输出 && 错误 commands模块特 ...
- uva1611 Crane
类似煎饼,先把1放到1,之后是子问题 (先放到前一半,再放到开头,两次操作)(任何位置,最多一次就可以放到前一半)) #include<iostream> #include<ve ...
- project .mpp 查看当天工作任务 1.选择自己 2.选择起始和终止时间 就显示当天的任务了
project .mpp 查看当天工作任务 1.选择自己 2.选择起始和终止时间 就显示当天的任务了
- Gear Pump: Why Install A Pressure Reducing Valve?
When the Gear Pump Manufacturers prompts to install a gear pump, the following points should ...
- jquery实现密码强度检测
jquery实现密码强度验证 jquery实现密码强度验证 JS代码: $('#pass').keyup(function(e) { var strongRegex = new RegExp( ...
- 含有通配符*的字符匹配(C语言)
含有通配符的字符匹配,采用贪心算法 //1 -> true //0 -> false int IsMatch(const char* reg, const char *str) { int ...