Zoj 2967 Colorful Rainbows (Stack app)

Source: Internet
Author: User

Test instructions: gives a straight line of n Y=ai*x+bi. For these lines, if there is x so that the line y is greater than any other straight line, then this line can be seen, asking how many lines can be seen.

Idea: First to go to the heavy, will those a value of the same line take the b the largest of the article retained, the other all deleted.

Next, the lines are sorted by a value from small to large, because the slope is different, so any two lines will intersect. These lines are sorted from small to large, so that when X is smaller than the X value of its intersection, the slope is small and the Y value is large.

With this feature, the line is put into the stack first. If the intersection of the line of the stack and the top of the stack is less than the X value of the intersection of the two lines at the top of the stack, the top line of the stack is stacked and the Last Judgment is made, knowing that all the lines have been judged.

# include <stdio.h># include <algorithm># include <iostream># include <string.h>using namespace    Std;struct node{Double A; Double b;};    struct node S[5010],x[5010];bool CMP (node A1,node a2) {if (a1.a!=a2.a) return a1.a<a2.a; return a1.b<a2.b;}    int main () {int t,n,i,cot,ans;    Double d,p;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        for (i=0; i<n; i++) scanf ("%lf%lf", &s[i].a,&s[i].b);        Sort (s,s+n,cmp);        cot=0;            for (i=0; i<n-1; i++)///go to weight {if (S[I].A==S[I+1].A) continue;        S[cot++]=s[i];        } S[cot++]=s[n-1];        if (cot<2) printf ("%d\n", cot);            else {x[1]=s[1];            X[0]=s[0];            D= (s[1].b-s[0].b) *1.0/(S[0].A-S[1].A);//////First node ans=2;           for (i=2; i<cot; i++) {p= (s[i].b-x[ans-1].b) *1.0/(X[ANS-1].A-S[I].A);     while (P<=d)///See-through has been and the stack of the intersection ratio {ans--;                        if (ans>1) {d= (x[ans-1].b-x[ans-2].b) *1.0/(X[ANS-2].A-X[ANS-1].A);                    p= (s[i].b-x[ans-1].b) *1.0/(X[ANS-1].A-S[I].A);                        } else {p= (s[i].b-x[ans-1].b) *1.0/(X[ANS-1].A-S[I].A);                    Break                }} X[ans++]=s[i];            D=p;        } printf ("%d\n", ans); }} return 0;}


Zoj 2967 Colorful Rainbows (Stack app)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.