計算幾何 半平面交

LA 4992?&& hdu 3761?Jungle Outpost

杭電的有點坑啊。。一直爆內存,后來發現大白的半平面交模板那里 point *p = new point[n]; line *q = new line[n]這里出了問題,應該是在函數里面申請不了比較大的數組,所以爆內存。。我在全局定義了兩個數組就不會爆了。。

本來跑了17s多的,后來半平面交sort( l, l + n ) 被我注釋了,就跑了9s多,LA上跑了 2s。。應該是輸入數據比較好,不用按照極角排序。。然后就是照著大白的想法,二分答案,用半平面交判斷是否滿足條件。。

輸入是按照逆時針輸入的,所以用p[i] - p[(i+m+1)%n]表示直線的向量。。

這道題被坑了好久,好傷心。。貼的是在杭電ac的代碼

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<string>
  5 #include<algorithm>
  6 #include<queue>
  7 #include<cmath>
  8 #include<vector>
  9 
 10 using namespace std;
 11 
 12 #define mnx 50050
 13 #define LL long long
 14 #define mod 1000000007
 15 #define inf 0x3f3f3f3f
 16 #define eps 1e-8
 17 #define Pi acos(-1.0);
 18 #define lson l, m, rt << 1
 19 #define rson m+1, r, rt << 1 | 1
 20 
 21 int dcmp( double x ){
 22     if( fabs( x ) < eps ) return 0;
 23     return x < 0 ? -1 : 1;
 24 }
 25 struct point{
 26     double x, y;
 27     point( double x = 0, double y = 0 ) : x(x), y(y) {}
 28     point operator + ( const point &b ) const{
 29         return point( x + b.x, y + b.y );
 30     }
 31     point operator - ( const point &b ) const{
 32         return point( x - b.x, y - b.y );
 33     }
 34     point operator * ( const double &k ) const{
 35         return point( x * k, y * k );
 36     }
 37     point operator / ( const double &k ) const{
 38         return point( x / k, y / k );
 39     }
 40     bool operator < ( const point &b ) const{
 41         return dcmp( x - b.x ) < 0 || dcmp( x - b.x ) == 0 && dcmp( y - b.y ) < 0;
 42     }
 43     bool operator == ( const point &b ) const{
 44         return dcmp( x - b.x ) == 0 && dcmp( y - b.y ) == 0;
 45     }
 46     double len(){
 47         return sqrt( x * x + y * y );
 48     }
 49 };
 50 typedef point Vector;
 51 struct line{
 52     point p; 
 53     Vector v;
 54     double ang;
 55     line() {}
 56     line( point p, point v ) : p(p), v(v) {
 57         ang = atan2( v.y, v.x );
 58     }
 59     bool operator < ( const line &b ) const{
 60         return ang < b.ang;
 61     }
 62 };
 63 double dot( Vector a, Vector b ){
 64     return a.x * b.x + a.y * b.y;
 65 }
 66 double cross( Vector a, Vector b ){
 67     return a.x * b.y - a.y * b.x;
 68 }
 69 bool onleft( line l, point p ){
 70     return cross( l.v, p - l.p ) > 0;
 71 }
 72 point getintersection( line a, line b ){
 73     Vector u = a.p - b.p;
 74     double t = cross( b.v, u ) / cross( a.v, b.v );
 75     return a.p + a.v * t;
 76 }
 77 point pp[mnx];
 78 line q[mnx];
 79 int halfplaneintersection( line *L, int n, point *poly ){
 80     //sort( L, L + n );
 81     int first, last;
 82     q[first = last = 0] = L[0];
 83     for( int i = 1; i < n; i++ ){
 84         while( first < last && !onleft( L[i], pp[last-1] ) ) last--;
 85         while( first < last && !onleft( L[i], pp[first] ) ) first++;
 86         q[++last] = L[i];
 87         if( fabs( cross( q[last].v, q[last-1].v ) ) < eps ){
 88             last--;
 89             if( onleft( q[last], L[i].p ) ) q[last] = L[i];
 90         }
 91         if( first < last ) pp[last-1] = getintersection( q[last-1], q[last] );
 92     }
 93     while( first < last && !onleft( q[first], pp[last-1] ) ) last--;
 94     if( last - first <= 1 ) return 0;
 95     pp[last] = getintersection( q[last], q[first] );
 96     int m = 0;
 97     for( int i = first; i <= last; i++ ){
 98         poly[m++] = pp[i];
 99     }
100     return m;
101 }
102 point p[mnx], poly[mnx];
103 line l[mnx];
104 bool check( int n, int m ){
105     if( n - m <= 2 ) return 1;
106     for( int i = 0; i < n; i++ ){
107         l[i] = line( p[i], p[i] - p[(i+m+1)%n] );
108     }
109     int all = halfplaneintersection( l, n, poly );
110     if( !all ) return 1;
111     else return 0;
112 }
113 int main(){
114     int n, cas;
115     scanf( "%d", &cas );
116     while( cas-- ){
117         scanf( "%d", &n );
118         for( int i = 0; i < n; i++ ){
119             scanf( "%lf %lf", &p[i].x, &p[i].y );
120         }
121         if( n > 50000 ) continue;
122         int l = 0, r = n, ans;
123         while( l < r ){
124             int m = ( l + r ) >> 1;
125             if( check( n, m ) == 1 ){
126                 ans = m, r = m;
127             }
128             else l = m + 1;
129         }
130         printf( "%d\n", ans );
131     }
132     return 0;
133 }
View Code

