[codeforces 200 A Cinema]暴力,优化
题意大致是这样的:有一个有n行、每行m个格子的矩形,每次往指定格子里填石子,如果指定格子里已经填过了,则找到与其曼哈顿距离最小的格子,然后填进去,有多个的时候依次按x、y从小到大排序然后取最小的。输出每次填的格子的坐标。
思路:这道题出自Codeforces Round #126 (Div. 2)是个暴力优化的题。如果指定格子未填,则填到里面。否则枚举曼哈顿距离,然后枚举格子找答案。裸的暴力太慢了,主要是因为每次曼哈顿距离都是从1开始搜索,如果每次指定的坐标都是同一个,则做了大量的重复工作。不妨用一个数组r[x][y]表示与(x,y)这个格子曼哈顿距离不超过r[x][y]的格子全部被填过了。r数组满足这样一个关系,r[x][y]>=r[x'][y']-dist{(x,y),(x',y')},每次使用r[x][y]之前,用(x,y)周围的一些点更新下就行了。复杂度比较模糊,必须承认,这种优化简直太神,对于极端数据和随机数据都灰常之快
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/* ******************************************************************************** */#include <iostream> //#include <cstdio> //#include <cmath> //#include <cstdlib> //#include <cstring> //#include <vector> //#include <ctime> //#include <deque> //#include <queue> //#include <algorithm> //#include <map> //#include <cmath> //using namespace std; // //#define pb push_back //#define mp make_pair //#define X first //#define Y second //#define all(a) (a).begin(), (a).end() //#define fillchar(a, x) memset(a, x, sizeof(a)) // //typedef pair<int, int> pii; //typedef long long ll; //typedef unsigned long long ull; // //#ifndef ONLINE_JUDGE //void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);} //void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R> //void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1; //while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T> //void print(const T t){cout<<t<<endl;}template<typename F,typename...R> //void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T> //void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;} //#endif // ONLINE_JUDGE //template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);} //template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);} //template<typename T> //void V2A(T a[],const vector<T>&b){for(int i=0;i<b.size();i++)a[i]=b[i];} //template<typename T> //void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];} // //const double PI = acos(-1.0); //const int INF = 1e9 + 7; // ///* -------------------------------------------------------------------------------- */const int maxn = 2e3 + 7;bool plot[maxn][maxn];int r[maxn][maxn];int n, m, k;int check(int row, int col) { if (col >= 0 && col < m) return true; return false;}int main() {#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout);#endif // ONLINE_JUDGE cin >> n >> m >> k; for (int i = 0; i < k; i ++) { int x, y; scanf("%d%d", &x, &y); x --; y --; if (!plot[x][y]) { printf("%d %d\n", x + 1, y + 1); plot[x][y] = true; continue; } for (int d = 1; d <= 3; d ++) { int Max = min(n, x + d + 1); for (int row = max(0, x - d); row < Max; row ++) { int t = d - abs(x - row), col1 = y - t, col2 = y + t; if (check(row, col1)) umax(r[x][y], r[row][col1] - d); if (check(row, col2)) umax(r[x][y], r[row][col2] - d); } } for (int d = r[x][y] + 1; ; d ++) { int Max = min(n, x + d + 1); bool ok = false; for (int row = max(0, x - d); row < Max; row ++) { int t = d - abs(x - row), col1 = y - t, col2 = y + t; if (check(row, col1) && !plot[row][col1]) { ok = true; printf("%d %d\n", row + 1, col1 + 1); plot[row][col1] = true; break; } if (check(row, col2) && !plot[row][col2]) { ok = true; printf("%d %d\n", row + 1, col2 + 1); plot[row][col2] = true; break; } } if (ok) { r[x][y] = d - 1; break; } } } return 0;}/* ******************************************************************************** */ |
[codeforces 200 A Cinema]暴力,优化的更多相关文章
- Codeforces 977F - Consecutive Subsequence - [map优化DP]
题目链接:http://codeforces.com/problemset/problem/977/F 题意: 给定一个长度为 $n$ 的整数序列 $a[1 \sim n]$,要求你找到一个它最长的一 ...
- Codeforces 1129D - Isolation(分块优化 dp)
Codeforces 题目传送门 & 洛谷题目传送门 又独立切了道 *2900( 首先考虑 \(dp\),\(dp_i\) 表示以 \(i\) 为结尾的划分的方式,那么显然有转移 \(dp_i ...
- codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...
- codeforces 897A Scarborough Fair 暴力签到
codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: ...
- Codeforces 626E Simple Skewness(暴力枚举+二分)
E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...
- Codeforces A. Playlist(暴力剪枝)
题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces 843D (Dijkstra算法的优化,动态最短路)
题面 (http://codeforces.com/problemset/problem/843/D) 题目大意: 给定一张带权无向图,有q次操作 操作有两种 1 v 询问1到v的最短路 2 c 将边 ...
- [TJOI2017]城市 【树的直径+暴力+优化】
Online Judge:Luogu P3761 Label:树的直径,暴力 题目描述 从加里敦大学城市规划专业毕业的小明来到了一个地区城市规划局工作.这个地区一共有n座城市,n-1条高速公路,保证了 ...
- 牛客寒假基础集训营 | Day1 E-rin和快速迭代(暴力 + 优化)
E-rin和快速迭代 题目描述 rin最近喜欢上了数论. 然而数论实在太复杂了,她只能研究一些简单的问题. 这天,她在研究正整数因子个数的时候,想到了一个"快速迭代"算法.设 f( ...
随机推荐
- Missing artifact net.sf.json-lib:json-lib:jar:2.2.3
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib --><dependency> <gro ...
- C++ 11 +,开坑。
最近换新工作了.工作中需要用到高端的c++11的一些操作,至于我后面又plus一下还是因为可能是c++14或者17中提供的一些操作.反正都是c++11以及之后的一些特性. 首先,今天看一下关于函数模板 ...
- 基于nodejs的游戏服务器
开源一个四年前自己写的node服务器,有兴趣的可以继续开发-- 架构为mysql,redis,node. 数据格式为 protocol buff 如果只做简单的演示,这个架构非常适合你.. 还是typ ...
- Spring Cloud Gateway+Nacos,yml+properties两种配置文件方式搭建网关服务
写在前面 网关的作用不在此赘述,举个最常用的例子,我们搭建了微服务,前端调用各服务接口时,由于各服务接口不一样,如果让前端同事分别调用,前端同事会疯的.而网关就可以解决这个问题,网关屏蔽了各业务服务的 ...
- 关于json转义中文
服务器传递或者程序传递中,不识别读取到的JSON数据中 \u开头的数据. PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据. 网上很多,但是其实都是错误的,正确的方法是在j ...
- 《JAVA8开发指南》使用流式操作
为什么需要流式操作 集合API是Java API中最重要的部分.基本上每一个java程序都离不开集合.尽管很重要,但是现有的集合处理在很多方面都无法满足需要. 一个原因是,许多其他的语言或者类库以声明 ...
- Auth认证中的think_auth_rule type字段干嘛用的?
昨晚认真研究了一下这个类,设计的很巧妙,但是你说的这个字段,我认为应该是作者想加功能但还没写,在session判断的地方可以看到,type这个字段实际是对应的 1-实时验证,2登陆验证 ,显然,这个字 ...
- wincache 与 zend guard 的冲突
ZendLoader.dll 与wincache.dll 同时开启 问题分析:zend与wincache冲突 解决方法: 关掉wincache: 在php.ini中的 extension=php_w ...
- 【Linux常见命令】tail命令
tail - output the last part of files tail 命令可用于查看文件的内容,有一个常用的参数 -f 常用于查阅正在改变的日志文件. tail -f filename ...
- vue中递归组件的使用
2019独角兽企业重金招聘Python工程师标准>>> 递归 简单来讲就是程序自己调用自身. vue中的递归组件就是,组件自身调用自身. 父组件 <template> & ...