読者です 読者をやめる 読者になる 読者になる

JOIJOI

女医

1~4は入出力で前後に変なのくっついてる
1やるだけ

for t in range(1, 6):
    f = open('./2014-yo-t1-in'+str(t)+'.txt', 'r')
    s = 0
    for i in range(5):
        s += max(40, int(f.readline()))
    print(s//5, file=open('out'+str(t)+'.txt', 'w'))

2やるだけ

import sys
for t in range(1, 6):
    f = open('./2014-yo-t2-in'+str(t)+'.txt', 'r')
    n, m = map(int, f.readline().split())
    a = [int(f.readline()) for i in range(n)]
    b = [int(f.readline()) for i in range(m)]
    r = [0 for i in range(n)]
    for d in b:
        for j in range(n):
            if (a[j] <= d):
                r[j] += 1
                break
    m = 0
    res = 0
    for i in range(n):
        if (r[i] > m):
            m = r[i]
            res = i
    print(res+1, file=open('out'+str(t)+'.txt', 'w'))

3死

import sys
for t in range(1, 6):
#    f = sys.stdin
    f = open('./2014-yo-t3-in'+str(t)+'.txt', 'r')
    w, h, n = map(int, f.readline().split())
    X, Y = map(int, f.readline().split())
    r = 0
    for i in range(n-1):
        x, y = map(int, f.readline().split())
        x -= X
        y -= Y
        r += abs(x)+abs(y)
        if x*y > 0:
            r -= min(abs(x), abs(y))
    print(r)
    print(r, file=open('out'+str(t)+'.txt', 'w'))

3正解と思われるもの

import sys
for t in range(1, 6):
#    f = sys.stdin
    f = open('./2014-yo-t3-in'+str(t)+'.txt', 'r')
    w, h, n = map(int, f.readline().split())
    X, Y = map(int, f.readline().split())
    r = 0
    for i in range(n-1):
        x, y = map(int, f.readline().split())
        cx = x-X
        cy = y-Y
        r += abs(cx)+abs(cy)
        if cx*cy > 0:
            r -= min(abs(cx), abs(cy))
        X = x
        Y = y
    print(r)
    print(r, file=open('out'+str(t)+'.txt', 'w'))

4DP

import sys
for t in range(1, 6):
#    f = sys.stdin
    f = open('./2014-yo-t4-in'+str(t)+'.txt', 'r')
    n = int(f.readline())
    s = f.readline()
    r = [[0 for i in range(8)] for i in range(n+1)]
    r[0][1] = 1
    for i in range(1, n+1):
        c = s[i-1]
        if (c == 'J'):
            d = 0
        elif (c == 'O'):
            d = 1
        elif (c == 'I'):
            d = 2
        for j in range(8):
            if not j & (1 << d):
                continue
            for k in range(8):
                if j & k:
                    r[i][j] += r[i-1][k]
            r[i][j] %= 10007
    print(sum(r[n]) % 10007, file=open('out'+str(t)+'.txt', 'w'))

5ダイグストラ

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <queue>
#include <random>

using namespace std;

typedef long long ll;
typedef pair<ll, int> P;

struct edge {int to; ll cost;};
const ll INF = 1000000000;
const int MAX_N = 5000;
vector<edge> G[MAX_N];
ll d[MAX_N];

void dijkstra(int s) {
    priority_queue<P, vector<P>, greater<P>> q;
    fill(d, d+MAX_N, INF);
    d[s] = 0;
    q.push(P(0, s));
    while (!q.empty()) {
        P p = q.top(); q.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                q.push(P(d[e.to], e.to));
            }
        }
    }
}


int n, k;
int c[MAX_N] = {0}, r[MAX_N] = {0};
int root[MAX_N][MAX_N] = {};
int main(int argc, char *argv[]) {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> c[i] >> r[i];
    }
    for (int i = 0; i < k; i++) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        root[x][y] = 1;
        root[y][x] = 1;
    }

    bool used[MAX_N];
    for (int i = 0; i < n; i++) {
        fill(used, used+MAX_N, false);
        used[i] = true;

        queue<P> q;
        q.push(P(i, r[i]));
        while (!q.empty()) {
            P p = q.front(); q.pop();
            if (p.second <= 0) continue;
            for (int j = 0; j < n; j++) {
                if (!used[j] && root[p.first][j]) {
                    q.push(P(j, p.second - 1));
                    edge e; e.to = j; e.cost = c[i];
                    G[i].push_back(e);
                    used[j] = true;
                }
            }
        }
    }
    dijkstra(0);
    printf("%lld\n", d[n-1]);
    return 0;
}