Pressure DP Lightoj 1194 colored T-shirts

Source: Internet
Author: User

Give you a sequence of the minimum number of exchanges to put together the same numbers in this sequence

Range of numbers in a sequence 1-16

Number of numbers in a sequence 1-1e5

Ideas:

First preprocessing Cnt[i][j] indicates the number of interchanges to be used before the digit J is placed in the number I

Then enumerate each of the cases 1<<m

For example: M 0

000000000 the first I position is 0, indicating that the number I is disorderly, and vice versa is put good

The code is as follows

/********************************************author:crystalcreated time:file Name:* ********************* /#include <cstdio> #include <cstdlib> #include <iostream> #include < algorithm> #include <cstring> #include <climits> #include <string> #include <vector># Include <cmath> #include <stack> #include <queue> #include <set> #include <map>using Namespace Std;int cnt[20][20];int Pre[20];long long dp[1<<16];int nmin; #define INF 0x3f3f3f3fint main () {Freopen (" In.txt "," R ", stdin),//freopen (" OUT.txt "," w ", stdout), int t;cin >> t;for (int i=1;i<=t;i++) {cout <<" case  "<<" << I << ': ' << '; memset (cnt,0,sizeof cnt); memset (pre,0,sizeof pre); memset (dp,inf,sizeof DP) int n,m;cin >> n >> m;for (int j=0;j<n;j++) {int A; CIN >> a;a--;for (int r=0;r<m;r++) {Cnt[a][r] +=pre[r];//How many times a is required to change a before the R color//Add a number each time to update the front}pre[a]++;} for (int j=0;j<m;j++) for (int r=0;r<m;r++) cout << Cnt[j][r] << endl;dp[0]=0;int s = 1<<m;for (int j=0;j<s;j++)// 0 indicates a disorderly order for (int r=0;r<m;r++) {//r chaos, put all the disorderly order to the side if (J>>r & 1) continue;//if not 1continuelong long cost=0;for ( int k=0;k<m;k++) {if (K==r | | j>>k & 1) Continue;cost + = Cnt[r][k];} Dp[j | 1<<r]=min (dp[j | 1 << r],dp[j]+cost);} cout << dp[s-1] << ' \ n ';} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Pressure DP Lightoj 1194 colored T-shirts

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.