Logu P1282 Domino and p1282 Domino

Source: Internet
Author: User

Logu P1282 Domino and p1282 Domino
Description

Domino has two upper and lower squares, each of which is 1 ~ Six points. Existing

The sum of points in the upper square is S1, the sum of points in the lower square is S2, their difference is | S1-S2 |. For example in Figure 8-1, S1 = 6 + 1 + 1 + 1 = 9, S2 = 1 + 5 + 3 + 2 = 11, | S1-S2 | = 2. Each Domino card can be rotated by 180 °, so that the upper and lower squares are interchangeable. Programming uses the minimum number of rotations to minimize the number of points between the upper and lower 2 rows of a domino card.

For the example in the figure, as long as the last domino card is rotated 180 °, the difference between the two upper and lower rows is 0.

Input/Output Format

Input Format:

 

The first line of the input file is a positive integer n (1 ≤ n ≤ 1000), indicating the number of dominoes. The next n rows represent the points of n dominoes. Each line has two positive integers separated by spaces, indicating the number of points a and B in the upper and lower squares of the domino card, and 1 ≤ a, B ≤ 6.

 

Output Format:

 

The output file contains only one line and an integer. Indicates the minimum number of rotations.

 

Input and Output sample input sample #1: Copy
46 11 51 31 2
Output example #1: Copy
1

 

 

 

Ah ah

I haven't had A DP problem for A long time.

$ Dp [I] [j] $ indicates the number of the enumeration to $ I $. The difference is the minimum number of steps required by $ j $.

Next, let's take an enumeration and see if the status can be reached.

Something similar to a backpack

Note that the array may be out of bounds in this question.

So I use 1000 as 0.

 

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int MAXN=2001; 6 inline char nc() 7 { 8     static char buf[MAXN],*p1=buf,*p2=buf; 9     return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++;10 }11 inline int read()12 {13     char c=nc();int x=0,f=1;14     while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}15     while(c>='0'&&c<='9'){x=x*10+c-'0';c=nc();}16     return x*f;17 }18 inline int work(int x)19 {20     int ans=0;21     for(int i=1;i<x;i++)   22         if(x%i==0)  ans+=i;23     return ans;24 }25 int dp[MAXN][2*MAXN];26 int a[MAXN],b[MAXN];27 int zero=1000;28 int main()29 {30     #ifdef WIN3231     freopen("a.in","r",stdin);32     #else33     #endif34     int n=read();35     for(int i=1;i<=n;i++)    a[i]=read(),b[i]=read();36     memset(dp,0x3f,sizeof(dp));37     dp[0][1000]=0;38     for(int i=1;i<=n;i++)39     {40         for(int j=0;j<=2001;j++)41         {42             if(dp[i-1][j]<100000)43             {44                 dp[i][j+(a[i]-b[i])]=min(dp[i][j+(a[i]-b[i])],dp[i-1][j]);45                 dp[i][j+(b[i]-a[i])]=min(dp[i][j+(b[i]-a[i])],dp[i-1][j]+1);46             }47         }48     }49     int anspos=0x7ffff,ans=0x7ffff;50     for(int i=1;i<=2001;i++)51         if(dp[n][i]<=10000&&abs(i-1000)<=anspos)52             anspos=abs(i-1000);53     for(int i=1;i<=2001;i++)54         if(abs(i-1000)==anspos)55             ans=min(ans,dp[n][i]);56     printf("%d",ans);57         58     return 0;59 } 60                     

 

 

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.