HDU 3644
模拟退火算法。。。。
这道题,呃。我怎么感觉他就是随机的。同一个代码,时而AC,时而WA。其实还真的是随机的。呵呵呵呵呵。。。因为下降火太快了,没办法,而降得慢又会。。。TLE,虽然精度提高了。
敢问,还有什么好的方法?我是在做退火算法时遇到这个练手的。
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <time.h> using namespace std;
const int MAXN=55;
const double PI=3.141592653;
const double eps=1e-5; #define zero(a) fabs(a)<eps struct point {
double x,y;
};
struct Segment{
point a,b;
}; point p[MAXN]; int n; double ans; int cot;
point tar[MAXN]; double best[MAXN]; double R; point operator -(point &u,point &v){
point re;
re.x=u.x-v.x; re.y=u.y-v.y;
return re;
} double dot(point &u,point &v){
return u.x*v.x+u.y*v.y;
} double dist(point tt){
return sqrt(tt.x*tt.x+tt.y*tt.y);
}
double multi(point &u,point &v){
return u.x*v.y-u.y*v.x;
}
/*
bool whether_in(point &t){
double angle=0;
for(int i=0;i<n;i++){
point A=p[i]-t;
point B=p[i+1]-t;
angle+=acos(dot(A,B)/dist(A)/dist(B));
}
if(fabs(angle-PI*2)<1e-5)
return true;
return false;
} */ double xmul(point p0,point p1,point p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
} bool online(point p1,point p2,point p){
if(zero(xmul(p1,p2,p))&&((p.x-p1.x)*(p.x-p2.x)<eps&&(p.y-p1.y)*(p.y-p2.y)<eps))
return true;
return false;
} inline bool across(Segment s1,Segment s2){
if(xmul(s1.a,s1.b,s2.a)*xmul(s1.a,s1.b,s2.b)<eps)
if(xmul(s2.a,s2.b,s1.a)*xmul(s2.a,s2.b,s1.b)<eps)
return true;
return false;
} bool whether_in(point cen){
int cnt=0;
Segment s,e;
s.a=cen;s.b.y=cen.y;s.b.x=20000.0;
for(int i=0;i<n;i++){
e.a=p[i];e.b=p[i+1];
if(online(p[i],p[i+1],cen)) return false;
if(zero(p[i].y-p[i+1].y)) continue;
if(online(s.a,s.b,p[i])){
if(p[i].y>p[i+1].y) cnt++;
}
else if(online(s.a,s.b,p[i+1])){
if(p[i+1].y>p[i].y) cnt++;
}
else if(across(s,e))
cnt++;
}
return cnt&1;
} double count_d(point &tt){
double mm=1e10; double tp;
for(int i=0;i<n;i++){
point A=tt-p[i];
point B=p[i+1]-p[i];
if(dot(A,B)<=0){
tp=dist(A);
// cout<<"1"<<' '<<tp<<endl;
mm=min(mm,tp);
continue;
}
A=tt-p[i+1];
B=p[i]-p[i+1];
if(dot(A,B)<=0){
tp=dist(A);
mm=min(mm,tp);
// cout<<"2"<<' '<<tp<<endl;
continue;
}
double area=multi(A,B);
tp=fabs(area)/dist(p[i]-p[i+1]);
// cout<<"3"<<' '<<tp<<endl;
mm=min(mm,tp);
}
return mm;
} double getdouble(){
double re=((rand()*rand())%1000000)*1.0/1e6;
return re;
} int main(){
srand(time(0));
while(scanf("%d",&n),n){
cot=0;
for(int i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
scanf("%lf",&R);
p[n]=p[0];
point tmp;
for(int i=0;i<n;i++){
tmp.x=(p[i].x+p[i+1].x)/2;
tmp.y=(p[i].y+p[i+1].y)/2;
tar[cot++]=tmp;
}
// cout<<cot<<endl;
bool flag=false;
for(int i=0;i<cot;i++){
best[i]=0;
}
// cout<<"YES"<<endl;
double T=50;
for(double t=T;t>1e-4;t*=0.55){
for(int i=0;i<cot;i++){
for(int j=0;j<10;j++){
double td=getdouble();
if(rand()&1) td*=-1;
tmp.x=tar[i].x+td*t;
td=getdouble();
if(rand()&1) td*=-1;
tmp.y=tar[i].y+td*t;
if(whether_in(tmp)){
td=count_d(tmp);
if(td>best[i]){
best[i]=td;
tar[i]=tmp;
}
if(td>=R||fabs(td-R)<1e-4){
// cout<<tmp.x<<' '<<tmp.y<<endl;
// cout<<td<<endl;
flag=true;
break;
}
}
}
if(flag) break;
}
if(flag) break;
}
if(flag) printf("Yes\n");
else printf("No\n");
}
return 0;
}
HDU 3644的更多相关文章
- HDU - 3644:A Chocolate Manufacturer's Problem(模拟退火, 求多边形内最大圆半径)
pro:给定一个N边形,然后给半径为R的圆,问是否可以放进去. 问题转化为多边形的最大内接圆半径.(N<50): sol:乍一看,不就是二分+半平面交验证是否有核的板子题吗. 然而事情并没有那 ...
- 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856
并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- bzoj 1191 [ HNOI 2006 ] 超级英雄Hero —— 二分图匹配
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1191 就是二分图匹配的裸题: 注意题目要求是第一次匹配失败就退出!没仔细看题差点丢失1A. ...
- c++ 写进文件并读出
#include <iostream>#include <fstream>#include <vector>#include <string> int ...
- html5 窗口之间的通信
一般窗口通信分为三种: iframe嵌套:多个iframe之间通信. 父页面操作子页面元素:oFrame.contentWindow.document.body. 父页面调用子页面方法:oFrame. ...
- python 端口扫描程序
#! /usr/bin/env python3 #-*- coding:utf-8 -*- import socket import threading OPEN_COUNT = 0 lock = t ...
- 【HDU1698】 Just a Hook 【线段树入门】
原题:原题链接 题意:(机器翻译的...) 让我们将钩子的连续金属棒从1到N编号.对于每次操作,Pudge可以将连续的金属棒(从X到Y编号)改为铜棒,银棒或金棒. 钩的总值计算为N个金属棒的值的总和. ...
- Hibernate多表查询、查询优化策略(四)
多表HQL private static void innerJoin(){ //sql内连接 隐式内连接 select * from A,B where b.aid = a.id // 显示内连接 ...
- sql学习--insert
insert的四种插入方式 第一种最简单的 into 和目标列的列表是可选的 intsert [into] tableA [(col1,col2)] values(val1,val2) 第二种 ins ...
- 复习HTML+CSS(9)
n CSS简介 CSS:Cascading Style Sheets 层叠样式表 CSS的主要目的:是给HTML标记添加各种各样的表现(格式.样式).比如:文字样式.背景.文本样式.链接样式. 提示 ...
- 前端-Vue学习思维导图笔记
看不清的朋友右键保存或者新窗口打开哦!喜欢我可以关注我,还有更多前端思维导图笔记有vue结构分析,JS基础,JQ,JS高级,Angular,git等等
- OpenCV:OpenCV中的 parallel_for 和opencv parallel_for_
OpenCV使用OMP完成并行运算,在使用AdaBoost检测的时候,在cascadedetect.cpp 里面,大量使用 parallel_for_(Range(0, stripCount), Ca ...