HDU – ntzyz's blog https://archive.ntzyz.io Mon, 18 Sep 2017 12:20:18 +0000 zh-CN hourly 1 https://wordpress.org/?v=5.8 [HDU 5504] GT and sequence https://archive.ntzyz.io/2015/10/28/hdu-5504-gt-and-sequence/ https://archive.ntzyz.io/2015/10/28/hdu-5504-gt-and-sequence/#comments Wed, 28 Oct 2015 13:48:10 +0000 https://blog.dimension.moe/?p=241 继续阅读[HDU 5504] GT and sequence]]> 题目好像不难欸,思路很清晰的……但是WA了好久……(摔
题目链接:Problem – 5504
思路很简单,既然题目保证了不会溢出,那就开了long long直接上。
对于所有正数,全部直接乘绝对是最优解。
对于偶数个负数,全部相乘后负号自然就都没了,还是全部乘上去。
最后,奇数个负数,那么就去掉绝对值最小的负数,然后将余下的偶数个奇数全部相乘即可。

这个思路是没问题的是个人都想得到,但是实际写的时候问题颇多。此题出自Bestcoder,当时情况是这样的:(摔

Accepts: 95   Submissions: 1467

_(:з」∠)_

不许吐槽代码里的LoveLive!

代码:

[cc lang=”cpp”]#include

using namespace std;

typedef long long LoveLive;

int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T–) {
int n;
cin >> n;
vector la, lb;
LoveLive result = 1;
for (int i = 0; i != n; i++) {
LoveLive temp;
cin >> temp;
if (temp > 0)
la.push_back(temp);
else if (temp < 0) lb.push_back(temp); } for (auto i: la) result *= i; if (lb.size() == 0 && la.size() == 0) { cout << 0 << endl; continue; } if (lb.size() == 1 && la.size() == 0) { if (n == 1) { cout << lb[0] << endl; continue; } else { cout << 0 << endl; continue; } } if (lb.size() & 1) { sort(lb.begin(), lb.end()); lb.pop_back(); } for (auto i: lb) result *= i; if (la.size() + lb.size() == n) { cout << result << endl; continue; } else cout << max(static_cast(0), result) << endl; } return 0; } [/cc]

]]>
https://archive.ntzyz.io/2015/10/28/hdu-5504-gt-and-sequence/feed/ 1
[HDU1728] 逃离迷宫 https://archive.ntzyz.io/2015/10/10/hdu1728/ https://archive.ntzyz.io/2015/10/10/hdu1728/#respond Sat, 10 Oct 2015 04:22:32 +0000 https://blog.dimension.moe/?p=185 继续阅读[HDU1728] 逃离迷宫]]> 题目链接 Problem – 1728

最终在王学姐的助攻下A了……不简单啊……

一个深搜居然花了两天 12次提交后才A……

过两天把宽搜版本也顺便改下交掉……

一般深搜没有回溯或多或少都有一些问题
bfs状态记录不全也会有一些问题

嗯……下面贴代码……

#include 

using namespace std;

const int LEFT  = 0;
const int RIGHT = 1;
const int UP    = 2;
const int DOWN  = 3;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

char data[150][150];    // Storage the map.
int visited[150][150];  // The counter of how many times has we turned when we reach here
bool found;             // whether the solution is found or not
int m, n, k;            // the same meaning with the problem description

