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 ...
随机推荐
- hihocoder offer收割编程练习赛9 B 水陆距离
思路: 宽搜,多个起点. 实现: #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- C# 获取本机IP(优化项目实际使用版)
好一段时间没来更新博客了,因为密码实在记不住,烦死了,密码干脆直接用那个找回密码链接的一部分. 吐槽完说正事了,关于C# 获取本机IP的,最开始用的是下面的,但是因为获取IP的有点多,而且难判断,忽 ...
- 在uwp仿IOS的页面切换效果
有时候我们需要编写一些迎合IOS用户使用习惯的uwp应用,我在这里整理一下仿IOS页面切换效果的代码. 先分析IOS的页面切换.用户使用左右滑动方式进行前进和后退,播放类似于FlipView的切换动画 ...
- 大数据开发学习之构建Hadoop集群-(0)
有多种方式来获取hadoop集群,包括从其他人获取或是自行搭建专属集群,抑或是从Cloudera Manager 或apach ambari等管理工具来构建hadoop集群等,但是由自己搭建则可以了解 ...
- Python 语言规范
Python 语言规范 pychecker 对你的代码运行pychecker 定义: pychecker 是一个在Python 源代码中查找bug 的工具. 对于C 和C++这样的不那 么动态的( ...
- Android(java)学习笔记185:多媒体之设置全屏的方法
在实际的应用程序开发中,我们有时需要把 Activity 设置成全屏显示,一般情况下,可以通过两种方式来设置全屏显示效果.其一,通过在代码中可以设置,其二,通过manifest配置文件来设置全屏. 其 ...
- Using URL Schemes to Communicate with Apps
要素:1)通信双方:2)协议:3)支持机制(系统,进行协议注册):4)数据 https://developer.apple.com/library/content/documentation/iPho ...
- 请简述HTML和XHTML最重要的4点不同?
请简述HTML和XHTML最重要的4点不同? 不同: XHTML要求正确嵌套 XHTML 所有元素必须关闭 XHTML 区分大小 ...
- QT+信号有参数与无参数的实现+QT4和QT5在信号和槽使用上的区别
在QT5中,信号有参数和无参数 #ifndef SUBWIDGET_H #define SUBWIDGET_H #include <QWidget> #include <QPushB ...
- 字符串 || CodeForces 591B Rebranding
给一字符串,每次操作把字符串中的两种字母交换,问最后交换完的字符串是多少 arr数组记录每个字母最后被替换成了哪个字母 读入字符前面加一空格 scanf(" %c %c", &am ...