Codeforces Round #291 (Div. 2)

Codeforces Round #291 (Div. 2)


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 shouldn't 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 it's 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).
或者使用 gcdset 来避免精度问题

#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;
}

你可能感兴趣的:(codeforces)