Educational Codeforces Round 104(A-E题解)

本文详细解析了EducationalCodeforcesRound104的五道编程竞赛题目,包括思路和解决方案。A题通过查找最小值出现次数求解;B题通过观察规律推导公式得出答案;C题根据不同奇偶性构造比赛结果;D题运用数论方法解决勾股数问题;E题利用优先队列解决分层图的最优化问题。文章展示了如何在有限时间内高效解决各种类型的算法问题。

我昨天打了一下Educational Codeforces Round 104的比赛,场内做出了A,B,C,D,E五题,这场的C,D比以往简单。

A. Arena

思路:直接找最小值出现的次数即可。

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
int _;
int n;
int a[110];

void solve(){
    cin >> _;
    while(_--){
        cin >> n;
        int minn = 1e9;
        for(int i = 1; i <= n; i++){
            cin >> a[i];
            minn = min(minn, a[i]);
        }
        int ans = 0;
        for(int i = 1; i <= n; i++){
            if(a[i] == minn){
                ans++;
            }
        }
        cout << n - ans << "\n";
    }
    return ;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    solve();
    return 0;
}

B. Cat Cycle

思路:刚开始被卡了一下,找规律,发现n偶数时候,不会冲突。发现n = 3,会在2,3,4,5,,,,,冲突。n = 5,会在3, 5, 7, ,,,冲突。n = 7,会在4,7,10,,冲突,根据规律推公式即可。

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
int _;
ll n, k;

