首页 > 其他分享 >Codeforces Round 859 (Div. 4)

Codeforces Round 859 (Div. 4)

时间:2023-03-20 20:36:19浏览次数:44  
标签:ch cout cin int Codeforces long 859 while Div

A. Plus or Minus

#include <bits/stdc++.h>

using namespace std;

int32_t main() {
    ios::sync_with_stdio(false) , cin.tie(nullptr) , cout.tie(nullptr);
    int t;
    cin >> t;
    while( t-- )
    {
        int a, b, c;
        cin >> a >> b >> c;
        if (a + b == c) cout << "+\n";
        else cout << "-\n";
    }
    return 0;
}

B. Grab the Candies

#include <bits/stdc++.h>

using namespace std;

void solve(){
    int n;
    cin >> n;
    int a = 0 , b = 0;
    for( int x ; n ; n -- ){
        cin >> x;
        if( x & 1 ) b += x;
        else a += x;
    }
    if( a > b ) cout << "YES\n";
    else cout << "NO\n";
}

int32_t main() {
    ios::sync_with_stdio(false) , cin.tie(nullptr) , cout.tie(nullptr);
    int t; cin >> t;
    while( t -- )
        solve();
    return 0;
}

C. Find and Replace

把奇数位置的字符放入一个集合,把偶数位置的字符放入另一个集合,然后判断两个集合是否相交

#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n;
    string s;
    cin >> n >> s;
    set<char> st;
    for (int i = 0; i < n; i += 2)
        st.insert(s[i]);

    for (int i = 1; i < n; i += 2) {
        if (st.find(s[i]) != st.end()) {
            cout << "NO\n";
            return;
        }
    }
    cout << "YES\n";
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}

D. Odd Queries

前缀和

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve(){
    int n , q ;
    cin >> n >> q;
    vector<int> a(n+1);
    for( int i = 1 ; i <= n ; i ++ ) cin >> a[i];
    for( int i = 1 ; i <= n ; i ++ ) a[i] += a[i-1];
    for( int l , r , k , t; q ; q -- ){
        cin >> l >> r >> k;
        t = a[n] - a[r] + a[l-1] + ( l + r - 1 ) * k;
        if( t  & 1 ) cout << "YES\n";
        else cout << "NO\n";
    }
}

int32_t main() {
    ios::sync_with_stdio(false) , cin.tie(nullptr) , cout.tie(nullptr);
    int t;
    cin >> t;
    while( t -- )
        solve();
    return 0;
}

E. Interview

这题其实不算难,就是交互题,二分判断一下就好。

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    int n, q;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i], a[i] += a[i - 1];
    int l = 1 , r = n , mid , res , t;
    while( l <= r ){
        mid = ( l + r ) >> 1;
        cout << "? " << ( mid - l + 1 ) << " ";
        for( int i = l ; i <= mid ; i ++ ) cout << i << " ";
        cout << endl;
        cin >> t;
        if( t == a[mid] - a[l-1] ) l = mid + 1;
        else r = mid - 1 , res = mid;
    }
    cout << "! " << res<< endl;
    return ;
}

