Codeforce Gym 100685E Epic Fail of a Genie (greedy)

Source: Internet
Author: User

Test instructions: Give a bunch of elements, to find a subset, so that the product of the largest subset, if there are multiple, should make the subset of the number of elements as small as possible.

Puzzle: Greedy, if there is a positive number of products greater than 1, then it is necessary to choose, note that negative numbers may also be more than 1 positive number, then the absolute value is greater than 1 negative 22 pairs, if there is still one, then in the judgment, that negative and smaller than its smallest negative product is greater than 1, if so select two After selecting all the numbers that may be larger than 1, the remaining number must be smaller than 1, then it is not selected.

If you cannot make up more than 1 of the number, then categorize the discussion.

Very easy to write wrong ...

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e4+6;structnum{intA[MAXN]; intID[MAXN]; intR[MAXN]; intsz; Num () {sz=0;} voidPushDoubleXinti) {A[sz]= x* -; R[SZ]=sz; ID[SZ]=i; SZ++; }    int_upper_bound (intLintRintv) { while(l<s) {            intm = (l+r) >>1; if(A[R[M]]&GT;V) R =m; ElseL = m+1; }        returnL; }    int_lower_bound (intLintRintv) { while(l<R) {            intm = (l+r) >>1; if(A[R[M]]&GT;=V) R =m; ElseL = m+1; }        returnL; }   //int ID (int x) {return r[x];}    int operator[](intx) {        returnA[r[x]]; }}p,m;intANS[MAXN];#defineGetbound (L,r,v,which) \L= Which._lower_bound (0, which.sz,v); R= Which._upper_bound (0, which.sz,v);#defineADD (which,x) \Ans[sz++] =Which.id[which.r[x]];BOOLcmpintXintY) {returnP.A[X] <p.a[y];}BOOLCMP2 (intXintY) {returnM.A[X] <m.a[y];}intMain () {//freopen ("In.txt", "R", stdin);    intN scanf"%d",&N); BOOLZero =false; intPzero;  for(inti =1; I <= N; i++){        DoubleT; scanf ("%LF",&t); if(t>0) {P.push (t,i); }Else if(t<0) {M.push (-t,i); }Else if(!Zero) {Zero=true; Pzero=i; }} sort (P.R,P.R+p.sz,cmp); Sort (M.R,M.R+M.SZ,CMP2); intm1l,m1r,p1l,p1r; Getbound (M1L,M1R, -, M) Getbound (P1L,P1R, -, P)intSZ =0; intT;  for(t = p1r; t < P.sz; t++) Add (p,t)intOdd = (M.SZ-M1R) &1;  for(t = m1r+odd; t < M.sz; t++) Add (m,t)if(odd) {if(m1r>0&& m[m1r]/10000. *m[m1r-1] >1) {Add (M,M1R) Add (m,m1r-1)  }    }    if(!SZ) {        intPsz = p.sz, Msz =M.sz; if(psz) {if(msz>=2&& m[msz-1]*m[msz-2] > p[psz-1]* -) {Add (M,msz-1) Add (m,msz-2)            }Else{ADD (p,psz-1) }        }Else {            if(msz>=2) {Add (M,msz-1) Add (m,msz-2)            }Elseans[sz++] =Pzero; }} sort (Ans,ans+sz); printf ("%d\n%d", sz,ans[0]);  for(inti =1; I < sz; i++) printf ("%d", Ans[i]); Putchar ('\ n'); return 0;}

Codeforce Gym 100685E Epic Fail of a Genie (greedy)

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.