zoukankan      html  css  js  c++  java
  • Codeforces 1159F Winding polygonal line(叉积)

    其实这个几何写起来还是比较方便,只用到了叉积。首先我们贪心的考虑一种情况,对于任意给定的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<<"
    ";
     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 }
  • 相关阅读:
    尤瓦尔•赫拉利简史三部曲读书笔记
    5星|《城市与国家财富》:经济发展的基本单位是城市而不是国家
    oracle SQL Develop导出数据库中的表格数据到excel
    selvert的过滤器filter处理中文乱码
    jsp中四种传递参数的方法
    Mybatis中配置Mapper的方法
    JAVA文件中获取路径及WEB应用程序获取路径方法
    MyEclipse 常用设置
    Java连接oracle数据库的OCI和thin
    Java连接oracle数据库的OCI和THIN
  • 原文地址:https://www.cnblogs.com/dd-bond/p/10858450.html
Copyright © 2011-2022 走看看