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\n",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 }

原文地址:https://www.cnblogs.com/--HPY-7m/p/11508712.html

时间: 2024-08-12 02:41:53

Analyzing Polyline -- Codeforces Round #123 (Div. 2)的相关文章

Codeforces Round #259 (Div. 2) 解题报告

终于重上DIV1了.... A:在正方形中输出一个菱形 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月01日 星期五 23时27分55秒 4 5 #include<vector> 6 #include<set> 7 #include<deque> 8 #include<stack> 9 #include<bitset> 10 #inclu

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest

题目范围给的很小,所以有状压的方向. 我们是构造出一个数列,且数列中每两个数的最大公约数为1; 给的A[I]<=30,这是一个突破点. 可以发现B[I]中的数不会很大,要不然就不满足,所以B[I]<=60左右.构造DP方程DP[I][J]=MIN(DP[I][J],DP[I][J^C[K]]+abs(a[i]-k)); K为我们假设把这个数填进数组中的数.同时开相同空间记录位置,方便输出结果.. #include<iostream> #include<stdio.h>

Codeforces Round #424 (Div. 2) E. Cards Sorting(线段树)

题目链接:Codeforces Round #424 (Div. 2) E. Cards Sorting 题意: 将n个数放进一个队列,每次检查队首,看看是不是队列中最小的数,如果是就扔掉,如果不是就放到队尾. 这样直到队列为空,为需要操作多少次. 题解: 考虑用两个指针模拟,最开始now指针指向第一个数,然后nxt指针指向下一个将要被删除的数. 然后我们要算出这里需要移动多少步,然后删掉这个数,一直重复操作,直到将全部的数删完. nxt指针可以用set来维护,now指针可以用并查集来维护. 计

Codeforces Round #366 (Div. 2) ABC

Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 1 #I hate that I love that I hate it 2 n = int(raw_input()) 3 s = "" 4 a = ["I hate that ","I love that ", "I hate it","I love it"] 5 fo

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0

Codeforces Round #542 (Div. 2)

Codeforces Round #542 (Div. 2) 题目做不下去的我写一写题解 A. Be Positive 水题,考虑正数,负数个数是否\(\geq \lceil \frac{n}{2} \rceil\) B. Two Cakes 给定位置大小为1...n的蛋糕,每种大小共两份,要求两个人从最左边的位置增序各取1..n的蛋糕,求最小步长 两个一起考虑,取到第i块蛋糕,两人的位置状态是确定的,无论是a,b两人怎样分布总是占据了这个位置,有点类似蚂蚁相遇后逆行的想法 所以我们只需min(

CodeCraft-19 and Codeforces Round #537 (Div. 2) - D. Destroy the Colony(动态规划+组合数学)

Problem  CodeCraft-19 and Codeforces Round #537 (Div. 2) - D. Destroy the Colony Time Limit: 2000 mSec Problem Description Input Output For each question output the number of arrangements possible modulo 10^9+7. Sample Input abba21 41 2 Sample Output