?

轉載于:https://www.cnblogs.com/LJ-blog/p/3960924.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/273598.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/273598.shtml
英文地址,請注明出處:http://en.pswp.cn/news/273598.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Maven 強制導入jar包

場景 有時候因為各種原因(依賴有了&#xff0c;jar包有了)&#xff0c;項目中就是沒有這個jar包。 在需要強導的項目中創建lib文件夾&#xff0c;將需要強導的jar包訪問lib中。添加依賴$&#xff5b;pom.basedir&#xff5d;:獲取當前所在的項目目錄 $&#xff5b;pom.basedir&…

0910

我累得時候希望你能在我身邊&#xff0c;在你的懷里好好的睡一覺。轉載于:https://www.cnblogs.com/zhanzhao/p/3964175.html

《Java 高并發》03 線程的生命周期

相關概念 進程是指一個內存中運行的應用程序&#xff0c;每個進程都有自己獨立的一塊內存空間&#xff0c;一個進程中可以啟動多個線程。 一個進程是一個獨立的運行環境&#xff0c;它可以被看作一個程序或者一個應用。而線程是在進程中執行的一個任務。Java運行環境是一個包含…

OpenLayers3 online build

openlayers3使用了一個比較復雜的build工具&#xff0c;從github上下載下來的代碼中并沒有build之后的版本&#xff0c;要配置build環境又比較繁瑣&#xff0c;好在官方的example中提供了在線的版本&#xff0c;下面就是link&#xff1a; http://openlayers.org/en/v3.0.0/buil…

Mysql 必知必會(一)

文章案例所需的SQL文件&#xff0c;點擊下載 使用MySQL 進入mysql安裝目錄下的bin目錄&#xff1a; 連接Mysql&#xff1a;mysql -uroot -p123456;顯示Mysql下的所有數據庫&#xff1a;show databases;切換數據庫&#xff1a;use local;顯示數據庫下所有表名&#xff1a;show t…

design.js

//模塊式開發 var myNamespace (function () { var myPrivateVar 0;var myPrivateMethod function (foo) {console.log(foo); };return {myPublicVar : "foo",myPublicFunction : function (bar) {myPrivateVar;myPrivateMethod(bar);} }; })(); //原型模式 var…

Spring boot 整合dynamic實現多數據源

項目git地址&#xff1a;Jacob-dynamic 準備工作 # 創建數據庫db1 CREATE DATABASE db1CHARACTER SET utf8 COLLATE utf8_bin # 創建user表 CREATE TABLE user (id int(11) DEFAULT NULL,name varchar(255) DEFAULT NULL ) ENGINEInnoDB DEFAULT CHARSETutf8 # 添加數據 INSERT…

LInux 命令大全

開關機 reboot&#xff1a;重啟shutdown -h 0 或者init 0 &#xff1a;關機halt&#xff1a;關機poweroff:關機 文件的操作 ll&#xff1a;顯示文件夾詳細信息ls&#xff1a;顯示文件目錄mkdir fileName&#xff1a;創建目錄mkdir -p fileName/fileName&#xff1a;目錄cd file…

企業級業務系統開發實戰-序言

前些年一直在做微軟的解決方案實施與軟件開發的工作。在學習、項目實施、開發與管理的過程中學到了別人不少好的東西&#xff0c;也自身總結了大量的經驗&#xff0c;希望能夠通過一個系列來跟大家分享關于軟件開發方面的內容。 這個開發系列的由來是這樣的&#xff0c;兩年前作…

Could not autowire. No beans of 'JavaMailSender' type found..md

Could not autowire. No beans of JavaMailSender type found. 導入依賴 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>2.1.5.RELEASE</version> </depe…

極客Web前端開發資源集錦

本周我們帶來的前端推薦包含當前熱門的bootstrap&#xff0c;html5&#xff0c;css3等技術內容和新聞話題&#xff0c;如果你還想近一步學習如何開發&#xff0c;還可以關注我們的極客課程庫&#xff0c;里面涵蓋了現代開發技術的‘學’與‘習’的全新功能。希望對大家有所幫助…

mahout學習筆記4

分析數據 有哪些數據 選用什么樣的推薦算法 Finding an effective recommender 各種算法組合測試 Tanimoto算法在與thresholdneighborhoold結合時值應該設置比較底&#xff0c;0.5已經是很高的相似度 可以重寫ItemSimilarity &#xff0c;把自己的功能放到里面 IDRescorer 可以…

使用 Spring Cloud 實現微服務系統

使用 Spring Cloud 實現微服務系統 準備工作&#xff1a;為了方便創建項目&#xff0c;以及各版本以來關系&#xff0c;此次創建項目使用 Spring Assistant插件。 創建單體服務中心項目 啟用服務端的服務注冊&#xff0c;發現功能 EnableEurekaServer SpringBootApplication pu…

HTML+CSS公司培訓(一)高手請飄過

隨著公司的轉向&#xff0c;從.net到webapp很多人無從適應。因此在公司進行一些簡單的培訓。同時把我微薄的經驗分享給大家&#xff0c;并且和大家一起學習進步。 對于HTML在正常的開發中我們其實用的標簽就是那么簡單的幾個&#xff08;是小編在項目開發中常用的一些&#xff…

【LeetCode】整數反轉

package leetcode.editor.cn;//給出一個 32 位的有符號整數&#xff0c;你需要將這個整數中每位上的數字進行反轉。 // // 示例 1: // // 輸入: 123 //輸出: 321 // // // 示例 2: // // 輸入: -123 //輸出: -321 // // // 示例 3: // // 輸入: 120 //輸出: 21 // // //…

sql 中實現打亂數據的排序

sql 中實現打亂數據的排序order by NEWID()就實現了數據的打亂 轉載于:https://www.cnblogs.com/yangjinwang/p/3998271.html

【LeetCode】兩數之和

package leetcode.editor.cn;//給定一個整數數組 nums 和一個目標值 target&#xff0c;請你在該數組中找出和為目標值的那 兩個 整數&#xff0c;并返回他們的數組下標。 // // 你可以假設每種輸入只會對應一個答案。但是&#xff0c;數組中同一個元素不能使用兩遍。 // // …

Docker學習筆記1 :鏡像制作

參考資源&#xff1a; http://blog.csdn.net/kongxx?viewmodecontents http://my.oschina.net/feedao/blog 運行環境win8.1 virtual box, 運行 centos6.4 64bit&#xff0c; 內網通過代理上網。 如下操作基本都在root下進行。 目的嘗試自己建立Docker鏡像 基礎工作1&#xf…

讓不帶www的域名跳轉到帶www的域名

域名不帶www和帶www不是同一碼事&#xff1a;前者稱作根域名&#xff0c;后者是前者的二級域名。長久以來&#xff0c;人們都習慣了訪問網站的時候帶上www&#xff0c;所以大多數站長朋友域名解析的時候都是帶www的和不帶www的一起解析。然而對于搜索引擎來說&#xff0c;還是會…

RestTemplate 發送 Https 請求調用

RestTemplate 發送 Https 請求調用 個人博客:https://jacob.org.cn import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.htt…