SSブログ

WUPC [コンテスト]

あまりにもひどい成績だったので自信無くしかけです.
勉強したことが全く生かせていないのです. 萎え萎え.

終わった後に一応全部解きました. 拡張ダイクストラもぐもぐ.
A.
#include <stdio.h>

int toDay(int month, int date)
{
    int tbl[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int i;
    int ret;
    
    ret = 0;
    
    for (i = 0; i < month - 1; i++){
        ret += tbl[i];
    }
    
    ret += date;
    
    return (ret);
}

int main(void)
{
    int Ma, Da, Mb, Db;
    
    scanf("%d%d", &Ma, &Da);
    scanf("%d%d", &Mb, &Db);
    
    printf("%d\n", toDay(Mb, Db) - toDay(Ma, Da));
    
    return (0);
}


B. N^2全探索最初からしていればよいものを...
辞書順2つを出力するとWAです.反例探すのは少し難しいです.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct {
    char word[101];
} String;

int comp(const void *a, const void *b)
{
    String x, y;
    
    x = *(String *)a;
    y = *(String *)b;
    
    return (strcmp(x.word, y.word));
}

int main(void)
{
    int N;
    int i, j, k;
    String data[50], list[2500];
    
    scanf("%d", &N);
    
    for (i = 0; i < N; i++){
        scanf("%s", data[i].word);
    }
    
    k = 0;
    for (i = 0; i < N; i++){
        for (j = 0; j < N; j++){
            if (i != j){
                strcpy(list[k].word, data[i].word);
                strcat(list[k++].word, data[j].word);
            }
        }
    }
    
    qsort(list, k, sizeof(String), comp);
    
    printf("%s\n", list[0].word);
    
    return (0);
}


C. fflush(stdin);を使うとREとWAになりました. なぜ. (やめたらACになりました. かなしみ)
#include <stdio.h>
#include <string.h>

#define QS (1000000)

char map[512][512];
char vis[512][512];

int tx[QS], ty[QS], time[QS];
int head, tail;

void enq(int x, int y, int t)
{
    tx[head] = x, ty[head] = y, time[head] = t;
    head++;
    head %= QS;
}

void deq(int *x, int *y, int *t)
{
    *x = tx[tail], *y = ty[tail], *t = time[tail];
    tail++;
    tail %= QS;
}

int bfs(int ax, int ay, int bx, int by)
{
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    int nx, ny, nt;
    int i;
    
    head = tail = 0;
    memset(vis, 0, sizeof(vis));
    vis[ay][ax] = 1;
    
    enq(ax, ay, 0);
    
    while (head != tail){
        deq(&nx, &ny, &nt);
        
        if (nx == bx && ny == by){
            return (nt);
        }
        
        for (i = 0; i < 4; i++){
            if (map[ny + dy[i]][nx + dx[i]] != '#' && !vis[ny + dy[i]][nx + dx[i]]){
                vis[ny + dy[i]][nx + dx[i]] = 1;
                enq(nx + dx[i], ny + dy[i], nt + 1);
            }
        }
    }
    
    return (-1);
}

int main(void)
{
    int i, j;
    int temp, ans;
    int N, M;
    int sx, sy, cx, cy, gx, gy;
    
    scanf("%d%d", &N, &M);
    
    for (i = 0; i < N; i++){
        scanf("%s", map[i]);
        for (j = 0; j < M; j++){
            if (map[i][j] == 'S'){
                sx = j, sy = i;
            }
            if (map[i][j] == 'C'){
                cx = j, cy = i;
            }
            if (map[i][j] == 'G'){
                gx = j, gy = i;
            }
        }
    }
    
    temp = bfs(sx, sy, cx, cy);
    
    if (temp == -1){
        printf("-1\n");
        return (0);
    }
    ans = temp;
    
    temp = bfs(cx, cy, gx, gy);
    
    if (temp == -1){
        printf("-1\n");
        return (0);
    }
    
    printf("%d\n", ans + temp);
    
    return (0);
}


D. 典型的なDPでした. いつかのIOIの問題だったり, いつかのPCKの問題だったりします.
#include <stdio.h>
#include <string.h>

int path[100][100];
int sum[100][100];
int i, j;
int n;
int ans;

int main(void)
{
    scanf("%d", &n);
    
    for (i = 0; i < n; i++){
        for (j = 0; j <= i; j++){
            scanf("%d", &path[i][j]);
            
            if (i == 0 && j == 0){
                sum[i][j] = path[i][j];
                break;
            }
            
            if (j == 0 || sum[i - 1][j] > sum[i - 1][j - 1]){
                sum[i][j] = path[i][j] + sum[i - 1][j];
            }
            else {
                sum[i][j] = path[i][j] + sum[i - 1][j - 1];
            }
        }
        if (i == n - 1){
            for (j = 0; j < n; j++){
                if (sum[i][j] > ans){
                    ans = sum[i][j];
                }
            }
        }
    }
    
    printf("%d\n", ans);
    
    return (0);
}


E. 拡張ダイクストラもぐもぐ. 幅優先チックですネ.
#include <cstdio>
#include <queue>
#include <vector>

#define NODE (1000)
using namespace std;

typedef __int64 ll;
typedef struct {
    int to;
    ll cost;
} Graph;

bool operator < (const Graph &a, const Graph &b){
    return (a.cost > b.cost);
}

vector<Graph> vertex[NODE];

int main(void)
{
    int N, M;
    int p, q, r;
    
    scanf("%d%d", &N, &M);
    
    for (int i = 0; i < M; i++){
        Graph temp;
        scanf("%d%d%d", &p, &q, &r);
        temp.to = p;
        temp.cost = r;
        vertex[q].push_back(temp);
        temp.to = q;
        vertex[p].push_back(temp);
    }
    
    priority_queue<Graph> que;
    static bool v[NODE][4][7];
    
    Graph temp;
    temp.cost = 0;
    temp.to = 0;
    
    que.push(temp);
    
    while (!que.empty()){
        Graph temp = que.top(); que.pop();
        if (v[temp.to][(int)(temp.cost % 4)][(int)(temp.cost % 7)]){
            continue;
        }
        else {
            v[temp.to][(int)(temp.cost % 4)][(int)(temp.cost % 7)] = 1;
        }
        
        if (temp.to == N - 1 && (temp.cost % 4 == 0 || temp.cost % 7 == 0)){
            printf("%I64d\n", temp.cost);
            break;
        }
        
        if (temp.to != N - 1){
            for (int i = 0; i < vertex[temp.to].size(); i++){
                Graph add;
                add.to = vertex[temp.to][i].to;
                add.cost = vertex[temp.to][i].cost + temp.cost;
                que.push(add);
            }
        }
    }
    
    return (0);
}


F. 累積和とって頑張ります. (そんなにがんばりません(謎))
(N * (N + 1) / 2) <= 10^8でさすがにやばいのではと思いましたが ACしました. 5secだし大丈夫らしいです.
#include <cstdio>
#include <algorithm>

typedef struct {
    int x, y;
} Point;

using namespace std;

int main(void)
{
    static int axis[1024][1024];
    static bool exist[1024][1024];
    static Point data[10000];
    int N;
    int ans;
    
    scanf("%d", &N);
    
    for (int i = 0; i < N; i++){
        scanf("%d%d", &data[i].x, &data[i].y);
        axis[data[i].y][data[i].x] = 1;
        exist[data[i].y][data[i].x] = 1;
    }
    
    for (int i = 0; i < 1000; i++){
        for (int j = 0; j < 1000; j++){
            if (i > 0){
                axis[i][j] += axis[i - 1][j];
            }
            if (j > 0){
                axis[i][j] += axis[i][j - 1];
            }
            if (i > 0 && j > 0){
                axis[i][j] -= axis[i - 1][j - 1];
            }
        }
    }
    
    ans = 0;
    for (int i = 0; i < N; i++){
        for (int j = i + 1; j < N; j++){
            Point s, e;
            s.x = min(data[i].x, data[j].x), s.y = min(data[i].y, data[j].y);
            e.x = max(data[i].x, data[j].x), e.y = max(data[i].y, data[j].y);
            
            if (exist[s.y][s.x] && exist[e.y][s.x] && exist[s.y][e.x] && exist[e.y][e.x]){
                int all = axis[e.y - 1][e.x - 1] - axis[e.y - 1][s.x] - axis[s.y][e.x - 1] + axis[s.y][s.x];
                if (all == 0){
                    ans = max(ans, (e.y - s.y) * (e.x - s.x));
                }
            }
        }
    }
    
    printf("%d\n", ans);
    
    return (0);
}

nice!(0)  コメント(0)  トラックバック(0) 

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。