码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #656 (Div. 3)

时间:2020-07-18 22:42:22      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:clu   type   ops   lan   计算   push   txt   top   bool   

A

根据性质,\(a,b,c\) 中的最大值一定会在 $x,y,z $ 中出现两次

#include <bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin >> t;
    int a[3];
    while(t --) {
        memset(a,0,sizeof a);
        for(int i = 0;i < 3; ++i) cin >> a[i];
        sort(a,a + 3);
        if(a[1] != a[2]) cout << "NO" << endl;
        else {
            cout << "YES\n" << a[0] << ‘ ‘ << a[0] << ‘ ‘ << a[1] << endl; 
        }
    }
    return 0;
}

C

题意 :去除一个数组的最短前缀使得余下的数组每次从首或尾部取元素可以排为非降序列

每次从首尾取元素,组成非降序列的话,首位肯定是最小的,中间肯定是最大的,

所以,只要从尾部开始找到一组形如 $a_1\leq a_2\leq \dots\leq a_{max}\geq \dots\geq a_{k-1}\geq a_k $ 这样的序列即可

#include <bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin >> t;
    int n;
    while(t --) {
        cin >> n;
        vector<int> a(n);
        int pos = n - 1;
        for(int i = 0;i < n; ++i) cin >> a[i];
        while(pos > 0 && a[pos - 1] >= a[pos]) -- pos;
        while(pos > 0 && a[pos - 1] <= a[pos]) -- pos;
        cout << pos << endl;
    }
    return 0;
}

参考

Kanoon

https://codeforces.com/blog/entry/80257

D

  • string 递归拷贝的时候,要加 const

每次都是对半分字符递增,计算每个半区域有多少不同的字符即可,然后左右两边取 \(min\) 返回

#include <bits/stdc++.h>
using namespace std;
int calc(const string &s,char c) {
    if(s.size() == 1) return s[0] != c;
    int mid = s.size() / 2;
    int cntl = calc(string(s.begin(), s.begin() + mid), c + 1);
    cntl += mid - count(s.begin() + mid,s.end(),c);
    int cntr = calc(string(s.begin() + mid, s.end()), c + 1);
    cntr += mid - count(s.begin(),s.begin() + mid,c);
    return min(cntl,cntr);
}
int main() {
    int t;
    cin >> t;
    while(t --) {
        string s;
        int n;
        cin >> n >> s;
        cout << calc(s,‘a‘) << endl;
    }
    return 0;
}

参考

https://codeforces.com/blog/entry/80257

E

先把有向边连上,跑一遍topsort,在这个过程中,记录每个点的优先级大小,因为无向边没有连,所以他们也会进队跑一遍,

但是不用担心,因为这些无向边都是一组的,不管怎么跑,永远不可能比有向边的权值大,如果最后topsort跑成环,输出 \(NO\)

否则,就按权值大小,小的点连到大的点上即可

技术图片

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef pair<int,int> PII;
int e[N],ne[N],h[N],idx,d[N],n,m,top[N],cnt;
void add(int a,int b) {
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx ++;
}
bool topsort() {
    queue<int> q;
    int t;
    for(int i = 1;i <= n; ++i) {
        if(d[i] == 0) q.push(i);
    }
    while(q.size()) {
        t = q.front();
        top[t] = cnt ++;// 记录每个topsort的优先值大小
        q.pop();
        for(int i = h[t]; ~i;i = ne[i]) {
            int j = e[i];
            d[j] --;
            if(d[j] == 0) q.push(j);
        }
    }
    if(cnt < n) return 0;
    else return 1;
}
int main() {
    // freopen("in.txt","r",stdin);
    int t;
    cin >> t;
    while(t --) {
        idx = 0;
        cnt = 1;
        memset(h,-1,sizeof h);
        memset(e,0,sizeof e);
        memset(ne,0,sizeof ne);
        memset(d,0,sizeof d);
        memset(top,0,sizeof top);
        int op,u,v;
        cin >> n >> m;
        vector<PII> vec;
        for(int i = 0;i < m; ++i) {
            cin >> op >> u >> v;
            vec.push_back({u,v});
            if(op == 1) {
                add(u,v);
                d[v] ++;
            }
        }
        if(topsort() == 0) {
            cout << "NO" << endl;
        }
        else {
            cout << "YES" << endl;
            for(int i = 0;i < m; ++i) {
                int &u = vec[i].first,&v = vec[i].second;
                if(top[u] > top[v]) swap(u,v);
                cout << u << ‘ ‘ << v << endl;
            }
        }
    }
    return 0;
}

参考

Half-Blood Prince

Codeforces Round #656 (Div. 3)

标签:clu   type   ops   lan   计算   push   txt   top   bool   

原文地址:https://www.cnblogs.com/lukelmouse/p/13336745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!