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 ...
随机推荐
- UISegmentedControl去掉背景色与UIScrollView联动
UISegmentControl分段控制器是UIKit框架提供的一组按钮栏,提供多个可选的按钮,只能激活其中的一个,响应事件.主要用来在同一层次重要性下不同的信息展示或者不同的界面展示之间切换.例如手 ...
- python2和python3的区别(转)
基本语法差异 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3只支持uni ...
- RxJava的map方法与flatMap方法
简单讲,map和flatMap都是来完成Observable构造的数据到Observer接收数据的一个转换,这么说有点绕
- java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】
java比较日期大小及日期与字符串的转换[SimpleDateFormat操作实例] package com.ywx.test; import java.text.ParseException; im ...
- PMP项目管理学习笔记(9)——范围管理
关于范围管理的几个名词定义 产品范围:表示你和你的团队正在构建的产品或服务的特性和功能:产品范围与最终产品有关,包括产品的特性,组件和组成部分.人们谈论确定产品的范围时,大多都是在谈论确定产品的特性, ...
- 【HEVC帧间预测论文】P1.4 Motion Vectors Merging: Low Complexity Prediction Unit Decision
Motion Vectors Merging: Low Complexity Prediction Unit Decision Heuristic for the inter-Prediction o ...
- 从源码中无法看出函数所在的js脚本的解决方法
通过设置断点调试使js脚本自动出现
- Mac 查看端口情况
一个进程可以占用多个端口. 查看某个进程占用哪些端口: lsof -nP | grep TCP | grep -i 进程名 ➜ cocos_creator lsof -nP | grep TCP | ...
- 自定义分隔符|/i|/x|/xs|需要转译
小骆驼 第八章 用正则表达式进行匹配 #!/usr/bin/envperl use strict; use warnings; $_ ="#adchbehnyhme3534f\nvdh5ej ...
- loadClass和forName 的区别
类的加载方式 1.隐式加载:new 2.显式加载,loadClass,forName 等 loadClass 和 forName的区别 类的装载过程 1.加载:通过classLoader加载class ...