1. 程式人生 > >Codeforces 1159F Winding polygonal line(叉積)

Codeforces 1159F Winding polygonal line(叉積)

strong 端點 spa 並且 pac rst int() urn memory

其實這個幾何寫起來還是比較方便,只用到了叉積。首先我們貪心的考慮一種情況,對於任意給定的LR串,我們起點的選擇肯定是在這些點圍成的凸包端點上,對於這樣的起點來說,他對於L或者R都是有選擇的機會,而且一定可以從剩下n-1個點選出兩個點滿足要求(可以畫圖觀察),接下來我們對於這個起點出發開始去尋找滿足LR的點,對於第二個點來說,我們需要去找到剩下n-1個點中最外側的點,並且滿足剩下n-2個點都在向量point[1]-point[0]的左側或者右側,這個可以直接由叉積得到,那麽我們便得到了第二個點,顯然第二個點也一定是在剩下n-1個點圍成的凸包端點上,無論取剩下n-2個點中的任何一個都是滿足兩個向量滿足 point[2]-point[1],point[1]-point[0]的向量轉向滿足s[0],那麽問題就可以看成是一個 n‘=n-2 的新問題,那麽此時起點是point[1],那麽依然通過上述方法找到point[2],反復如此,直到找到 n-1 個點,最後剩下一個點必然滿足最後一個s的轉向,那麽此題也是沒有-1的情況。

  1 //      ——By DD_BOND
  2 
  3 //#include<bits/stdc++.h>
  4 #include<functional>
  5 #include<algorithm>
  6 #include<iostream>
  7 #include<sstream>
  8 #include<iomanip>
  9 #include<climits>
 10 #include<cstring>
 11 #include<cstdlib>
 12
#include<cstddef> 13 #include<cstdio> 14 #include<memory> 15 #include<vector> 16 #include<cctype> 17 #include<string> 18 #include<cmath> 19 #include<queue> 20 #include<deque> 21 #include<ctime> 22 #include<stack> 23
#include<map> 24 #include<set> 25 26 #define fi first 27 #define se second 28 #define MP make_pair 29 #define pb push_back 30 #define INF 0x3f3f3f3f 31 #define pi 3.1415926535898 32 #define lowbit(a) (a&(-a)) 33 #define lson l,(l+r)/2,rt<<1 34 #define rson (l+r)/2+1,r,rt<<1|1 35 #define Min(a,b,c) min(a,min(b,c)) 36 #define Max(a,b,c) max(a,max(b,c)) 37 #define debug(x) cerr<<#x<<"="<<x<<"\n"; 38 39 using namespace std; 40 41 typedef long long ll; 42 typedef pair<int,int> P; 43 typedef pair<ll,ll> Pll; 44 typedef unsigned long long ull; 45 46 const ll LLMAX=2e18; 47 const int MOD=1e9+7; 48 const double eps=1e-8; 49 const int MAXN=1e6+10; 50 const int hmod1=0x48E2DCE7; 51 const int hmod2=0x60000005; 52 53 inline ll sqr(ll x){ return x*x; } 54 inline int sqr(int x){ return x*x; } 55 inline double sqr(double x){ return x*x; } 56 ll __gcd(ll a,ll b){ return b==0? a: __gcd(b,a%b); } 57 ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%MOD;a=a*a%MOD;n>>=1;}return sum;} 58 inline int dcmp(double x){ if(fabs(x)<eps) return 0; return (x>0? 1: -1); } 59 60 int use[MAXN]; 61 vector<int>ans; 62 63 struct Point{ 64 ll x,y,id; 65 Point(){ x=y=0; } 66 Point(ll a,ll b){ x=a,y=b; } 67 Point operator -(const Point &n)const{ 68 return Point(x-n.x,y-n.y); 69 } 70 bool operator <(const Point &n)const{ 71 if(x==n.x) return y<n.y; 72 return x<n.x; 73 } 74 }point[MAXN]; 75 76 int dcmp(ll x){ 77 if(x==0) return 0; 78 return x>0? 1: -1; 79 } 80 81 bool cmp(Point a,Point b){ 82 return a.id<b.id; 83 } 84 85 ll cross(Point a,Point b){ 86 return a.x*b.y-a.y*b.x; 87 } 88 89 int main(void) 90 { 91 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 92 int n; cin>>n; 93 for(int i=1;i<=n;i++) cin>>point[i].x>>point[i].y,point[i].id=i; 94 string s; cin>>s; 95 sort(point+1,point+n+1); 96 ans.pb(point[1].id),use[point[1].id]=1; 97 sort(point+1,point+n+1,cmp); 98 for(int i=0;i<s.size();i++){ 99 int k=0,flag=(s[i]==L? 1: -1); 100 for(int j=1;j<=n;j++){ 101 if(use[j]) continue; 102 if(!k||dcmp(cross(point[k]-point[ans[i]],point[j]-point[k]))!=flag) k=j; 103 } 104 ans.pb(k),use[k]=1; 105 } 106 for(int i=1;i<=n;i++) 107 if(!use[i]) 108 ans.pb(i); 109 for(auto i:ans) cout<<i<< ; 110 return 0; 111 }

Codeforces 1159F Winding polygonal line(叉積)