int32_t main() {
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

F. Bouncy Ball

同时维护点的坐标和方向向量,如果横坐标到边界就反向vx,如果纵坐标到边界就反向vy

然后每次都计算出在当前方向上最多可以移动多远不用改变方向,然后一步移动过去。注意在移动前要判断目标点是否在移动的路径上。

#include <bits/stdc++.h>

using namespace std;

#define int long long

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

void solve() {
    int n = read(), m = read(), sx = read(), sy = read(), tx = read(), ty = read();
    string s;
    cin >> s;
    int vx, vy, cnt = 0, f, p, q, dx, dy;
    if (s[0] == 'D') vx = 1;
    else vx = -1;
    if (s[1] == 'R') vy = 1;
    else vy = -1;
    set<tuple<int, int, int, int> > st;
    while (sx != tx || sy != ty) {
        if (st.count(make_tuple(sx, sy, vx, vy)) != 0) {
            cout << "-1\n";
            return;
        }
        st.emplace(sx, sy, vx, vy);
        f = 0;
        if (sx == 1 && vx == -1) vx = 1, f = 1;
        if (sx == n && vx == 1) vx = -1, f = 1;
        if (sy == 1 && vy == -1) vy = 1, f = 1;
        if (sy == m && vy == 1) vy = -1, f = 1;

        if (vx == 1) p = n - sx;
        else p = sx - 1;
        if (vy == 1) q = m - sy;
        else q = sy - 1;
        p = min(p, q);
        cnt += f;
        dx = vx * abs(sx - tx), dy = vy * abs(sy - ty);
        if ( abs(dx) == abs(dy) && tx - sx == dx && ty - sy == dy) break;
        sx += vx * p, sy += vy * p;
    }
    printf("%lld\n", cnt);

}

int32_t main() {
    int t = read();
    while (t--)
        solve();
    return 0;
}

G. Subsequence Addition

G1,G2其实我只写了一遍。简单来说就是把数组排序,然后判断每一个数是否小于他的前缀之和。注意特判第一个数是不是 1。证明没有,但是我暴力推了长度为 5 的所有情况,发现是复合的。

#include <bits/stdc++.h>

using namespace std;

#define int long long

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

void solve() {
    int n = read();
    vector<int> c(n);
    for (auto &i: c) i = read();
    sort(c.begin(), c.end());
    if (c[0] != 1) {
        cout << "NO\n";
        return;
    }
    for( int i = 1 , sum = 1 ; i < n ; i ++ ){
        if( c[i] > sum ){
            cout << "NO\n";
            return;
        }
        sum += c[i];
    }
    cout << "YES\n";
    return;
}

int32_t main() {
    int t = read();
    while (t--)
        solve();
    return 0;
}

标签:ch,cout,cin,int,Codeforces,long,859,while,Div
From: https://www.cnblogs.com/PHarr/p/17237610.html

相关文章

  • CF 1368B Codeforces Subsequences
    题目地址题意:给你一个数n,构造一个字符串,使得至少有n个子串为codeforcesSolution用贪心的思想肯定是只在codeforces基础上修改对于每个字符,对答案的贡献都是乘以字符的......
  • CF855 Div3 VP 游记
    比赛链接好长时间不写博文了甚至快忘记了(今天水一发Div3游记,在Div4比赛之前。第一次VP,当然得选一个简单点的了,打了50分钟多一点。排名不错,400多。$T1$:开始时......
  • Codeforces Round 858 (Div. 2)
    Preface这场CF打的好难受啊,A刚开始忘记切换语言了CE了两发,B漏了一种情况挂了一发,C纯靠暴力找性质,D比赛时没人写我题目都没仔细看然后E本来秒出了解法,结果就是因为unorder......
  • Educational Codeforces Round 115 (Rated for Div. 2)(D,E)
    EducationalCodeforcesRound115(RatedforDiv.2)(D,E)DD题目给出\(n\)个问题,每个问题都会有一个主题和一个难度,并且没有两个题目的问题和主题都是一样的,我们需要选......
  • Codeforces 87D Beautiful Road
    Codeforces87DBeautifulRoadCF传送门Description​ 给定一个无向图,\(n\)个点,\(n-1\)条边,保证图联通(就是一棵树),并且给定每条边的权值。任取两个点,连接二者的路径上......
  • div + css命名规则
    页头:header登录条:loginBar标志:logo侧栏:sideBar广告:banner导航:nav子导航:subNav菜单:menu子菜单:subMenu搜索:search滚动:scroll页面......
  • Practical Diversified Recommendations on YouTube with Determinantal Point Proces
    目录概MotivationDPPWilhelmM.,RamanathanA.,BonomoA.,JainS.,ChiE.H.andGillenwaterJ.Practicaldiversifiedrecommendationsonyoutubewithdetermin......
  • Codeforces Round 350 (Div. 2) ABCD1D2
    https://codeforces.com/contest/670A.Holidays题目大意:给定n天,求出可以休息的最大小时间和最大时间。input14output44#include<bits/stdc++.h>usingnamesp......
  • Codeforces Round 851 (Div. 2) (CF1788) 题解
    CF1788AOneandTwo对于一个序列,题目要求蓝色部分的乘积等于绿色部分的乘积,因为序列中只有\(1\)和\(2\),所以我们只要蓝色部分和绿色部分的\(2\)的数量相等即可,使用......
  • Codeforces Round 406 (Div. 1) C. Till I Collapse 主席树上二分
    首先贪心是显然的,但是询问有n个先考虑一个朴素的主席树做法对于每个k,对于当前固定的L,二分R,用主席树查询一下[L,R]区间内的不同数个数是否大于K个(主席树的经典应用),更新......