poj3347(思維題)

如圖擺放正方形,問從上往下看可以看到哪些正方形,實在想不到怎麼求每個正方形的座標

看到心裏只有學習 天下欲傾,利刃安在。 計算幾何 poj 3347這個博客才豁然開朗。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const int mx = 105;
const double eps = 1e-8;
int n, s[mx], t[mx], ans[mx];

int main() {
    while (scanf("%d",&n) && n != 0) {
        for (int i = 1; i <= n; i++) {
            scanf("%d",&s[i]);
        }
        t[1] = s[1];
        for (int i = 2; i <= n; i++) {
            int now = 0;
            for (int j = 1; j < i; j++) {
                now = max(now, t[j] + 2*min(s[j],s[i]));
            }
            now = max(now, s[i]);
            t[i] = now;
        }
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            int l = 0, r = 0;
            for (int j = 1; j < i; j++) {
                if (s[j] > s[i])//這個if其實可以去掉的
                    l = max(l, s[i] + s[j] - (t[i] - t[j]));
            }
            for (int j = i+1; j <= n; j++) {
                if (s[j] > s[i])
                    r = max(r, s[i] + s[j] - (t[j] - t[i]));
            }
            if ((l + r) < 2*s[i])
                ans[cnt++] = i;
        }
        for (int i = 0; i < cnt; i++) {
            printf("%d%c",ans[i],i==cnt-1?'\n':' ');
        }
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章