zoukankan      html  css  js  c++  java
  • HDU 5355 Cake (构造 + 暴力)

    题意:给定 n,m,让你把 1 ~ n 分成 m 部分,而且每部分和是一样大的。

    析:首先先判断不能分成的,第一种是 sum (1 ~ n 的和)不能被 m 整除,或者 sum / m < n,其他的情况都有解。

    这个题采用的是构造加暴力搜索的思想,首先,先成对的构造解,也就 2 * m 个,每 2 * m 组,分别放到 m 个部分,这样都每部分的贡献都是一样的(最大的和最小一组,次大和次小等等),然后剩下的部分进行搜索暴力,但是要注意的是,如果剩下的不够 m 个的话,这样是不可能搜索出解的,要再加上一个 2*m,但是加上一个 2 * m,复杂度就大了很多,会超时,所以可以对于大于 20 的再减去 m,这样就能够少搜索一部分。就是有一组神数据,那就是 20 6 (这个是我用暴力找出来的),如果特判这个数据的话 嘻嘻 就更容易了。

    代码如下:

    #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>
    #define debug() puts("++++")
    #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 = 1e5 + 20;
    const int maxm = 1e6 + 10;
    const LL mod = 1000000000000000LL;
    const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
    const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    inline int readInt(){ int x;  scanf("%d", &x);  return x; }
    
    int a[50][50];
    int b[50];
    
    bool dfs(int cur){
      if(cur == 0)  return true;
      for(int i = 0; i < m; ++i)  if(a[i][40] + cur <= b[i]){
        a[i][40] += cur;
        a[i][++a[i][0]] = cur;
        if(dfs(cur-1))  return true;
        a[i][40] -= cur;
        --a[i][0];
      }
      return false;
    }
    
    int main(){
      int T;  cin >> T;
      while(T--){
        scanf("%d %d", &n, &m);
        vector<int> ans[20];
        LL sum = (LL)n * (n+1) / 2;
        LL p = sum / m;
        if(sum % m || sum < (LL)n * m){ puts("NO");  continue; }
        puts("YES");
        int t = n % (m<<1);
        if(t)  t += m<<1;
        t = min(t, n);
        for(int i = t+1; i < n; i += (m<<1)){
          for(int j = 0; j < m; ++j)  ans[j].pb(i+j);
          for(int j = 0; j < m; ++j)  ans[j].pb(i+(m<<1)-j-1);
        }
        sum = t * (t+1) / 2 / m;
        for(int i = 0; i < m; ++i)  b[i] = sum;
        if(t > 20){
          for(int i = t, j = 0; j < m; ++j, --i)  b[j] -= i, ans[j].pb(i);
          t -= m;
        }
        ms(a, 0);
        dfs(t);
        for(int i = 0; i < m; ++i){
          printf("%d", ans[i].sz + a[i][0]);
          for(int j = 0; j < ans[i].sz; ++j)  printf(" %d", ans[i][j]);
          for(int j = 1; j <= a[i][0]; ++j)  printf(" %d", a[i][j]);
          printf("
    ");
        }
      }
      return 0;
    }
    

      

  • 相关阅读:
    tomcat 支持https
    linux环境下jdk 安装以及maven私服搭建
    消息中间间初识
    HDU 5527 Too Rich
    HDU 5534 Partial Tree
    HDU 5543 Pick The Sticks
    HDU 5542 The Battle of Chibi dp+树状数组
    CodeForces 842D Vitya and Strange Lesson
    Codeforces 858D Polycarp's phone book
    HDU 5489 Removed Interval
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/8974806.html
Copyright © 2011-2022 走看看