Codeforces round #127 (Div. 1), problem: (c) Fragile bridges priority queue

Source: Internet
Author: User

The movement of this person is similar to the 77C-Beavermuncher-0xFF to find the last point that can be traveled most.

Practice: Suppose a person finally returns to J, can walk through the most point, can first find the i-0-i, I-n-I, can walk through the most points, this can be directly greedy, the point where the bridges between I-n are collapsed can be the most common.

You can also find the sum [I] of the vertices that can be done at most without passing through the two-point range.

Then DP [J] = sum [J]-sum [I] + left [J] + right [I] = sum [J] + left [J] + left [I] -sum [I], this can be recorded in a monotonous queue.

This type of question should first enumerate the starting point and the ending point, and then judge the going back and not going back. The movements here can be categorized. Do not be confused by situations that seem unnecessary at the beginning.

# Include <vector> # include <algorithm> # include <cstdio> # include <cstring> # include <queue> # define ll long longconst int LMT = 100005; using namespace STD; ll lef [LMT], rig [LMT], sum [LMT], Bri [LMT]; struct comp {bool operator () (const int I, const Int J) {return lef [I]-sum [I] <lef [J]-sum [J] ;}}; priority_queue <int, vector <int>, comp> que; int main (void) {int N; ll ans = 0; scanf ("% d", & N); For (INT I = 1; I <n; I ++) {scanf ("% I64d", & Bri [I]); If (BRI [I]> = 2) lef [I] + = lef [I-1]; lef [I] + = Bri [I] & 1? Bri [I]-1: Bri [I]; sum [I] + = sum [I-1] + (BRI [I] & 1? Bri [I]: Bri [I]-1) ;}for (INT I = n-1; I> = 0; I --) {rig [I] + = Bri [I + 1] & 1? Bri [I + 1]-1: Bri [I + 1]; If (BRI [I + 1]> = 2) rig [I] + = rig [I + 1]; // depends on !!} For (INT I = 0; I <n; I ++) {que. push (I); ans = max (ANS, Lef [Que. top ()]-sum [Que. top ()] + sum [I] + rig [I]);} printf ("% i64d \ n", ANS); Return 0 ;}

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.