luogu 1369
给出平面上的n个点,请找出一个边与坐标轴平行的矩形,使得它的边界上有尽量多的点
模拟退火题解
$n^2$ 处理每行的前缀和与每列的前缀和
退火50次即可
#include <bits/stdc++.h> const int N = ; int n, A[N][N];
int sum_h[N][N], sum_l[N][N];
int Max_n = , Max_m = ; #define gc getchar() inline int read() {
int x = ;
char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} #define DB double const DB Max_tmp = , Del = 0.98; int Get_ans(int l, int r, int u, int d) {
if(l == r && u != d) {
return sum_l[d][l] - sum_l[u - ][l];
} else if(l != r && u == d) {
return sum_h[u][r] - sum_h[u][l - ];
} else if(l == r && u == d) {
return A[l][u];
} else {
int ret = ;
ret += (sum_h[u][r] - sum_h[u][l - ])
+ (sum_h[d][r] - sum_h[d][l - ])
+ (sum_l[d][l] - sum_l[u - ][l])
+ (sum_l[d][r] - sum_l[u - ][r]);
if(A[u][l]) ret --;
if(A[u][r]) ret --;
if(A[d][l]) ret --;
if(A[d][r]) ret --;
return ret;
}
} inline int MNTH() {
DB Now_tmp = Max_tmp;
int l = rand() % Max_m + , r = rand() % Max_m + , u = rand() % Max_n + , d = rand() % Max_n + ;
int Now_ans = Get_ans(l, r, u, d);
while(Now_tmp > 0.01) {
int l_ = l, r_ = r, u_ = u, d_ = d;
int opt = rand() % + ;
if(opt == ) l_ = rand() % Max_m + ;
else if(opt == ) r_ = rand() % Max_m + ;
else if(opt == ) u_ = rand() % Max_n + ;
else d_ = rand() % Max_n + ;
int Ls_ans = Get_ans(l_, r_, u_, d_);
if(Ls_ans > Now_ans || (Ls_ans < Now_ans && exp((Ls_ans - Now_ans) / Now_tmp) * RAND_MAX) >= rand())
Now_ans = Ls_ans, l = l_, r = r_, u = u_, d = d_;
Now_tmp *= Del;
}
return Now_ans;
} int main() {
srand(time() + );
n = read();
for(int i = ; i <= n; i ++) {
int x = read(), y = read();
A[x][y] = ;
Max_n = std:: max(Max_n, x), Max_m = std:: max(Max_m, y);
}
for(int i = ; i <= Max_n; i ++)
for(int j = ; j <= Max_m; j ++)
sum_h[i][j] = sum_h[i][j - ] + A[i][j];
for(int i = ; i <= Max_m; i ++)
for(int j = ; j <= Max_n; j ++)
sum_l[j][i] = sum_l[j - ][i] + A[j][i];
int T = ;
int Answer = -;
for(; T; T --) Answer = std:: max(Answer, MNTH());
printf("%d", Answer);
return ;
}
luogu 1369的更多相关文章
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- [luogu P2170] 选学霸(并查集+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...
- [luogu P2647] 最大收益(贪心+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...
- 1369 xth 砍树
1369 xth 砍树 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 在一个凉爽的夏夜,xth 和 ...
- Luogu 考前模拟Round. 1
A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...
- 1369 - Answering Queries(规律)
1369 - Answering Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 M ...
- luogu P2580 于是他错误的点名开始了
luogu P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...
- CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)
CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...
随机推荐
- Go实战--golang中使用redis(redigo和go-redis/redis)
开源库redigo的使用 github地址: https://github.com/garyburd/redigo 文档地址: http://godoc.org/github.com/garyburd ...
- Scala 面向对象编程之继承
extends关键字 // Scala中,让子类继承父类,与Java一样,也是使用extends关键字 // 继承就代表,子类可以从父类继承父类的field和method:然后子类可以在自己内部放入父 ...
- 模板模式(Template Pattern)
模板模式(Template Pattern) -- 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. ...
- 使用VS2012编译和使用C++ STL(STLport)
使用VS2012编译和使用C++ STL(STLport) http://cstriker1407.info/blog/use-vs2012-to-compile-and-use-the-c-stl- ...
- module 'cv2' has no attribute 'KNearest_create'
python版本:3.6.5 opencv版本:3.2.0 使用的是jupyter notebook 源代码如下: import cv2 import numpy as np import matpl ...
- iftop:Linux流量监测工具
一.安装须知: (1)iftop 必须以root运行 (2)安装前首先安装:libpcap和libcurses 及包驱动ncurses-devel libpcap-devel (2.1)libpc ...
- javscript函数的运用
函数,一段能够自动完成某些功能的代码块,函数的出现,既解决了重复使用重一功能的需求,又可以避免代码的臃肿性. 使用函数有两个要求:必须调用后才可以执行;函数名不要和关键字以及系统函数相同; 函数主要有 ...
- Canvas 绘制一个像素风电子时钟
想法是在 Canvas 上绘制由小方块组成的数字. 第一步是实现绘制小方块的方法,先画出一个边长为 5 的 10x10 个方块,使用两个 for 循环很简单就能完成. for (let i = 0; ...
- CVE-2019-11604 Quest KACE Systems Management Appliance <= 9.0 XSS
CVE-2019-11604 Quest KACE Systems Management Appliance CVE-2019-11604 Quest KACE Systems Management ...
- Android NDK 学习之接受Java传入Object数组
本博客主要是在Ubuntu 下开发,且默认你已经安装了Eclipse,Android SDK, Android NDK, CDT插件. 在Eclipse中添加配置NDK,路径如下Eclipse-> ...