Codeforces Round #707 (Div. 2)A~C题解

写在前边

链接:Codeforces Round #707 (Div. 2)
心态真的越来越不好了,看A没看懂,赛后模拟了一遍就过了,B很简单,但是漏了个判断重复的条件。

A. Alexey and Train

链接:A题链接

题目大意:

不想说了,题目看了半天没看懂,心态又看炸了。

思路

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'
#define pub push_back
#define pob pop_back

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

const int Mod = 1000000007;

LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}

const int N = 110;
int a[N], b[N], tm[N];

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> tm[i];
    }
    int moment = 0;
    for (int i = 1; i <= n; i++) {
        moment = moment + a[i] - b[i - 1] + tm[i];
        if (i == n) break;
        int wait = (b[i] - a[i] + 1) / 2;
        moment += wait;
        if (moment >= b[i]) continue;
        else moment = b[i];
    }

    cout << moment << endl;
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    scanf("%d", &t); 
    while (t--) {
        solve();
    }
    return 0;
}

B. Napoleon Cake

链接:B题链接

题目大意:

就是往n层蛋糕上涂奶油,看最后有哪些层被奶油浸透。

思路

  1. 双指针。
    倒序枚举,枚举到一个涂有奶油的层,那么比它小的ia[i]+1都会被浸透,同时要注意如果遇到一个奶油更多的应该更新一下,比如1 0 0 0 4 3这个数据,枚举到3的时候,我们知道它能将蛋糕变成1 0 0 1 1 1,但是由于它的前边还有一个更厚的奶油,会使得蛋糕变成1 1 1 1 1 1所以应该要判断一下,详细说不清,看代码吧,主要要判断是否越界!
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'
#define pub push_back
#define pob pop_back

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

const int Mod = 1000000007;

LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}

const int N = 2e5 + 10;
int a[N];
bool st[N];

void solve() {
    memset(st, false, sizeof(st));
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }

    for (int i = n; i >= 1; i--) {
        if (a[i] > 0) {
            int j = i;
            int temp = a[i];
            while (temp) {
                st[j] = true;
                temp--;
                j--;
                if (j <= 0) break;
                if (a[j] >= temp) break;
            }
            i = j + 1;
        }
    }

    for (int i = 1; i <= n; i++) {
        if (st[i]) printf("%d ", 1);
        else printf("%d ", 0);
    }
    cout << endl;
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    scanf("%d", &t); 
    while (t--) {
        solve();
    }
    return 0;
}

2.差分
b[i+1], 让b[max(ia[i]+1,1)]++ 也挺巧妙。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'
#define pub push_back
#define pob pop_back

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

const int Mod = 1000000007;

const int N = 2E5 + 10;
int b[N], n, a[N];

void solve() {
    memset(b, 0, sizeof(b));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
        b[i + 1]--;
        b[max(i - a[i] + 1, 1)]++;
    }
    
    for (int i = 1; i <= n; i++) b[i] += b[i - 1];
    for (int i = 1; i <= n; i++) printf("%d ", (b[i] > 1 ? 1 : b[i]));
    puts("");
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    scanf("%d", &t); 
    while (t--) {
        solve();
    }
    return 0;
}

C. Going Home

链接:C题链接

题目大意:

给定n个数,找出四个坐标xyzw使得ax+ay=az+zw

思路

这个题是真的没想到,暴力O(n2)随便过,原理是抽屉原理,比如有8个苹果7个箱子,现在将苹果全部装入箱子,那么至少有一个箱子有两个苹果,而现在给了N个数,那么我们可以有N(N1)/2个对,如果N(N1)2>5,000,000,那么其中必然有和相同的对。一共可以枚举出n(n1)2个数对,但是由于两个数最大之和为5106,我们所能枚举到的和的个数最多也就是5106,我们可以把先枚举到的和存到一个数组里,而最多枚举到5106个数我们就能接着枚举到一个重复的和,而大部分情况下根本不用枚举到5106个数我们就能枚举到一个已经存在的数,那么跳出循环即可,所以复杂度就是O(min(n2,n+c)),做过好几个类似的题了,都是可以通过判断出最大枚举范围然后直接暴力来做的。

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")

using namespace std;

#define Inf 0x3f3f3f3f
#define PII pair<int, int>
#define P2LL pair<long long, long long>
#define endl '\n'
#define pub push_back
#define pob pop_back

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<long long> VLL;
typedef vector<int> VI;

const int Mod = 1000000007;

const int N = 2e5 + 10, M = 5e6 + 10;
int a[N];
PII temp[M];

void solve() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    for (int i = 1; i <= n - 1; i++) {
        for (int j = i + 1; j <= n; j++) {
            int t = a[i] + a[j];
            if (temp[t].first == 0 || temp[t].second == 0) {
                temp[t] = {i, j};
                continue;
            } else {
                if (temp[t].first != i && temp[t].first != j && temp[t].second != i && temp[t].second != j) {
                    puts("YES");
                    printf("%d %d %d %d\n", temp[t].first, temp[t].second, i, j);
                    return;
                }
            }
        }
    }
    
    puts("NO");
}

int main()
{
    //ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    solve();
    return 0;
}
posted @   Xxaj5  阅读(124)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2020-03-16 (Good topic)压缩字符串 (3.16 leetcode每日打卡)
点击右上角即可分享
微信分享提示