Codeforces #395 Div2

本文解析了五道涉及数学计算、数组操作、图遍历、几何问题及算法优化的编程题目,提供了完整的代码实现和思路说明。

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

A. Taymyr is calling you

Accepted

#include<iostream>
using namespace std;

int gcd(int a, int b) {
    if(!b) return a;
    else return gcd(b, a % b);
}

int main() {
    int n, m, z;
    cin >> n >> m >> z;
    int t = n*m/gcd(n, m);
    cout << z / t << endl;
    return 0;
}

B. Timofey and cubes

Accepted

#include<iostream>
using namespace std;
int main()
{
    int n,i,t;
    cin >> n;
    int a[n+1];
    for(i=1;i<=n;i++)
        cin >> a[i];
    for(i=1;i<=n/2;i++)
    {
        if(i%2==1)
        {
            t=a[i];
            a[i]=a[n+1-i];
            a[n+1-i]=t;
        }
    }
    for(i=1;i<=n;i++)
        cout << a[i] <<' ';
}

C. Timofey and a tree

Accepted

思路:遍历所有连着不同颜色的点的边,若这些边有一个公共结点,则为YES,否则为NO。

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

const int maxn = 100000 + 10;
struct Node {
    int v1, v2;
    Node(int x = -1, int y = -1): v1(x), v2(y) {}
};

vector<Node> edge;
int n, c[maxn], f[maxn];

int main() {
    cin >> n;
    int s, t, cnt = 0;
    for(int i = 0; i < n-1; i++) {
        cin >> s >> t;
        edge.push_back(Node(s-1, t-1));
    }
    for(int i = 0; i < n; i++) cin >> c[i];

    memset(f, 0, sizeof(f));
    for(int i = 0; i < n-1; i++) {
        Node& u = edge[i];
        if(c[u.v1] == c[u.v2]) continue;
        cnt++;
        f[u.v1]++; f[u.v2]++;
        //if(f[u.v1] != cnt && f[u.v2] != cnt) { cout << "NO" << endl; return 0; }
    }
    for(int i = 0; i < n; i++)
        if(f[i] == cnt) { cout << "YES" << endl << i+1 << endl; return 0; }
    cout << "NO" << endl;
    return 0;
}

D. Timofey and rectangles

Accepted
思路:以右下方点的坐标(x, y)代表矩形,如果两个矩阵接触,那么x和x1, y和y1的奇偶不能同时相同,因为各个边的长度是奇数。取contrapositive, 奇偶性都相同,那么一定不接触。所以只要保证每种奇偶性一种颜色即可。奇奇、偶偶、奇偶、偶奇,四种颜色刚好够。此问题必然有解!

#include<iostream>
using namespace std;

int main() {
    int n, x0, y0, x1, y1;
    cin >> n;
    cout << "YES" << endl;
    for(int i = 0; i < n; i++) {
        cin >> x0 >> y0 >> x1 >> y1;
        cout << 2*abs(x0 % 2) + abs(y0 % 2) + 1 << endl;
    }
    return 0;
}

E. Timofey and remoduling

Wrong answer on test 8

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,start=1,i,val[2],cnt[2],loc[2];
    unsigned long long int m;
    cin >> m >> n;
    unsigned long long int a[n+1],d[n+1];
    for(i=1;i<=n;i++)
        cin >> a[i];
    sort(a+1,a+n+1);
    for(i=1;i<=n-1;i++)
        d[i]=a[i+1]-a[i];
    d[n]=(m+a[1]-a[n])%m;

    val[0]=d[1];
    val[1]=-1;
    cnt[0]=1;
    cnt[1]=0;
    loc[0]=1;
    loc[1]=0;
    for(i=2;i<=n;i++)
    {
        if(d[i]==val[0])
            cnt[0]++;
        else if(val[1]==-1)
                {
                    val[1]=d[i];
                    cnt[1]++;
                    loc[1]=i;
                }
                else if(val[1]==d[i])
                        cnt[1]++;
                        else
                        {
                            val[0]=-1;
                            break;
                        }
    }
    if(val[0]==-1)
        cout <<-1;
    else if(cnt[0]>1&&cnt[1]>1)
            cout <<-1;
        else if(cnt[0]>1||cnt[1]==0) cout << a[1+loc[1]%n] << ' ' << val[0];
                else cout << a[1+loc[0]%n ]<< ' ' << val[1];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值