zoukankan      html  css  js  c++  java
  • hdu1518 bjfuoj1042 zoj1909 poj2362 经典的搜索加剪枝

    bjfuoj的测试数据最水,用很简单的方法一下就过了,又调了好长时间,才过掉其它OJ上的这道题目~

    /*
    * hdu1518/win.cpp
    * Created on: 2011-11-8
    * Author : ben
    */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <functional>
    #include <numeric>
    #include <cctype>
    using namespace std;
    const int MAXN = 25;
    int lens[MAXN];
    bool visited[MAXN];
    int N, ave, cur_tol_len, totallen;

    inline bool cmp(const int &a, const int &b) {
    return a > b;
    }

    bool myfind(int step, int num) {
    if (num > 3) {
    return true;
    }
    if (step >= N) {
    return false;
    }
    if (!visited[step] && (lens[step] + cur_tol_len) <= ave * num) {
    visited[step] = true;
    cur_tol_len += lens[step];
    if(cur_tol_len == ave * num) {
    if(myfind(0, num + 1)) {
    return true;
    }
    }else if (myfind(step + 1, num)) {
    return true;
    }
    visited[step] = false;
    cur_tol_len -= lens[step];
    }
    return myfind(step + 1, num);
    }

    bool judge() {
    if (N < 4 || totallen % 4 != 0) {
    return false;
    }
    ave = totallen / 4;
    for (int i = 0; i < N; i++) {
    if (lens[i] > ave) {
    return false;
    }
    }
    sort(lens, lens + N, cmp);
    memset(visited, false, sizeof(visited));
    cur_tol_len = 0;
    return myfind(0, 1);
    }

    int main() {
    #ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
    #endif
    int T;
    scanf("%d", &T);
    while (T--) {
    scanf("%d", &N);
    totallen = 0;
    for (int i = 0; i < N; i++) {
    scanf("%d", &lens[i]);
    totallen += lens[i];
    }
    if (judge()) {
    puts("yes");
    } else {
    puts("no");
    }
    }
    return 0;
    }



  • 相关阅读:
    一个界面描述标签的想法
    使用MyGeneration创建模板:介绍(翻译)
    ICE:C#和Java共同的服务器
    Spring.NET 快速入门 (翻译)
    TSQL命令在SQLServer查询中的运用
    SQL数据库修复
    区分汉字和字母的函数
    如何生成静态页
    delphi 开发扩展(一)
    20072008
  • 原文地址:https://www.cnblogs.com/moonbay/p/2241638.html
Copyright © 2011-2022 走看看