Codeforces Round #154 (Div. 2)

A题水题

B题:

二分

View Code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m;
int a[100005];
int main()
{
    int i, j;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    while( ~scanf("%d", &n))
    {
        for(i = 0; i < n; i++)
            scanf("%d", &a[i]);
        sort(a, a+n);
        int ans = 0;
        for(i = 0; i < n; i++)
        {
            j = upper_bound(a, a+n, a[i]*2) - a - i;
        //    printf("%d\n", j);
            if(ans < j) ans = j;
        }
        printf("%d\n", n - ans);
    }
    return 0;
}

单调队列(or two points)

View Code
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int t, ans, n;
int a[100004];
queue <int> q;
int main()
{
    int i, j;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    while( ~scanf("%d", &n))
    {
        for(i = 0; i < n; i++)
            scanf("%d", &a[i]);
        sort(a, a+n);
        for(i = 0; i < n; i++)
        {
            q.push(a[i]);
            while(q.front() * 2 < q.back()) q.pop();
            if(ans < q.size()) ans = q.size();
        }
        printf("%d\n", n - ans);
    }
    return 0;
}

C题:

bfs

View Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n;
int a[105];
bool vis[103][100005];
struct node
{
    int x, y, c;
    friend bool operator < (node a, node b)
    {
        return a.c > b.c;
    }
}s, t, u, v;

queue <node> q;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void bfs() 
{
    int i, j;
    s.c = 0;
    q.push(s);
    vis[s.x][s.y] = 1;
    while(!q.empty())
    {
        u = q.front(); q.pop();
        if(u.x == t.x && u.y == t.y)
        {
            printf("%d\n", u.c);
            return;
        }
        for(i = 0; i < 4; i++)
        {
            v = u;
            v.x += dir[i][0];
            v.y += dir[i][1];
            if(v.y > a[v.x]) v.y = a[v.x]+1;
            if(v.x <= 0 || v.x > n || v.y <= 0 || v.y > a[v.x]+1 || vis[v.x][v.y]) continue;
            vis[v.x][v.y] = 1;    
            v.c++;
            //printf("haha1 %d %d %d\n", v.x, v.y, v.c);
            q.push(v);
        }
    }
    printf("-1\n");
}
int main()
{
    int i, j;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    while( ~scanf("%d", &n))
    {
        for(i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        scanf("%d%d%d%d", &s.x, &s.y, &t.x, &t.
            y);
        bfs();
    }
    return 0;
}

O(n)暴力

View Code
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int Abs(int x) { return x > 0 ? x : -x; }
int min(int a, int b) { return a < b ? a : b; }
int a[103], sx, sy, tx, ty;
int main() 
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int i, j, n, tmp, ans;
    while( ~scanf("%d", &n))
    {
        for(i = 1; i <= n ; i++)
            scanf("%d", &a[i]), a[i]++;
        scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
        if(sx <= tx)
        {
        tmp = sy, ans = 1<<29;
        for(i = sx; i <= tx; i++)
            tmp = min(tmp, a[i]);
        ans = min(ans, tx-sx+Abs(tmp - ty));
        int t = tmp;
        for(i = sx-1; i >= 1; i--)
        {
            t = min(t, a[i]);
            ans = min(ans, tx-sx+Abs(t - ty) + 2*(sx-i));
        }
        t = tmp;
        for(i = tx+1; i <= n; i++)
        {
            t = min(t, a[i]);
            ans = min(ans, tx-sx+Abs(t - ty) + 2*(i-tx));
        }
        }
        else
        {
        tmp = sy, ans = 1<<29;
        for(i = tx; i <= sx; i++)
            tmp = min(tmp, a[i]);
        ans = min(ans, sx-tx+Abs(tmp - ty));
        int t = tmp;
        for(i = tx-1; i >= 1; i--)
        {
            t = min(t, a[i]);
            ans = min(ans, sx-tx+Abs(t - ty) + 2*(tx-i));
        }
        t = tmp;
        for(i = sx+1; i <= n; i++)
        {
            t = min(t, a[i]);
            ans = min(ans, sx-tx+Abs(t - ty) + 2*(i-sx));
        }
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

posted @ 2012-12-09 17:48  To be an ACMan  Views(208)  Comments(0)    收藏  举报