1. 程式人生 > 實用技巧 >HDU 6778 Car (狀壓DP)

HDU 6778 Car (狀壓DP)

Car

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 272Accepted Submission(s): 113


Problem Description W 市最近面臨了嚴重的交通擁堵問題,現在決定要在工作日(週一到週五)限號。
每天可以限制若干尾號的車輛,譬如說週一限尾號為 0 的車,週二限尾號為 1,2 的車。

每個尾號在五天當中最多隻能被限一次,一天也可以什麼牌照都不限。

我們要設定一個容量上限m,使得至少存在一種方案,每一天不被限號的車的總數都小於等於m

請求出最小的m

Input 第一行一個整數test(1test10)表示資料組數。

對於每組資料,第一行一個正整數n(1n10000)表示這個城市裡有多少輛車。

接下來n行,每行一個字串表示車牌。車牌由 5 位字元構成,每位都是'0'-'9'的數字。兩輛車的車牌可能相同。

Output 對於每組資料,一行一個整數表示答案。

Sample Input 2 1 00000 10 00000 00001 00002 00003 00004 00005 00006 00007 00008 00009

Sample Output 1 8

Source 2020 年百度之星·程式設計大賽 - 初賽二

Recommend heyang

Statistic
|Submit|Discuss|Note

析:我做的方式和官方題解不一樣,我沒有使用二分,直接dp[i][state]表示前i天,已經限號的尾號的狀態是state,在第i 天時,只需要每次列舉剩下的還沒有限號的尾號,需要和前i-1取的最大值,然後再在第i天中取個最小值即可

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#include <unordered_map>
#define debug() puts("++++")
#define print(x) cout<<(x)<<endl
// #define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define FOR(i,n,x)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1024 + 7;
const int maxm = 2000000 + 7;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
int n, m;

inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){
  int x;  cin >> x;  return x;
}

const int limit = 1024;


unordered_map<int, int> mp;

int dp[7][maxn];

int get_num(int state){
  int res = n;
  for(int i = 0; i < 10; ++i)
    if(state&1<<i)  res -= mp[i];
  return res;
}

int main(){
  int T;  scanf("%d", &T);
  while(T--){
    scanf("%d", &n);  mp.cl;
    for(int i = 0; i < n; ++i){
      scanf("%d", &m);
      ++mp[m%10];
    }
    ms(dp, INF);
    for(int i = 0; i < limit; ++i){
      int res = n;
      for(int j = 0; j < 10; ++j)
        if(i&1<<j)  res -= mp[j];
      dp[1][i] = res;
    }

    for(int i = 2; i <= 5; ++i){
      for(int j = 0; j < limit; ++j){
        int s = j ^ limit - 1;
        for(int s0 = s; s0; s0 = (s0-1) & s){
          dp[i][j|s0] = min(dp[i][j|s0], max(get_num(s0), dp[i-1][j]));
        }
      }
    }

    int ans = INF;
    for(int i = 0; i < limit; ++i)
      ans = min(ans, dp[5][i]);
    printf("%d\n", ans);

  }
  return 0;
}