Codeforces 1087C Connect Three (思维+模拟)
题意:
网格图选中三个格,让你选中一些格子把这三个格子连起来,使得选中的格子总数最小。最后输出方案
网格范围为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 (思维+模拟)的更多相关文章
- codeforces 887A Div. 64 思维 模拟
A. Div. 64 time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- CodeForces.158A Next Round (水模拟)
CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...
- 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 ...
- 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 ...
- Codeforces 758D:Ability To Convert(思维+模拟)
http://codeforces.com/problemset/problem/758/D 题意:给出一个进制数n,还有一个数k表示在n进制下的值,求将这个数转为十进制最小可以是多少. 思路:模拟着 ...
- Codeforces 758C:Unfair Poll(思维+模拟)
http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...
- CF--思维练习--CodeForces - 216C - Hiring Staff (思维+模拟)
ACM思维题训练集合 A new Berland businessman Vitaly is going to open a household appliances' store. All he's ...
- Educational Codeforces Round 63 (Rated for Div. 2) B. Game with Telephone Numbers 博弈思维+模拟+贪心思维
题意:博弈题面 给出一个数字序列 (>=11) 有两个人任意删除数字 直到 数字只剩下11位 如果删除后的数字串开头是8那么就是第一个赢 否则就是第二个人赢 第一个人先手 数字序列一定是奇 ...
- Codeforces Round #469 (Div. 2)C. Zebras(思维+模拟)
C. Zebras time limit per test memory limit per test 512 megabytes input standard input output standa ...
随机推荐
- docker练习-堆栈
介绍 分布式应用程序层次结构的顶部:堆栈. 堆栈是一组相互关联的服务,它们共享依赖关系,并且可以协调和缩放在一起. 单个堆栈能够定义和协调整个应用程序的功能(尽管非常复杂的应用程序可能希望使用多个堆栈 ...
- vue报错 [Intervention] Ignored attempt to cancel a touchmove event with cancelable
在vue开发中使用vue-awesome-swiper制作轮播图,手动拖动时会报错,解决方案: 需要滑动的标签 { touch-action: none; } -------------------- ...
- 浏览器从输入url 到页面展示完成响应过程
用户从输入 url 到浏览器响应,呈现给用户的具体过程 1.用户在输入栏输入地址 (1) 如果有 beforeunload 事件会先执行判断继续还是跳出操作 (2) 浏览器进程识别是 地址还是关键字检 ...
- Java同步与异步
一.关键字: thread(线程).thread-safe(线程安全).intercurrent(并发的) synchronized(同步的).asynchronized(异步的). volatile ...
- django操作命令
下载安装 pip3 install django==1.11.21 -i https://pypi.tuna.tsinghua.edu.cn/simple 创建项目 1.终端找到存放项目的文件夹,dj ...
- python 线程条件
条件.事件.信号量本质上都是锁,不常用 """ 常用方法: obj,acquire() Obj.release() obj.wait(),创建是阻塞状态,等待obj.no ...
- ubuntu系统下载后的.deb软件安装命令
查看某个软件是否安装,比如查看QQ软件是否安装并列出软件包名: dpkg -l | grep qq 删除某款软件:sudo dpkg -r 软件包名 安装软件 : sudo dpkg -i *.deb
- centos7的新特性
1.修改主机名方式改变 centos6在/etc/ centos7使用命令hostnamectl set-hostname 主机名2.修改时间方式改变3.查看IP地址ifconfig不再支持改用ip4 ...
- python实例:从excel读取股票代码,爬取股票信息写到代码后面的单元格中
关键词:爬虫.python.request.接口.excel处理 思路: 1.首先准备好excel文档,把股票代码事先编辑进去. 2.脚本读取文档,依次读出股票代码到指定站点发起请求获取股票信息 3. ...
- Linux系统实时数据同步inotify+rsync
一.inotify简介 inotify是Linux内核的一个功能,它能监控文件系统的变化,比如删除.读.写和卸载等操作.它监控到这些事件的发生后会默认往标准输出打印事件信息.要使用inotify,Li ...