hiho太阁面经算法竞赛10

题目1 : Binary Watch

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Consider a binary watch with 5 binary digits to display hours (00 - 23) and 6 binary digits to display minutes (00 - 59).

For example 11:26 is displayed as 01011:011010.

Given a number x, output all times in human-readable format “hh:mm” when exactly x digits are 1.

输入
An integer x. (0 ≤ x ≤ 9)

输出
All times in increasing order.

样例输入
1
样例输出
00:01
00:02
00:04
00:08
00:16
00:32
01:00
02:00
04:00
08:00
16:00

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long LL;


int main() {
    // freopen("input", "r", stdin);
    int x; cin >> x;
    for (int i = 0; i < (1 << 11); i++) {
        if (__builtin_popcount(i) == x) {
            int hour = i >> 6;
            int minute = i & 0x3f;
            if (hour < 24 && minute < 60) {
                cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << endl;
            }
        }
    }
    return 0;
}

— Built-in Function: int __builtin_ffs (unsigned int x)

Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.
返回右起第一个‘1’的位置。

— Built-in Function: int __builtin_clz (unsigned int x)

Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.
返回左起第一个‘1’之前0的个数。

— Built-in Function: int __builtin_ctz (unsigned int x)

Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined.
返回右起第一个‘1’之后的0的个数。

— Built-in Function: int __builtin_popcount (unsigned int x)

Returns the number of 1-bits in x.
返回‘1’的个数。

— Built-in Function: int __builtin_parity (unsigned int x)

Returns the parity of x, i.e. the number of 1-bits in x modulo 2.
返回‘1’的个数的奇偶性。

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Given N lists of customer purchase, your task is to find the products that appear in all of the lists.

A purchase list consists of several lines. Each line contains 3 parts: the product id (format XXXX-XXXX), the purchase date (format mm/dd/yyyy) and the price (with decimal places). Two product are considered equal if both the product id and the price are equal.

输入
The first line contains an integer N denoting the number of lists. (1 ≤ N ≤ 1000)

Then follow N blocks. Each block describes one list.

The first line of each block contains an integer M denoting the number of products in the list. (1 ≤ M ≤ 50)

M lines follow. Each line contains the product id, the purchase date and the price.

输出
The products that appear in all of the lists. You should output the product id in increasing order.

If two different product share the same id (with different price) you should output the id twice.

样例输入
3
2
1111-1111 07/23/2016 998.00
1111-2222 07/23/2016 888.00
2
1111-2222 07/23/2016 888.00
1111-1111 07/23/2016 998.00
2
1111-2222 07/23/2016 888.00
1111-1111 07/23/2016 999.00
样例输出
1111-2222

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

struct Product {
    string id, price;
    Product(string id_, string price_): id(id_), price(price_) {}
    bool operator<(const Product& p) const {
        return (id + price) < (p.id + p.price);
    }
    bool operator==(const Product& p) const {
        return id == p.id && price == p.price;
    }
};

map<Product, int> cnt;

int main() {
    // freopen("input", "r", stdin);
    int N; cin >> N;
    for (int i = 0; i < N; i++) {
        int M; cin >> M;
        set<Product> s;
        for (int j = 0; j < M; j++) {
            string id, date, price;
            cin >> id >> date >> price;
            s.insert(Product(id, price));
        }
        for (const auto& p : s) {
            cnt[p]++;
        }
    }
    vector<string> ret;
    for (const auto& pair: cnt) {
        if (pair.second >= N) {
            ret.push_back(pair.first.id);
        }
    }
    sort(ret.begin(), ret.end());
    for (int i = 0; i < ret.size(); i++) {
        cout << ret[i] << endl;
    }
    return 0;
}

题目3 : Counting Islands II

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Country H is going to carry out a huge artificial islands project. The project region is divided into a 1000x1000 grid. The whole project will last for N weeks. Each week one unit area of sea will be filled with land.

As a result, new islands (an island consists of all connected land in 4 – up, down, left and right – directions) emerges in this region. Suppose the coordinates of the filled units are (0, 0), (1, 1), (1, 0). Then after the first week there is one island:

#…
….
….
….
After the second week there are two islands:

#…
.#..
….
….
After the three week the two previous islands are connected by the newly filled land and thus merge into one bigger island:

#…
##..
….
….
Your task is track the number of islands after each week’s land filling.

输入
The first line contains an integer N denoting the number of weeks. (1 ≤ N ≤ 100000)

Each of the following N lines contains two integer x and y denoting the coordinates of the filled area. (0 ≤ x, y < 1000)

输出
For each week output the number of islands after that week’s land filling.

样例输入
3
0 0
1 1
1 0
样例输出
1
2
1

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long LL;

const int MAX = 1010;
int fa[MAX * MAX];
int grid[MAX][MAX];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

void clear() {
    memset(fa, -1, sizeof fa);
    memset(grid, 0, sizeof grid);
}

int findset(int x) {
    if (fa[x] == -1) {
        return fa[x] = x;
    }
    return x == fa[x] ? x : fa[x] = findset(fa[x]);
}

void unionset(int x, int y) {
    int fx = findset(x), fy = findset(y);
    if (fx != fy) {
        fa[fx] = fy;
    }
}


int main() {
    // freopen("input", "r", stdin);

    clear();
    int cnt = 0;
    int N; cin >> N;
    for (int i = 0; i < N; i++) {
        int x, y; cin >> x >> y;
        grid[x][y] = 1;
        cnt++;
        for (int j = 0; j < 4; j++) {
            int nx = x + dx[j];
            int ny = y + dy[j];
            if (nx >= 0 && ny >= 0 && nx < 1000 && ny < 1000) {
                if (grid[nx][ny] && findset(nx * 1000 + ny) != findset(x * 1000 + y)) {
                    cnt--;
                    unionset(nx * 1000 + ny, x * 1000 + y);
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

代码取自第一名zp19911109,值得学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值