codeforces round 291 div2 題解

A. Chewbaсca and Number
對於給定的一個正整數n(1<=n<=10^18),我們可以將這個數的某些位翻轉,所謂翻轉是,將數字t用數字9-t替代。經過某些替換後,我們所能得到的最小正整數是多少。題目還說數字不能以0開頭,所以當9爲第一個數字時,是不能進行替換的。當然,也可能有其他的理解,不過命題者的意思是這樣的。其他的情況就讓數字儘可能小就行了。
/*************************************************************************
    > File Name: a.cpp
    > Author: gwq
    > Mail: [email protected] 
    > Created Time: 2015年04月28日 星期二 15時49分41秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;

const double esp = 1e-5;

#define N 20

char str[N];

int main(int argc, char *argv[])
{
    while (scanf("%s", str) != EOF) {
        for (int i = 0; str[i]; ++i) {
            int x = str[i] - '0';
            if (!i && str[i] == '9') {
                continue;
            }
            if (9 - x < x) {
                str[i] = 9 - x + '0';
            }
        }
        printf("%s\n", str);
    }

    return 0;
}
B. Han Solo and Lazer Gun
一個二維的座標系,某一個點上有機搶手,其他的點上有敵人,每發射一顆炮彈,就可以將整個直線上的所有敵人都消滅,即使在同一個點上有許多敵人。只需要枚舉就行了。記錄那些點上的敵人已經被消滅過了。
判斷三點在不在一條直線上,這裏我使用的是最原始的斜率,注意斜率爲0,和斜率不存在的情況。同樣可以使用三角形的有向面積,有向面積爲0,表明在同一條直線上。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

#include <iostream>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define N 1010

const double esp = 1e-6;

struct Point {
    int x, y;
    Point() {
    }
    Point (int xx, int yy) {
        x = xx;
        y = yy;
    }
    int input(void)
    {
        return scanf("%d%d", &x, &y);
    }
    void print(void)
    {
        printf("%d %d\n", x, y);
    }
}pt[N], gun;

int vis[N];

int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}

int check(Point u, Point v)
{
    int x, y;
    x = u.x;
    y = u.y;
    u.x = x / gcd(x, y);
    u.y = y / gcd(x, y);
    x = v.x;
    y = v.y;
    v.x = x / gcd(x, y);
    v.y = y / gcd(x, y);
    if (u.x == v.x && u.x == 0) {
        return 1;
    }
    if (u.y == v.y && u.y == 0) {
        return 1;
    }
    if (u.x == v.x && u.y == v.y) {
        return 1;
    }
    u.x = -u.x;
    u.y = -u.y;
    if (u.x == v.x && u.y == v.y) {
        return 1;
    }
    return 0;
}

int main(int argc, char *argv[])
{
    int n;
    while (scanf("%d", &n) != EOF) {
        gun.input();
        for (int i = 0; i < n; ++i) {
            pt[i].input();
        }
        memset(vis, 0, sizeof(vis));
        int cnt = 0;
        for (int i = 0; i < n; ++i) {
            if (vis[i]) {
                continue;
            }
            vis[i] = 1;
            int flag = 0;
            for (int j = 0; j < n; ++j) {
                if (i == j || vis[j]) {
                    continue;
                }
                Point x1(pt[i].x - gun.x, pt[i].y - gun.y);
                Point x2(pt[j].x - gun.x, pt[j].y - gun.y);
                if (check(x1, x2)) {
                    vis[j] = 1;
                    //pt[j].print();
                    ++flag;
                }
            }
            //printf("...\n\n");
            ++cnt;
        }
        printf("%d\n", cnt);
    }

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