ZOJ 2856 Happy Life 暴力求解

由於是Special Judge 的題目,只要輸出正確答案便可,不惟一ios

暴力力求解, 只要每次改變 happiness 值爲負的人的符號便可。c++

若是計算出當前人的 happiness 值爲負,那麼將其 p(i) 值賦值爲-p(i) 便可
這題是保證有解的,至至於爲什麼難以證實。app

 

Source Code:ui

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int N = 210            ;
const int M = 1100011*2      ;
const ll P = 10000000097ll   ;
const int MAXN = 10900000    ;

int a[220][220], n;
int ans[220];

int main(){
    std::ios::sync_with_stdio(false);
    int i, j, t, k, u, v, numCase = 0;

    while (EOF != scanf ("%d",&n)) {
        for (i = 1; i <= n; ++i) {
            for (j = 1; j <= n; ++j) {
                scanf("%d", &a[i][j]);
            }
        }
        for (i = 1; i <= n; ++i)    ans[i] = 1;
        int cnt = 1;
        for (;;) {
            if (cnt > n)   break;
            int sum = 0;
            for (i = 1; i <= n; ++i) {
                sum += a[cnt][i] * ans[i];
            }
            if (sum * ans[cnt] < 0) {
                ans[cnt] = -ans[cnt];
                cnt = 1;
            } else {
                ++cnt;
            }
        }
        printf ("Yes\n");
        for (i = 1; i <= n; ++i) {
            if (ans[i] == 1) {
                printf("+\n");
            } else {
                printf("-\n");
            }
        }
    }

    return 0;
}
相關文章
相關標籤/搜索