题意:

网格图选中三个格,让你选中一些格子把这三个格子连起来,使得选中的格子总数最小。最后输出方案

网格范围为1000

思路:

首先两点间连起来最少需要的格子为他们的曼哈顿距离

然后连接方案一定是曼哈顿距离最短的两个点先连上,然后第三个点再接过去

然后题目就是求第三个点接到的那个点pos,答案就是path(a,pos)+path(b,pos)+path(c,pos)

求pos有两种方法

方法一:O(n2)

1e6枚举pos求最短即可,也能过

方法二:O(n)

首先第三个点一定在前两个点组成的矩形之外的,(不然他就不是第三个点了)

POS一定在前两个点组成的矩形的边界上,也是枚举即可。。

我写臭了。。还分了八个象限两种情况写的,可以参考一下添加路径的代码

其实比赛就是这样,口嗨能过就赶紧写,想优化说不定会花更长时间

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); vector<PI>ans;
PI a[];
int d(PI a, PI b){
return abs(a.fst-b.fst)+abs(a.sc-b.sc);
}
void link(PI a, PI b){
int x = a.fst;
int y = a.sc;
int dx,dy;
//printf("a:%d %d\nb:%d %d\n",x,y,b.fst,b.sc);
while(make_pair(x,y)!=b){
if(b.fst==x)dx=;
else dx=abs(b.fst-x)/(b.fst-x);
if(b.sc==y)dy=;
else dy=abs(b.sc-y)/(b.sc-y); if(dx!=)x+=dx;
else y+=dy;
//printf("yeh:%d %d\n",x,y);
if(x==b.fst&&y==b.sc)break;
ans.pb(make_pair(x,y));
}
return;
}
bool cmp(PI a, PI b){
if(a.fst==b.fst)return a.sc<b.sc;
return a.fst < b.fst;
}
int main(){
for(int i = ; i <= ; i++){
scanf("%d %d", &a[i].fst, &a[i].sc);
ans.pb(a[i]);
} int d1 = d(a[],a[]);
int d2 = d(a[],a[]);
int d3 = d(a[],a[]);
int dd = min(min(d1,d2),d3);
if(dd==d3){
swap(a[],a[]);
}
else if(dd == d2){
swap(a[],a[]);
}
PI pos=make_pair(-,-);
int x1 = min(a[].fst,a[].fst);int x2=max(a[].fst,a[].fst);
int y1 = min(a[].sc,a[].sc);int y2=max(a[].sc,a[].sc);
for(int i = ; i <= ; i++){
//printf("i:%d %d %d\n",i,a[i].fst,a[i].sc);
}
//printf("%d %d %d %d\n",x1,x2,y1,y2);
for(int dx = ; dx <= ; dx++){
int x = a[].fst+dx;
int y = a[].sc;
//printf(" %d %d %d\n",dx,x,y);
if(x>=x1&&x<=x2&&y>=y1&&y<=y2){
pos = make_pair(x,y);break;
}
x = a[].fst-dx;
if(x>=x1&&x<=x2&&y>=y1&&y<=y2){
pos = make_pair(x,y);break;
}
}
//printf(" %d %d\n",pos.fst,pos.sc);
for(int dy = ; dy <= ; dy++){
int x = a[].fst;
int y = a[].sc+dy;
if(x>=x1&&x<=x2&&y>=y1&&y<=y2){
pos = make_pair(x,y);break;
}
y = a[].sc-dy;
if(x>=x1&&x<=x2&&y>=y1&&y<=y2){
pos = make_pair(x,y);break;
}
}//printf(" %d %d\n",pos.fst,pos.sc);
if(pos.fst==-){
PI b[];
b[] = make_pair(x1,y1);
b[] = make_pair(x1,y2);
b[] = make_pair(x2,y1);
b[] = make_pair(x2,y2);
int ttmp = inf;
int pp = -;
for(int i = ; i <= ; i++){
if(ttmp>d(a[],b[i])){
ttmp=d(a[],b[i]);
pp=i;
}
}
pos=b[pp];
}
//printf(" %d %d\n",pos.fst,pos.sc);
//printf(" %d\n",ans.size());
link(a[],pos);
link(a[],pos);
link(a[],pos); if(pos!=a[]&&pos!=a[]&&pos!=a[])ans.pb(pos);
printf("%d\n",ans.size());
sort(ans.begin(), ans.end(), cmp);
for(int i = ; i < (int)ans.size(); i++){
printf("%d %d\n",ans[i].fst,ans[i].sc);
}
return ;
} /* */

