Codeforce 607a&&608c Chain Reaction

Source: Internet
Author: User

[Subject Portal]---------------> http://codeforces.com/problemset/problem/608/C

Topic

  

There is N beacons located at distinct positions on a number line. The I -th Beacon has position a I and power level b I when the I -th beacons are activated, it destroys all beacons to their left (direction of decreasing coordinates) within distance b i Inclusive. The beacon itself is not destroyed however. Saitama would activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.

Saitama wants Genos to add a beacons strictly to the right of all the existing beacons, with any position and any power Level, such the least possible number of beacons is destroyed. Note that Genos ' s placement of the beacon means it'll be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.

Input

The first line of input contains a single integer n (1≤ n ≤100)-the initial number of being Acons.

The i-th of next n lines contains II integers ai and b c10>i (0≤ ai ≤1, 1≤ bi ≤1 000 000) -the position and power level of the I-th Beacon respectively. No. Beacons'll has the same position, so aiaJ If iJ.

Output

Print a single integer-the minimum number of beacons this could be destroyed if exactly one beacons is added.

Sample Test (s) Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3

Ideas

DP + two points

Read into the lighthouse data, position and energy into a pair and then sort in ascending order.

Dis[i] on behalf of Dis[i] was lit when the number of left-hand lighthouses was destroyed. The most left-hand beacon Energy, no matter how much, is not destroyed, so dis[0] = 0;

Next State transfer (for k = 1; K < n; k++), for the K-Lighthouse is transferred from the previous "Lighthouse not lit by the K lamp", plus the number of lighthouses destroyed by the light of the K-Lighthouse, if all the lighthouse on the left of the first K lighthouse are destroyed, then dp[k] = k;

Analysis, the question asked to join a right-hand lighthouse, in fact, let us "from the beginning of the sequence directly from the far right to remove a number of lighthouses as destroyed" and then the remaining sequence to continue the light operation to calculate the number of the destroyed lighthouse. Find the minimum number of all possible scenarios.

Enumeration removes the rightmost 0 lighthouse, a lighthouse, two lighthouses .... T Lighthouse

When a T-lighthouse is removed, the total number of lighthouses destroyed is T + dis[n-1-T];

Find the smallest.

Code

  

1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <algorithm>5 using namespacestd;6 #definePOS First7 #definePower second8 #defineMAX 1000109 #definePLL pair<int,int>Ten #defineLL Long Long One  A LL Dis[max]; - PLL Node[max]; -ll min (ll a, ll b) {returnA < b?a:b;} the  - intMain () - { -     intN; +CIN >>N; -dis[0] =0; +     /////////////////// A      for(inti =0; I < n; i++) at     { -CIN >> Node[i].pos >>Node[i].power; -     } -Sort (node, node +n); -      for(inti =1; I < n; i++) -     { in         intCurPos =Node[i].pos; -         intCurpower =Node[i].power; to         intIn_pos = lower_bound (node, node + N, Make_pair (Node[i].pos-node[i].power,0)) -node; +         if(In_pos >0) -         { theDis[i] = Dis[in_pos-1] + (I-in_pos); *         } $         ElsePanax Notoginseng         { -Dis[i] =i; the         } +     } ALL ans =N; the      for(inti =0; I < n; i++) +     { -Dis[i] = Dis[i] + (N-i-1); $Ans =min (Dis[i], ans); $     } -cout << ans <<Endl; -     return 0; the  -}

Codeforce 607a&&608c Chain Reaction

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.