void dfs(int startx, int starty, int endx, int endy, int vec, int c = 0) {
    // Check if we has used up the chance.
    if (c > k) return;
    // Check if we has already reached here before.
    if (visited[starty][startx] < c) return;
    // Mark the position where we are located in.
    visited[starty][startx] = c;

    // Check if we reach (endx, endy) with vec
    int x = startx, y = starty; // (x, y) means where we are.
    for (;;) {
        x += dx[vec];
        y += dy[vec];
        // Check if we reach outside of the map or reach the block in the map.
        if (x < 0 || y < 0 || x >= n || y >= m || data[y][x] == '*')
            break;
        // Check if we reach (endx, endy)
        if (x == endx && y == endy) {
            found = true;
            return;
        }
    }
    // We did not reach the destination directly, so we should turn around at one corner and continue the test.
    x = startx, y = starty; // (x, y) means where we are.
    for (;;) {
        x += dx[vec];
        y += dy[vec];
        // Check if we reach outside of the map or reach the block in the map.
        if (x < 0 || y < 0 || x >= n || y >= m || data[y][x] == '*')
            break;
        // Let's turn at this corner.
        switch (vec) {
        case LEFT:
        case RIGHT:
            dfs(x, y, endx, endy, UP, c + 1);
            if (found) return;
            dfs(x, y, endx, endy, DOWN, c + 1);
            if (found) return;
            break;
        case UP:
        case DOWN:
            dfs(x, y, endx, endy, LEFT, c + 1);
            if (found) return;
            dfs(x, y, endx, endy, RIGHT, c + 1);
            if (found) return;
            break;
        }
        // Mark the place we are now.
        visited[y][x] = c;
    }
}

int main() {
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while (T--) {
        cin >> m >> n;
        for (int i = 0; i != m; ++i)
            cin >> data[i];
        int startx, starty, endx, endy;
        cin >> k >> startx >> starty >> endx >> endy;
        startx--, starty--, endx--, endy--;
        // Check if the starting point is the same as target.
        if (startx == endx && starty == endy) {
            cout << "yes" << endl;
            continue;
        }
        found = false;
        for (auto &i: visited)
            fill(i, i + 150, k);
        // The first step has no direction, just try the four direction one by one.
        for (int i = 0; i < 4; i++) {
            visited[starty][startx] = k;
            dfs(startx, starty, endx, endy, i);
            if (found) break;
        }
        if (found)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}
]]>
https://archive.ntzyz.io/2015/10/10/hdu1728/feed/ 0
[Coding] 开源!我的所有AC代码。 https://archive.ntzyz.io/2015/09/02/coding-%e5%bc%80%e6%ba%90%ef%bc%81%e6%88%91%e7%9a%84%e6%89%80%e6%9c%89ac%e4%bb%a3%e7%a0%81%e3%80%82/ https://archive.ntzyz.io/2015/09/02/coding-%e5%bc%80%e6%ba%90%ef%bc%81%e6%88%91%e7%9a%84%e6%89%80%e6%9c%89ac%e4%bb%a3%e7%a0%81%e3%80%82/#respond Tue, 01 Sep 2015 16:03:05 +0000 http://blog.dimension.moe/?p=92 继续阅读[Coding] 开源!我的所有AC代码。]]> 以前经常重装系统导致大量代码的丢失(然而并没有什么重要的代码)……

为了避免这种可啪的事再次发生,我决定……

Accepted-codes – Coding.net

反正就放在coding上吧。我会随着A题的进度把代码贴上去。同时如果有了更快的算法我也会更新掉低效的代码。

]]>
https://archive.ntzyz.io/2015/09/02/coding-%e5%bc%80%e6%ba%90%ef%bc%81%e6%88%91%e7%9a%84%e6%89%80%e6%9c%89ac%e4%bb%a3%e7%a0%81%e3%80%82/feed/ 0
[HDU1008] Elevator https://archive.ntzyz.io/2015/08/06/hdu1008-elevator/ https://archive.ntzyz.io/2015/08/06/hdu1008-elevator/#respond Thu, 06 Aug 2015 04:45:03 +0000 http://blog.dimension.moe/?p=10 继续阅读[HDU1008] Elevator]]> 题目很简单,一次就能A,把代码拉过来测试一下语法高亮~

#include 

int main() {
    int n;
    while (std::cin >> n) {
        if (!n)
            return 0;
        int currFloor = 0;
        int sum = 0, aim;
        while (n--) {
            std::cin >> aim;
            sum += ((currFloor > aim) ? (4) : (-6)) * (currFloor - aim) + 5;
            currFloor = aim;
        }
        std::cout << sum << std::endl;
    }

    return 0;
}
]]>
https://archive.ntzyz.io/2015/08/06/hdu1008-elevator/feed/ 0