bzoj1226: [SDOI2009] School canteen dining pressure DP

Source: Internet
Author: User

Description

Little F's school is in a remote corner of the city, and all the students have to eat at school. The school has a canteen, although humble, but the canteen chef can always make students satisfied with the dishes. Of course, the tastes of different people are not necessarily the same, but everyone's tastes can be expressed in a non-negative integer. Due to insufficient manpower, the canteen can only cook for one person at a time. The time to do each dish is related to the previous dish, if the corresponding taste of the previous dish is a, this is a B, then the time required for this dish is (a or B)-(A and B), and the first course is not required to calculate the time. where, OR and and represents integer bitwise OR arithmetic and bitwise and operations, the corresponding operator in the C language is "|" and "&". The number of students compared to this school is still more, eat cooking often will spend a lot of time. As a result, the school canteen occasionally does not follow the order of everyone to cook, to shorten the total meal time. Although the students can understand the school canteen this practice, but each classmate still have a certain tolerance. In other words, the group I classmate, up to allow the bi person behind him to get the food first. Once after this, any classmate than the current students to get the rice, the current students will be very angry. Therefore, the canteen cooking also have to take care of the students mood. Now, Little F wants to know how long it will take to finish these dishes in your own school cafeteria, in the context of satisfying everyone's tolerance.

Input

The first line contains a positive integer c, which represents the number of data groups for the test point. The first row of each group of data contains a positive integer n, which indicates the number of classmates. The second row of each set of data is a total of n rows, each containing two non-negative integers separated by a space of TI and Bi, representing the taste of the dishes and the endurance of the classmate in order of the previous students in the queue. There are no extra blank lines between each set of data.

Output

Contains line C, an integer per line, indicating the minimum time required for the canteen to complete all the dishes in the corresponding data.

Exercises

We observe the data, and only the endurance range can be compressed in state. So\ (f[i][j][k]\)Represents the previous\ (i-1\)The shortest time it takes to get a meal,\ (j\)Said\ (i\)After\ (7\)The state of the individual (the last one is\ (i\)Status, left, and easy to delete\ (i\)'s state),\ (k\)Said\ (i\)Ago\ (8\)Individuals (may be able to\ (i\)Impact), post-\ (7\)Personal\ (i\)may have an impact on it) and\ (i\)One of the first to lead the meal.
For\ (i\)had been brought to the food, and transferred directly to\ (i+1\)
\[f[i+1][j>>1][k-1]=min (f[i+1][j>>1][k-1],f[i][j][k]) \]
And for\ (i\)Without a meal, we vigorously violent to the right sweep. We remember\ (rn\)To endure the range, if the sweep of the person beyond the scope of endurance, jump out of the loop, starting from their own sweep, each time to update the minimum value of endurance, and then to sweep the person's answer to update. More specific, you can see the code comment

Code
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int Inf=0x3f3f3f3f;const int maxn= (1<<8) +1;int n;int t[1010],b[1010];int f[1010][maxn][20];int Tim (int X,int y) {return t[x]^t[y];}    X|y-x&y=x^yint Main () {Ios::sync_with_stdio (false);    int c;cin>>c;        while (c--) {memset (f,inf,sizeof (f));        cin>>n;        for (int i=1;i<=n;i++) cin>>t[i]>>b[i];        f[1][0][7]=0; for (int i=1;i<=n;i++) for (int j=0;j< (1<<8); j + +) for (int k=0;k<=15;k++) {//0~7 represents the first 8 individuals who have the potential to affect him, 8                On behalf of himself, 7~15 represents the first person after 7 persons he may affect if (f[i][j][k]==inf) continue;                    if (j&1) f[i+1][j>>1][k-1]=min (F[i+1][j>>1][k-1],f[i][j][k]);//i led the meal else{ int rn=inf;//Endurance range for (int h=0;h<=7;h++) {if (j& (1<

bzoj1226: [SDOI2009] School canteen dining pressure DP

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.