传送门

D. Cubes
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

In the only line print the answer to the problem.

Sample test(s)
Input
3
2 1
1 0
0 1
Output
19
Input
5
0 0
0 1
0 2
0 3
0 4
Output
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]的更多相关文章

  1. Codeforces Round #295 (Div. 2)

    水 A. Pangram /* 水题 */ #include <cstdio> #include <iostream> #include <algorithm> # ...

  2. codeforces 521a//DNA Alignment// Codeforces Round #295(Div. 1)

    题意:如题定义的函数,取最大值的数量有多少? 结论只猜对了一半. 首先,如果只有一个元素结果肯定是1.否则.s串中元素数量分别记为a,t,c,g.设另一个串t中数量为a',t',c',g'.那么,固定 ...

  3. 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 ...

  4. Educational Codeforces Round 64 -B(贪心)

    题目链接:https://codeforces.com/contest/1156/problem/B 题意:给一段字符串,通过变换顺序使得该字符串不包含为位置上相邻且在字母表上也相邻的情况,并输出. ...

  5. 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 ...

  6. 【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons

    题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 #include<iostream> ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Kotlin学习的一些笔记

    Introduction 写在前面 关于本书 这本书适合你吗? 关于作者 介绍 什么是Kotlin? 我们通过Kotlin得到什么 准备工作 Android Studio 安装Kotlin插件 创建一 ...

  2. WEB前端JS与UI框架

    前端Js框架汇总 概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领域心花路放,也是煞是羡慕.寻了寻.net的消息,也是振奋人 ...

  3. windows常用bat脚本

    windows常用bat脚本 https://blog.csdn.net/longyan_csc/article/details/78737722 Windows_批处理+任务计划实现文件夹定时备份 ...

  4. DP || HYSBZ 1207 打鼹鼠

    n*n的网格,有m个鼹鼠,t时间会有一只鼹鼠出现在(x,y)点处,如果机器人也在这个点就可以打到鼹鼠 机器人初始位置任意,每秒可以移动一格,问最多打到多少鼹鼠 *解法:f[i]表示前i只鼹鼠打了多少个 ...

  5. Python 面向对象 特殊方法(魔法方法)

    Python 的特殊方法,两边带双下划线的方法. 比如:__init__(self, ...).__del__(self) 1.__init__(self,...) : 构造方法 __init__(s ...

  6. PHP09 字符串和正则表达式

    学习要点 字符串处理简介 常用的字符串输出函数 常用的字符串格式化函数 字符串比较函数 正则表达式简介 正则表达式语法规则 与perl兼容的正则表达式函数    字符串处理介绍 Web开发中字符串处理 ...

  7. BZOJ1232: [Usaco2008Nov]安慰奶牛cheer(最小生成树)

    题意:给一个图 需要找到一个子图使得所有点都连通 然后再选择一个点做为起点 走到每个点并回到起点 每条边,每个点被经过一次就要花费一次边权.点权 题解:肯定是找一颗最小生成树嘛 然后惊奇的发现 任意选 ...

  8. Spring-1-IOC

    IOC与DI的区别? IOC:控制反转(Inversion of Control是面向对象的一种设计原则,可以用来降低计算机之间的耦合度,其中最常见的是依赖注入).是实现的目标 DI:是实现IOC的一 ...

  9. Swift 中的Range和NSRange不同

    Swift中的Ranges和Objective-C中的NSRange有很大的不同,我发现在处理Swift中Ranges相关的问题的时候,总是要花费比我想象的更多的时间.不过,现在回过头来看看,发现Sw ...

  10. uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 无效的列类型: 1111; nested exception is java.sql.SQLException: 无效的列类型: 1111

    WHERE 的条件取值时添加上jdbcType=NUMBER这样的配置 参考[1]:https://blog.csdn.net/fyhjuyol/article/details/45483167