Codeforces Intel Code Challenge Final Round (Oct/08/2016)

本文解析了三道算法题目,包括判断平年中特定日期连续出现的可能性、验证矩阵元素是否可通过限定操作达到有序状态,以及计算光线在网格中到达指定位置的时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A
大意就是判断会不会在一个平年中出现相邻的两个月的第一天分别是给定的日期。

判断一下31 mod 7, 30 mod 7, 28 mod 7就好。

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

char ch[100];
int x, y;

int main(void) {
    freopen("1.in", "r", stdin);
    gets(ch);
    if (ch[0] == 'm') x = 1;
    else if (ch[0] == 't' && ch[1] == 'u') x = 2;
    else if (ch[0] == 'w') x = 3;
    else if (ch[0] == 't') x = 4;
    else if (ch[0] == 'f') x = 5;
    else if (ch[0] == 's' && ch[1] == 'a') x = 6;
    else x = 7;
    gets(ch);
    if (ch[0] == 'm') y = 1;
    else if (ch[0] == 't' && ch[1] == 'u') y = 2;
    else if (ch[0] == 'w') y = 3;
    else if (ch[0] == 't') y = 4;
    else if (ch[0] == 'f') y = 5;
    else if (ch[0] == 's' && ch[1] == 'a') y = 6;
    else y = 7;
    y -= x; y = (y + 7) % 7;
    if (y == 0 || y == 2 || y == 3) puts("YES");
    else puts("NO");
} 

B

You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n+1 actions in total. Operations can be performed in any order.

既然最后可以交换两列,那么对于一个排序号的排列枚举交换的位置,一一匹配即可

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int mp[30][30], a[30][30], b[30];
int n, m, cnt;
bool flag = 0;

int main(void) {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            scanf("%d", &mp[i][j]);
    for (int i = 1; i <= m; i++) b[i] = i;
    for (int i = 1; i <= n; i++) {
        cnt = 0;
        for (int j = 1; j <= m; j++)
            cnt += (mp[i][j] != b[j]);
        if (cnt > 2) goto w;
    }
    flag = 1; w:;
    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= m; j++) {
            swap(b[i], b[j]);
            for (int k = 1; k <= n; k++) {
                cnt = 0;
                for (int x = 1; x <= m; x++)
                    cnt += (mp[k][x] != b[x]);
                if (cnt > 2) goto W;
            }
            flag = 1;
            W:swap(b[i], b[j]);
        }
    if (flag) puts("YES");
    else puts("NO");
    return 0;
}

C
给定nm的网格,从(0,0)有一道射线以v=2射出,遇到墙壁反射遵守反射定律。求射线经过(x,y)的时间。

可以把这张网格图拓展成(lcm(n,m),lcm(n,m)),然后发现其实总共四种情况(x,y)(2nx,y)(x,2my)(2nx,2my)
只要预处理出每过m时间的位置即可。其实就是在普通的计算过程中取模。

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;

typedef long long ll;

inline char get(void) {
    static char buf[100000], *p1 = buf, *p2 = buf;
    if (p1 == p2) {
        p2 = (p1 = buf) + fread(buf, 1, 100000, stdin);
        if (p1 == p2) return EOF;
    }
    return *p1++;
}
template<typename T>
inline int read(T &x) {
    static char c; x = 0;
    for (c = get(); c < '0' || c > '9'; c = get());
    for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = get());
    return x;
}
template<typename T>
inline T Min(T a, T b) {
    return a < b ? a : b;
}

const ll N = 100100, INF = 1e16;

ll n, m, k, x, y, pos, Cnt, res;
ll cnt[N << 2];

ll Calculate(ll x, ll y) {
    ll p = ((y - x) % n + n) % n;
    if (cnt[p] == -1) return INF;
    return m * cnt[p] + x;
}

int main(void) {
    read(m); read(n); read(k);
    n <<= 1; m <<= 1;
    memset(cnt, -1, sizeof cnt);
    while (1) {
        if (cnt[pos] != -1) break;
        cnt[pos] = Cnt++;
        pos = (pos + m) % n;
    }
    for (int i = 0; i < k; i++) {
        read(x); read(y); res = INF;
        res = Min(res, Calculate(x, y));
        res = Min(res, Calculate(m - x, y));
        res = Min(res, Calculate(x, n - y));
        res = Min(res, Calculate(m - x, n - y));
        if (res == INF) res = -1;
        printf("%I64d\n", res);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值