void solve(){
    cin >> _;
    while(_--){
        cin >> n >> k;
        if(n % 2 == 0){
            cout << 1 + (k - 1) % n << "\n";
        }
        else{
            cout << 1 + (k - 1 + ((k >= (n/2+1)) ? (1 + (k - (n/2+1))/(n/2)) : (0)))%n << "\n";
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    solve();
    return 0;
}

C. Minimum Ties

思路:构造题。n是奇数,不需要平局,一个人参加n-1场,一半胜利,一半失败。n是偶数,一个人参加n-1场,n-1此时是奇数,所有要有一场平局。

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
int _;
int n;

void solve(){
    cin >> _;
    while(_--){
        cin >> n;
        if(n % 2){
            int now = n - 1;
            int tim = now / 2;
            for(int i = 1; i <= n-1; i++){
                for(int j = 1; j <= now; j++){
                    if(j <= tim){
                        cout << "1" << " ";
                    }
                    else{
                        cout << "-1" << " ";
                    }
                }
                now--;
            }
        } 
        else{
            int now = n - 1;
            int tim = now / 2;
            for(int i = 1; i <= n-1; i++){
                for(int j = 1; j <= now; j++){
                    if(j <= tim){
                        cout << "1" << " ";
                    }
                    else if(j == tim + 1){
                        cout << "0" << " ";
                    }
                    else{
                        cout << "-1" << " ";
                    }
                }
                now--;
            }
        }
        cout << "\n";
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    solve();
    return 0;
}

D. Pythagorean Triples

思路:数论题。这个数论比上一场的C简单多了。1<=a,b,c<=n1<=a, b, c <= n1<=a,b,c<=n范围内找出a2+b2=c2&&a2−b=ca^2+b^2=c^2 \&\&a^2-b=ca2+b2=c2&&a2b=c的情况个数。
我们先消除a2a^2a2,得:
c2−b2=c+b c^2 - b^2 = c+b c2b2=c+b
c2−c=b2+b c^2-c=b^2+b c2c=b2+b
c∗(c−1)=b(b+1) c*(c-1) = b(b+1) c(c1)=b(b+1)
得出:
c=b+1 c = b+1 c=b+1
可得出:
a2=2∗b+1 a^2=2*b+1 a2=2b+1
我们找一下a,b,ca, b, ca,b,c三个数的范围:
1<=a<=n 1 <= a <= n 1<=a<=n
1<=b<=n 1 <= b <= n 1<=b<=n
1<=c<=n 1 <= c <= n 1<=c<=n
1<=b+1<=n 1 <= b+1 <= n 1<=b+1<=n
所以:
1<=b<=n−1 1 <= b <= n-1 1<=b<=n1
此时:
3<=a2<=2∗n−1 3 <= a^2 <= 2*n - 1 3<=a2<=2n1
3<=a<=2∗n−1 \sqrt{3} <= a <= \sqrt{2*n-1} 3<=a<=2n1
因为a是整数,我们要找出在[3,2∗n−1][\sqrt{3}, \sqrt{2*n-1}][3,2n1]中,找出(a2+1)mod2==0(a^2+1)mod2==0(a2+1)mod2==0的数的个数,就能得到答案。我们可以通过预处理,前缀和的方式,使得每一个询问复杂度达到O(1)O(1)O(1)

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
int _;
const int N = 100010;
ll sum[100010];
ll a[100010];
ll n;

void init(){
    memset(a, 0, sizeof a);
    memset(sum, 0, sizeof sum);
    for(ll i = 2; i <= 100000; i++){
        if(((i*i)-1)%2 == 0){
            a[i] = 1ll;
        }
    }
    for(int i = 1; i <= 100002; i++){
        sum[i] = sum[i-1] + a[i];
    }
}

void solve(){
    cin >> _;
    while(_--){
        cin >> n;
        ll k = sqrt(2ll*n-1ll);
        cout << sum[k] << "\n";
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    init();
    solve();
    return 0;
}

E. Cheap Dinner

思路:这是一个分层图,我们考虑dp[i][j]表示第i层选第j个的最小花费。第i层对j进行规划的时候,要从上一层和j不冲突的dp中选一个最小值,考虑把dp结合优先队列。

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
int _;
ll dp[5][200010];
int n1, n2, n3, n4;
ll a[200010];
ll b[200010];
ll c[200010];
ll d[200010];
set<int> vec[5][200010];
struct cmp{
    bool operator () (const pair<ll, int> &a, const pair<ll, int> &b) const{
        return a.first > b.first;
    }
};
priority_queue<pair<ll, int>, vector< pair<ll, int> >, cmp> q;

void solve(){
    cin >> n1 >> n2 >> n3 >> n4;
    for(int i = 1; i <= n1; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n2; i++){
        cin >> b[i];
    }
    for(int i = 1; i <= n3; i++){
        cin >> c[i];
    }
    for(int i = 1; i <= n4; i++){
        cin >> d[i];
    }
    int m1;
    cin >> m1;
    for(int i = 1; i <= m1; i++){
        int x, y;
        cin >> x >> y;
        vec[2][y].insert(x);
    }
    int m2;
    cin >> m2;
    for(int i = 1; i <= m2; i++){
        int x, y;
        cin >> x >> y;
        vec[3][y].insert(x);
    }
    int m3;
    cin >> m3;
    for(int i = 1; i <= m3; i++){
        int x, y;
        cin >> x >> y;
        vec[4][y].insert(x);
    }
    //cout << m1 << m2 << m3 << "\n";
    memset(dp, 0x3f, sizeof dp);
    for(int i = 1; i <= n1; i++){
        dp[1][i] = a[i];
    }
    for(int tt = 2; tt <= 4; tt++){
        //cout << tt << "\n";
        if(tt == 2){
            while(q.size()){
                q.pop();
            }
            for(int i = 1; i <= n1; i++){
                q.push({dp[1][i], i});
            }
            //cout << q.size() << "\n";
        }   
        else if(tt == 3){
            while(q.size()){
                q.pop();
            }
            for(int i = 1; i <= n2; i++){
                q.push({dp[2][i], i});
            }
        }
        else{
            while(q.size()){
                q.pop();
            }
            for(int i = 1; i <= n3; i++){
                q.push({dp[3][i], i});
            }
        }
        vector< pair<ll, int> > tmp;
        if(tt == 2){
            for(int i = 1; i <= n2; i++){
                tmp.clear();
                while(q.size() != 0 && vec[tt][i].find((q.top().second)) != vec[tt][i].end()){
                    pair<ll, int> now = q.top();
                    q.pop();
                    tmp.push_back(now);
                }
                if(q.size() != 0){
                    pair<ll, int> now = q.top();
                    dp[tt][i] = min(dp[tt][i], now.first + b[i]);
                }
                for(int j = 0; j < tmp.size(); j++){
                    q.push(tmp[j]);
                }
            }
        }
        else if(tt == 3){
            for(int i = 1; i <= n3; i++){
                tmp.clear();
                while(q.size() != 0 &&vec[tt][i].find((q.top().second)) != vec[tt][i].end()){
                    pair<ll, int> now = q.top();
                    q.pop();
                    tmp.push_back(now);
                }
                if(q.size() != 0){
                    pair<ll, int> now = q.top();
                    dp[tt][i] = min(dp[tt][i], now.first + c[i]);
                }
                for(int j = 0; j < tmp.size(); j++){
                    q.push(tmp[j]);
                }
            }
        }
        else{
            for(int i = 1; i <= n4; i++){
                tmp.clear();
                while(q.size() != 0 &&vec[tt][i].find((q.top().second)) != vec[tt][i].end()){
                    pair<ll, int> now = q.top();
                    q.pop();
                    tmp.push_back(now);
                }
                if(q.size() != 0){
                    pair<ll, int> now = q.top();
                    dp[tt][i] = min(dp[tt][i], now.first + d[i]);
                }
                for(int j = 0; j < tmp.size(); j++){
                    q.push(tmp[j]);
                }
            }
        }

    }
    ll ans = 0x3f3f3f3f3f3f3f3f;
    for(int i = 1; i <= n4; i++){
        ans = min(ans, dp[4][i]);
    }
    if(ans >= 0x3f3f3f3f3f3f3f3f){
        cout << "-1" << "\n";
    }
    else{
        cout << ans << "\n";
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    solve();
    return 0;
}
目前关于 Codeforces Educational Round 179 的题解和比赛信息尚未在提供的引用中出现。根据 Codeforces 的常规更新频率以及比赛安排,Educational Rounds 通常会在比赛结束后不久发布官方题解,并且社区中也会有大量用户分享他们的解法和思路。 以下是一个通用的查找方法以及可能的题目类型解析: ### 查找方法 1. **访问 Codeforces 官方网站**:直接前往 Codeforces 的比赛页面,搜索 "Educational Round 179",查看是否已经有官方题解发布。 2. **参考社区资源**:如 AtCoder、TopCoder 或其他 OJ 平台上的用户题解,或者在社交媒体(如 Reddit、Stack Overflow)上查找相关讨论。 3. **使用搜索引擎**:输入关键词如 "Codeforces Educational Round 179 Editorial" 或 "Codeforces Educational Round 179 Solutions",查找博客、论坛等资源。 ### 可能的题目类型及解法示例 根据以往的 Educational Rounds 特点,以下是一些可能的题目类型及其常见解法: #### 1. **字符串处理** - **题目描述**:给定一个字符串,要求判断其是否满足某些条件或进行特定操作。 - **解法**: ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; // 示例:判断字符串是否为回文 bool is_palindrome = true; for (int i = 0; i < s.size() / 2; ++i) { if (s[i] != s[s.size() - i - 1]) { is_palindrome = false; break; } } cout << (is_palindrome ? "YES" : "NO") << endl; return 0; } ``` #### 2. **数学问题** - **题目描述**:涉及数论、组合数学或简单代数问题。 - **解法**: ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; // 示例:判断 n 是否可以被 k 整除 cout << (n % k == 0 ? "YES" : "NO") << endl; return 0; } ``` #### 3. **贪心算法** - **题目描述**:通过局部最优解构造全局最优解。 - **解法**: ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int &x : a) cin >> x; sort(a.begin(), a.end()); // 示例:选择最大的元素 cout << a[n - 1] << endl; return 0; } ``` #### 4. **动态规划** - **题目描述**:需要通过状态转移方程解决的问题。 - **解法**: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1005; int dp[MAXN][MAXN]; int main() { int n; cin >> n; vector<int> a(n); for (int &x : a) cin >> x; // 初始化 for (int i = 0; i <= n; ++i) dp[i][i] = 0; // 状态转移 for (int len = 2; len <= n; ++len) { for (int i = 0; i + len - 1 < n; ++i) { int j = i + len - 1; dp[i][j] = max(a[i] - dp[i + 1][j], a[j] - dp[i][j - 1]); } } cout << dp[0][n - 1] << endl; return 0; } ``` ###
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值