Greedy/two points find Bestcoder Round #43 1002 Pog loves Szh II

Source: Internet
Author: User

Topic Portal

1 /*2 greedy/Two-point lookup: First for ai%=p, then sort, so that you can use binary search in order. The greedy idea is to find a AJ every time to make and for p-1 (if any)3 of course it is possible to have two numbers and more than P, then the value of an is optimal and is compared with an at a time4 Note: You cannot select two identical numbers5 Reflection: The game thought of%p and Sort,lower_bound, but still did not think of this greedy method to ensure maximum value, or the problem does less AH: (6 */7#include <cstdio>8#include <algorithm>9#include <cstring>Ten#include <cmath> One using namespacestd; A  -typedefLong Longll; - Const intMAXN = 1e5 +Ten; the Const intINF =0x3f3f3f3f; - ll A[MAXN]; -  - intMainvoid)//bestcoder Round #43 1002 Pog loves Szh II + { - //freopen ("", "R", stdin); +  A     intN; ll P; at      while(SCANF ("%d%i64d", &n, &p) = =2) -     { -          for(intI=1; i<=n; ++i) {scanf ("%i64d", &a[i]); A[i]%=p;} -Sort (A +1, A +1+n); -  -ll ans =0; in          for(intI=1; i<=n; ++i) -         { to             intpos = Lower_bound (A +1+i, A +1+n, P-a[i])-A; pos--; +             if(POS <= n && pos! = i) ans = max (ans, (a[i] + a[pos])%p); -             if(I! = n) ans = max (ans, (a[i] + a[n])%p); the         } *  $printf ("%i64d\n", ans);Panax Notoginseng     } -  the     return 0; +}

Greedy/two points find Bestcoder Round #43 1002 Pog loves Szh II

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: 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.