zoukankan      html  css  js  c++  java
  • poj 2362

    POJ 2362 Square

    最近不行了,这么个简单的深搜还超时了N多次/*

     * File:   main.cpp
    * Author: Administrator
    *
    * Created on 2011年7月16日, 上午11:29
    */

    #include <stdio.h>
    #include <stdlib.h>

    #ifdef DEBUG
    #define debug(fmt,...) (fprintf(stderr,"%d: ",__LINE__)+fprintf(stderr,fmt,##__VA_ARGS__))
    #else
    #define debug(...)
    #endif

    #define bool int
    #define true 1
    #define false 0

    int n, aver, mask;
    int a[30];

    int cmp(const void *a, const void *b) {
    return *(int *) b - *(int *) a;
    }

    bool dfs(int i, int xx, int cur) {
    if (xx == 0) {
    if (cur == mask) return true;
    for (i = 0; i < n; ++i) {
    if (cur & (1 << i)) continue;
    cur |= 1 << i;
    xx = a[i++];
    break;
    }
    }
    for (; i < n; ++i) {
    if (cur & (1 << i)) continue;
    if (xx + a[i] == aver) return dfs(i + 1, 0, cur | (1 << i));
    if (xx + a[i] < aver && dfs(i + 1, xx + a[i], cur | (1 << i))) return true;
    }
    return false;
    }

    bool solve() {
    int i, cur;
    for (i = aver = 0; i < n; ++i) {
    aver += a[i];
    }
    if (aver % 4) return false;
    aver /= 4;
    qsort(a, n, sizeof(a[0]), cmp);
    if (a[0] > aver) return false;
    cur = 0;
    for (i = 0; i < n; ++i) {
    cur |= 1 << i;
    if (a[i] == aver) continue;
    mask = (1 << n) - 1;
    return dfs(i + 1, a[i], cur);
    }
    return true;
    }

    int main() {
    int i, cas;
    scanf("%d", &cas);
    while (cas-- && 1 == scanf("%d", &n)) {
    for (i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    }
    puts(solve() ? "yes" : "no");
    }
    return 0;
    }

    /*
    11
    20 491 1050 1123 376 1045 1391 656 190 1305 537 20 533 1193 465 398 1258 756
    1005 949 63
    20 1431 949 852 616 899 1257 413 335 528 107 1126 146 782 571 407 114 1393
    681 1229 1340
    20 715 321 723 362 778 156 402 25 145 414 571 835 198 393 289 851 390 234
    1288 450
    20 1450 1289 710 1209 287 202 1394 732 480 1121 1246 286 146 1333 1467 926
    471 453 921 889
    20 407 1269 625 577 184 617 1357 994 107 250 783 117 1352 1355 820 16 336
    147 697 1134
    20 679 1102 1502 1321 374 1472 288 428 1210 1497 1244 1412 573 1224 654 1465
    528 891 1417 311
    20 1063 872 1391 987 513 566 1258 831 1295 604 1512 549 365 712 1146 1133 89
    561 1065 1476
    20 1515 1473 1033 159 312 502 1342 1097 1231 476 1419 737 1200 174 1486 1311
    1261 416 1109 43
    20 930 1165 584 56 1122 858 955 564 932 375 1267 580 1515 553 423 467 605
    722 1183 792
    20 1035 1338 77 1253 243 715 177 951 852 1409 736 1170 694 945 555 289 1410
    539 869 1059
    4 1 1 1 1

    */
  • 相关阅读:
    python学习 day6 (3月7日)
    day05作业---字典
    day04 列表
    python学习 day5 (3月6日)
    python学习 day4 (3月5日)---列表
    Head First JavaScript 第九章:9 异步编码——处理事件
    Head First JavaScript 第八章:8 编写一个应用程序
    Head First JavaScript 第七章:7 类型、相等、转换等系统地讨论类型
    《深入理解C指针》第五章 指针和字符串
    Architecture of a Database System论文——第四章:关系查询处理器
  • 原文地址:https://www.cnblogs.com/zhanhb/p/2108111.html
Copyright © 2011-2022 走看看