• Analyzing Polyline -- Codeforces Round #123 (Div. 2)


    题意:https://codeforc.es/problemset/problem/195/D

    求折线段数。

    思路:

    对pos进行sort,对不同区间段加k,两个dp处理不同k>0 or k<0前后缀,判断即可。

    注意:long double,ESP=1e-20。

      1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
      2 #include <cstdio>//sprintf islower isupper
      3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
      4 #include <iostream>//pair
      5 #include <fstream>//freopen("C:\Users\13606\Desktop\草稿.txt","r",stdin);
      6 #include <bitset>
      7 //#include <map>
      8 //#include<unordered_map>
      9 #include <vector>
     10 #include <stack>
     11 #include <set>
     12 #include <string.h>//strstr substr
     13 #include <string>
     14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
     15 #include <cmath>
     16 #include <deque>
     17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
     18 #include <vector>//emplace_back
     19 //#include <math.h>
     20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
     21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
     22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
     23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
     24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
     25 #define mem(a,b) memset(a,b,sizeof(a))
     26 #define pr printf
     27 #define sc scanf
     28 #define ls rt<<1
     29 #define rs rt<<1|1
     30 typedef long long ll;
     31 #define RG register int;
     32 void swapp(int &a,int &b);
     33 double fabss(double a);
     34 int maxx(int a,int b);
     35 int minn(int a,int b);
     36 int Del_bit_1(int n);
     37 int lowbit(int n);
     38 int abss(int a);
     39 //const long long INF=(1LL<<60);
     40 const double E=2.718281828;
     41 const double PI=acos(-1.0);
     42 const int inf=(1<<30);
     43 const double ESP=1e-20;
     44 const int mod=(int)1e9+7;
     45 const int N=(int)1e6+10;
     46 
     47 struct node
     48 {
     49     int k,b,id;
     50     long double pos;
     51     friend bool operator<(node a,node b)
     52     {
     53         return a.pos<b.pos;
     54     }
     55 }a[N];
     56 
     57 long double get(int k,int b)
     58 {
     59     long double len=1.0*b/(1.0*k);
     60     len=abs(len);
     61     if(k>0)
     62     {
     63         if(b>0)
     64             return -len;
     65         else
     66             return len;
     67     }
     68     else
     69     {
     70         if(b>0)
     71             return len;
     72         else
     73             return -len;
     74     }
     75 }
     76 bool same(long double x,long double y)
     77 {
     78     return abs(x-y)<ESP;
     79 }
     80 
     81 ll dp[N],dp2[N];
     82 
     83 int main()
     84 {
     85     int n,cnt=0;
     86     long double xx;
     87     sc("%d",&n);
     88     for(int i=1;i<=n;++i)
     89     {
     90         int k,b;
     91         sc("%d%d",&k,&b);
     92         if(k==0)
     93             continue;
     94         a[++cnt]={k,b},a[cnt].pos=get(a[cnt].k,a[cnt].b);
     95     }
     96     n=cnt;
     97     sort(a+1,a+1+n);
     98     a[1].id=1;
     99     int id=1;
    100     for(int i=2;i<=n;++i)
    101     {
    102         if(!same(a[i].pos,a[i-1].pos))
    103             id++;
    104         a[i].id=id;
    105     }
    106     for(int i=1;i<=n;++i)
    107         if(a[i].k>0)
    108             dp[a[i].id]+=a[i].k;
    109     for(int i=1;i<=n;++i)
    110         dp[i]+=dp[i-1];
    111     for(int i=n;i>=1;--i)
    112     {
    113         if(a[i].k<0)
    114             dp2[a[i].id-1]+=a[i].k;
    115     }
    116     for(int i=n;i>=0;--i)
    117         dp2[i]+=dp2[i+1];
    118     int ans=0;
    119     for(int i=1;i<=id;++i)
    120         if(dp[i]+dp2[i]!=dp[i-1]+dp2[i-1])
    121             ans++;
    122     pr("%d
    ",ans);
    123     return 0;
    124 }
    125 
    126 /**************************************************************************************/
    127 
    128 int maxx(int a,int b)
    129 {
    130     return a>b?a:b;
    131 }
    132 
    133 void swapp(int &a,int &b)
    134 {
    135     a^=b^=a^=b;
    136 }
    137 
    138 int lowbit(int n)
    139 {
    140     return n&(-n);
    141 }
    142 
    143 int Del_bit_1(int n)
    144 {
    145     return n&(n-1);
    146 }
    147 
    148 int abss(int a)
    149 {
    150     return a>0?a:-a;
    151 }
    152 
    153 double fabss(double a)
    154 {
    155     return a>0?a:-a;
    156 }
    157 
    158 int minn(int a,int b)
    159 {
    160     return a<b?a:b;
    161 }
  • 相关阅读:
    Python Virtualenv的使用
    Pycharm常用的设置
    Zabbix学习目录
    DELL R740 Raid10与U盘启动项的配置
    Django
    Django
    Django
    Django
    Django
    Django
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/11508712.html
Copyright © 2020-2023  润新知