# (Linear ordering + DP) Luo Gu P2577 [ZJOI2005] lunch (increase + / provincial election -)

Title Description

The morning training is over, THU ACM team collective lunch, they came to the famous line of N ten canteen. There are two Dafan windows, each moment, only to a person Dafan. Due to everyone's taste (and appetite) different, so they want to eat the food is different, the time it takes to Dafan is unique and individual. In addition, each person to eat are not the same speed, so it takes time to eat may also be different.

THU ACM team meal plan is this: first of all the people divided into two teams, and arrange the order of each team in each one, and then number one team to the number one window to line up Dafan, ranks number two to number two window to line up Dafan. Everyone kick start eating immediately after a meal, immediately after dinner everyone went six collections were taught in the basement of the afternoon training.

Now given everyone Dafan time and dinner time, on request of one of the best units and queuing scheme makes the meal all the time as early as possible.

Assume THU ACM team to reach ten cafeteria at time 0, and there is no canteen to eat other students (only Dafan master). Everyone must and can only be a minute in the ranks. Two windows are operating in parallel independently of each other, and everyone Dafan window and time is irrelevant, so after a meal immediately began to eat, with no delay.

N now given their personal Dafan time and dinner time, the time required for all meal under the best output.

Input Format

The first line of an integer N, N represents the total of the individual.

The following N lines of two integers Ai, Bi. In turn represents the person i Dafan time and dinner time.

Output Format

An integer T, on behalf of the earliest moments after dinner everyone.

Sample input and output

Input # 1
5
2 2
7 7
1 3
6 4
8 5
Output # 1
17

Description / Tips

All input data are positive integers not exceeding 200.

Analysis: First, eat slower, the more first Dafan, greed apparently thought correctness;

According to the first row at dinner time sequence. Then consider the state of the design:

Requires maintenance: the current number of people, time window No. 1, No. 2 window of time, for a total maximum time;

Set F [i] [j] [k] represents the individual before i, j time spent in 1, 2 maximum time spend time k;

(Consider the dimension reduction? Maintain a prefix and an array of storage when Dafan after ordering long! So k + j = sum [i], k = sum [i] -j, success dimension reduction!)

Before F [i] [j] stored in individual flower i j 1 minute maximum meal;

ans = min (f [n] [x]) x∈ [0, sum [s]];

Consider the state transition equation: For the i individual, he can go to No. 1 or No. 2 window, and is likely to have an impact on the original maximum time window of 1 or No. 2

No. 1: When j> = s [i] .a time, i.e. the i-th individual meal can be finished before the time point j, then consider the impact on the number 1:

f [i] [j] = min (f [i] [j], max (f [i-1] [js [i] .a], j + s [i] .b)); followed Effects!

Either the longest time in the past i-1 produced personal or person i have an impact!

number 2:

For either case, f [i] [j] = min (f [i] [j], max (f [i-1] [j], sum [i] -j + s [i] .b)); (2 consider the impact of the window, if more than known, the update)

AC Code:

#include<bits/stdc++.h>
using namespace std;
const int N = 210;
int f[N][N*N];
struct node
{
int a, b;
bool operator <(node z) const
{
return b>z.b;
}
}s[N];
int sum[N];
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d %d", &s[i].a, &s[i].b);
sort(s+1, s+1+n);
for(int i = 1; i <= n; i++)
sum[i] = sum[i-1] + s[i].a;
memset(f, 127, sizeof(f));
f[0][0] = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j <= sum[i]; j++)
{
if(j>=s[i].a) f[i][j] = min(f[i][j], max(f[i-1][j-s[i].a], j+s[i].b));
f[i][j] = min(f[i][j], max(f[i-1][j], sum[i]-j+s[i].b));
}
}
int ans = 2147483647;
for(int i = 0; i <= sum[n]; i++)
ans = min(ans, f[n][i]);
printf("%d\n", ans);
return 0;
}

 

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11441729.html