Codeforces 1087C Connect Three (思维+模拟)的更多相关文章

  1. codeforces 887A Div. 64 思维 模拟

    A. Div. 64 time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  2. CodeForces.158A Next Round (水模拟)

    CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...

  3. Codeforces Round #706 (Div. 2)B. Max and Mex __ 思维, 模拟

    传送门 https://codeforces.com/contest/1496/problem/B 题目 Example input 5 4 1 0 1 3 4 3 1 0 1 4 3 0 0 1 4 ...

  4. Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round 4) C. Connect Three 【模拟】

    传送门:http://codeforces.com/contest/1087/problem/C C. Connect Three time limit per test 1 second memor ...

  5. Codeforces 758D:Ability To Convert(思维+模拟)

    http://codeforces.com/problemset/problem/758/D 题意:给出一个进制数n,还有一个数k表示在n进制下的值,求将这个数转为十进制最小可以是多少. 思路:模拟着 ...

  6. Codeforces 758C:Unfair Poll(思维+模拟)

    http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...

  7. CF--思维练习--CodeForces - 216C - Hiring Staff (思维+模拟)

    ACM思维题训练集合 A new Berland businessman Vitaly is going to open a household appliances' store. All he's ...

  8. Educational Codeforces Round 63 (Rated for Div. 2) B. Game with Telephone Numbers 博弈思维+模拟+贪心思维

    题意:博弈题面  给出一个数字序列 (>=11)  有两个人任意删除数字 直到 数字只剩下11位 如果删除后的数字串开头是8那么就是第一个赢 否则就是第二个人赢 第一个人先手  数字序列一定是奇 ...

  9. Codeforces Round #469 (Div. 2)C. Zebras(思维+模拟)

    C. Zebras time limit per test memory limit per test 512 megabytes input standard input output standa ...

随机推荐

  1. shiro整合springmvc

    说明   代码及部分相关资料根据慕课网Mark老师的视频进行整理   其他资料: shiro官网 流程 配置 1) 配置web.xml整合shiro 把shiro整合到springMVC实质上是在we ...

  2. 5.pycharm中导入第三方模块的方法

    最近刚入门学习python,网上查找了一些资料,发现python编程用的软件pycharm还是比较多的,于是就跟随大众,学习使用pycharm,在学习的过程中,想要导入第三方模块pyperclip,但 ...

  3. Git 合并多次提交

    在合并分支的时候,希望将多次提交合并成一个,然后再 cherry-pick 到主分支. 合并分支 develop 分支做开发,可能会进行多次提交,但是在发布或者进行 PR 的时候,我们只希望看到一次提 ...

  4. 【转】Hive Data Manipulation Language

    Hive Data Manipulation Language Hive Data Manipulation Language Loading files into tables Syntax Syn ...

  5. 云资源中的低成本战斗机——竞价实例,AWS、阿里云等六家云厂商完全用户使用指南

    云端资源价格 预留实例:长期持有,批发路线,价格最便宜. 按需实例:即买即用,零售路线,价格最贵. 这两种资源,基于不同区域/价格的六家云厂商价格对比,连同原始数据文档我们已经打包成了一份电子文档,有 ...

  6. 【PCIE-4】---PCIE中部分概念或问题总结(很基础很重要)

    前面三小节,介绍了PCIE的基本知识和概念,以及扫描流程.在不求甚解的情况下,我想各位小伙伴应该对PCIE有了个宏观的认识,OK,那么本章我们在之前的基础上,再单独把一些概念和更深层次的问题摘出来具体 ...

  7. Webpack实战(一):Webpack打包工具安装及参数配置

    为什么要模块化 javascript跟其他开发语言有很多的区别,其中一个就是没有模块化概念,如果一个项目中有多个js文件,我们只能通过script标签引入的方式,把一个个js文件插入到页面,这种做法会 ...

  8. Win10永久版低价购买及激活工具使用说明

    目录 按 发展历程 用户界面 激活工具 按 Windows 10是由美国微软公司开发的应用于计算机和平板电脑的操作系统,于2015年7月29日发布正式版. Windows 10操作系统在易用性和安全性 ...

  9. [LOJ#2017][轮廓线DP][KMP]「SCOI2016」围棋

    题目传送门 看到 \(m\le 12\) 和 \(c\le 6\) ,容易想到状压 DP 考虑转化成 \(3^{nm}\) 减去不合法的方案数,轮廓线 DP :\(f[i][j][S][k][h]\) ...

  10. 高通量计算框架HTCondor(五)——分布计算

    目录 1. 正文 1.1. 任务描述文件 1.2. 提交任务 1.3. 返回结果 2. 相关 1. 正文 1.1. 任务描述文件 前文提到过,HTCondor是通过condor_submit命令将提交 ...