码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #291 (Div. 2)

时间:2015-02-16 01:41:05      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

 

A. Chewbaсca and Number

题意:
Inverting digit t means replacing it with digit 9 - t. Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldnt contain leading zeroes. 分析: It is obvious that all the digits, which are greater than 4, need to be inverted. The only exception is 9, if its the first digit. 时间复杂度:
O(len)
技术分享
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

char s[ 1000 ];

int main()
{
    scanf( "%s", s );
    int i = 0;
    if( s[i] == 9 ) i++;
    for( ; s[i]; ++i )
    {
        char c = 9 - s[i] + 0;
        if( c < s[i] ) 
            s[i] = c;
    }
    puts( s );
    
    return 0;
}
代码君

 

 

B. Han Solo and Lazer Gun

题意:
在一个坐标系上有一个机关枪,能够射穿一条线上的所有敌人,问射死整个坐标系中的敌人,至少需要多少发子弹。
分析:
开始考虑的是计算斜率,但是却用了int型导致WA两次,后来改为double AC了,但是使用这样的斜率是存在无常的。如果数据大一点也许就会出现错误。

这里可以考虑三点共线的条件:
Points (x1, y1), (x2, y2), (x3, y3) are on the same line ,if (x2 - x1)(y3 - y1) = (x3 - x1)(y2 - y1).

或者使用gcd和set来避免精度问题
技术分享
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int sx, sy, n;
double res[ 1010 ];

int main()
{
    scanf( "%d %d %d", &n, &sx, &sy );
    int flag = 0;
    int ct = 0;
    int out = 0;
    for( int i = 0; i < n; ++i )
    {
        int x, y;
        scanf( "%d %d", &x, &y );
        if( x == sx && y == sy ) continue;
        int ex = x - sx;
        int ey = y - sy;
        if( ex == 0 )
        {
            int k = 0x7fffffff;
            res[ ct++ ] = k;
        }
        else if( ey == 0 )
        {
            int k = 0x3f3f3f3f;
            res[ ct++ ] = k;
        }
        else
        {
            double k = (double)ex / (double)ey;
            res[ ct++ ] = k;
        }
    }
    sort( res, res + ct );
    for( int i = 0; i < ct; ++i )
    {
        if( res[i] == res[i+1] ) 
            continue;
        out += 1;
    }
    printf( "%d\n", out );
    return 0;
}
代码君
技术分享
#include <bits/stdc++.h>
using namespace std;

const int N = 1000;
long long x[N];
long long y[N];
bool killed[N];

int main() {
    int n, x0, y0; cin >> n >> x0 >> y0;
    for (int i = 0; i < n; ++i) {
        cin >> x[i] >> y[i];
        x[i] -= x0;
        y[i] -= y0;
    }
    int result = 0;
    for (int i = 0; i < n; ++i) if (!killed[i]) {
        for (int j = 0; j < n; ++j) if (x[i] * y[j] - y[i] * x[j] == 0) killed[j] = true;
        ++result;
    }
    cout << result << endl;
    return 0;
}
参考代码1
技术分享
#include <cstdio>
#include <algorithm>
#define N 1005
#define fi(a, b, c) for(int a = (b); a < (c); a++)
#define fd(a, b, c) for(int a = (b); a > (c); a--)
#define FI(a, b, c) for(int a = (b); a <= (c); a++)
#define FD(a, b, c) for(int a = (b); a >= (c); a--)
#define fe(a, b, c) for(int a = (b); a; a = c[a])
using namespace std;

int n, x, y, a[N], b[N], ans;
bool u[N];

int main(){
    scanf("%d %d %d", &n, &x, &y);
    fi(i, 0, n) scanf("%d %d", &a[i], &b[i]);
    fi(i, 0, n) if(!u[i]){
        ans++;
        fi(j, 0, n) if((a[i] - x) * (b[j] - y) == (a[j] - x) * (b[i] - y)) u[j] = 1;
    }
    printf("%d\n", ans);
    scanf("\n");
}
参考代码2
技术分享
#include <iostream>
#include <vector>
#include <set>

using namespace std;

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

int main() {
    ios::sync_with_stdio(false);

    int N, X0, Y0;
    cin >> N >> X0 >> Y0;

    set< pair<int, int> > S;
    for (int i = 0; i < N; ++i) {
        int X, Y; cin >> X >> Y;
        int dx = X - X0;
        int dy = Y - Y0;
        int d = gcd(dx, dy);
        dx /= d;
        dy /= d;
        if (dy < 0) {
            dy = -dy;
            dx = -dx;
        }
        if (dx == 0)
            dy = 1;
        S.insert(make_pair(dx, dy));
    }

    cout << S.size() << "\n";
}
参考代码3

 

Codeforces Round #291 (Div. 2)

标签:

原文地址:http://www.cnblogs.com/BigBallon/p/4293617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!