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原型和闭包(0)——目录
文章转载:https://www.cnblogs.com/wangfupeng1988/p/4001284.html 说明: 本篇文章一共16篇章,外加两篇后补的和一篇自己后来添加的学习笔记,一共19 ...
- redis集群架构(含面试题解析)
老规矩,我还是以循序渐进的方式来讲,我一共经历过三套集群架构的演进! Replication+Sentinel 这套架构使用的是社区版本推出的原生高可用解决方案,其架构图如下! 这里Sentinel的 ...
- ubuntu下安装mongo扩展
安装openssl apt-get install openssl libssl-dev libssl0.9.8 libgtk2.0-dev 安装php-pear apt-get install ph ...
- 10道有关ios的题
1.你使用过Objective-C的运行时编程(Runtime Programming)么?如果使用过,你用它做了什么?你还能记得你所使用的相关的头文件或者某些方法的名称吗? 2.你实现过多线程的Co ...
- os x 中出现message sent to deallocated instance 的错误总结
一般是程序中的某一个对象被release 了两次 一般情况下是与你定义的类型有关 这里面我的错误是吧 NSString 类型的变量的属性 设置为了 assign 了 目测与这个有关 补充object- ...
- Android(java)学习笔记176: 远程服务的应用场景(移动支付案例)
一. 移动支付: 用户需要在移动终端提交账号.密码以及金额等数据 到 远端服务器.然后远端服务器匹配这些信息,进行逻辑判断,进而完成交易,返回交易成功或失败的信息给移动终端.用户提交账号. ...
- diff - 找出两个文件的不同点
总览 diff [选项] 源文件 目标文件 描述 在最简单的情况是, diff 比较两个文件的内容 (源文件 和 目标文件). 文件名可以是 - 由标准输入设备读入的文本. 作为特别的情况是, dif ...
- 浅谈p值(p-value是什么)
当我们说到p-value时,我们在说什么? “这个变量的p-value小于0.05,所以这个变量很重要” ........ 你真的知道自己在说什么么???这个p-value到底是个什么鬼?为什么小于0 ...
- H5里div多行显示省略号
display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: ; overflow: hidden; -webkit- ...
- net core 使用ef生成实体类(SqlServer)
1)打开程序包管理器控制台 2)输入命令 Install-Package Microsoft.EntityFrameworkCore.SqlServer 3)输入命令 Install-Packag ...