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

Codeforces Round #617 (Div. 3)

时间:2020-02-16 17:58:52      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:ORC   贪心   version   题目   review   define   max   style   with   

C. Yet Another Walking Robot

每次把走到的点记录下来,如果后续出现了一个点在之前已经出现过了,那么肯定出现了重复走。另外维护下 r-l 要尽可能小

 

#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <math.h>
#include <cstdio>
#include <iomanip>
#include <time.h>
#include <bitset>
 
#define LL long long
#define INF 0x3f3f3f3f
#define ls nod<<1
#define rs (nod<<1)+1
 
using namespace std;
 
const int maxn = 2e5 + 10;
const LL mod = 1e9 + 7;
 
char s[maxn];
int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        cin >> s+1;
        map<pair<int,int>,int > mp;
        mp[make_pair(0,0)] = 1;
        int x = 0,y = 0;
        int r = 1e9,l = 0;
        for (int i = 1;i <= n;i++) {
            if(s[i]==L){
                x--;
            }else if(s[i]==R){
                x++;
            }else if(s[i]==U){
                y++;
            }else {
                y--;
            }
            if (mp.count(make_pair(x,y))) {
                int l1 = mp[make_pair(x,y)];
                int r1 = i;
                if (r1-l1 < r-l) {
                    r = r1;
                    l = l1;
                }
            }
            mp[make_pair(x,y)] = i+1;
        }
        if (l == 0)
            cout << -1 << endl;
        else
            cout << l << " " << r << endl;
    }
    return 0;
}

 

 

D. Fight with Monsters

 

贪心 + 简单数学

#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <math.h>
#include <cstdio>
#include <iomanip>
#include <time.h>
#include <bitset>

#define LL long long
#define INF 0x3f3f3f3f
#define ls nod<<1
#define rs (nod<<1)+1

using namespace std;


const int maxn = 2e5 + 10;

int v[maxn];
int main() {
    int n,a,b,k;
    int cnt = 0;
    cin >> n >> a >> b >> k;
    for (int i = 1;i <= n;i++) {
        cin >> v[i];
        v[i] = v[i] % (a + b);
        if (v[i] == 0)
            v[i] = b;
        else
            v[i] -= a;
    }
    sort(v+1,v+1+n);
    for (int i = 1;i <= n;i++) {
        if (v[i] <= 0) {
            cnt++;
        } else {
            if (v[i] % a == 0) {
                if (k >= v[i] / a) {
                    k -= v[i] / a;
                    cnt++;
                }
            } else {
                if (k >= (v[i] / a) + 1) {
                    k -= (v[i] / a) + 1;
                    cnt++;
                }
            }
        }
    }
    cout << cnt << endl;
    return 0;
}

 

 

E1. String Coloring (easy version)

暴力找上升的序列,然后跑两次就可以了

#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <math.h>
#include <cstdio>
#include <iomanip>
#include <time.h>
#include <bitset>

#define LL long long
#define INF 0x3f3f3f3f
#define ls nod<<1
#define rs (nod<<1)+1

using namespace std;

const int maxn = 2e5 + 10;
const LL mod = 1e9 + 7;

char s[maxn];
int vis[maxn];
int main(){
    memset(vis,-1,sizeof vis);
    int n;
    cin >> n;
    cin >> (s + 1);
    char c = a;
    for(int i = 1;i <= n;++i){
        if(s[i] >= c ) {
            vis[i] = 0;
            c = s[i];
        }
    }
    c = a;
    for(int i = 1;i <= n;++i){
        if(vis[i] == -1){
            if(s[i] >= c) {
                vis[i] = 1;
                c = s[i];
            }
        }
    }
    for(int i = 1;i <= n;++i)
        if(vis[i] == -1) {
            cout << "NO";
            return 0;
        }
    cout << "YES" << endl;
    for(int i = 1;i <= n;++i){
        cout << vis[i];
    }
}

 

E2. String Coloring (hard version)

题目大意:给你一串长度为n的字符串,你可以给每个位置上染上一种不大于n的颜色,对于相邻的两个位置,如果他们的颜色不同则可以交换他们的位置,现在需要交换若干次后按照字典序排序,你需要找到最少满足条件的颜色数并输出方案

可以想到,只有一个字符要与另一个字符交换,才要染成不同颜色,从前往后来考虑,我们只用考虑一个字符与在它前面的字符交换就行了,因为枚举到后面需要与其交换的字符,自然会考虑到它

只有比它大的字符才会跟它交换,所以我们只需要让它与比它大的字符不同颜色即可

然后要想到这样的一个结论,这一些字符的颜色必然在[1,??]这样的一个区间里,而且包含着[1,??]中的每一种颜色,所以我们对于枚举到的字符,找到对应的??,然后把枚举到的字符的颜色设为??+1即可

只有26个字符,暴力就完事了,复杂度??(26??)

 

#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <math.h>
#include <cstdio>
#include <iomanip>
#include <time.h>
#include <bitset>

#define LL long long
#define INF 0x3f3f3f3f
#define ls nod<<1
#define rs (nod<<1)+1

using namespace std;

const int maxn = 2e5 + 10;
const LL mod = 1e9 + 7;

int ans[maxn],maxx[maxn];

int main() {
    int n;
    cin >> n;
    int col = 0;
    for (int i = 1;i <= n;i++) {
        char c;
        cin >> c;
        int k = c - a + 1;
        int mx = 0;
        for (int j = k+1;j <= 26;j++) {
            mx = max(mx,maxx[j]);
        }
        ans[i] = mx + 1;
        col = max(col,ans[i]);
        maxx[k] = mx + 1;
    }
    cout << col << endl;
    for (int i = 1;i <= n;i++)
        cout << ans[i] << " ";
    return 0;
}

 

 

F. Berland Beauty

给一棵树,边权未知,现在给m组约束,每组约束给出从u到v路径中的最小值,现在让你给出一组边权,使得符合之前的约束,不能给出输出-1

思路:

因为n较小,对于每组约束我们可以直接暴力修改路径上的权值,如果边的权值小于当前约束的最小值,则将权值修改,最后再根据每组约束暴力走一遍路径看路径是否满足要求,如果不满足则输出-1,最后还得对那些没有修改过的边随意赋值

#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <math.h>
#include <cstdio>
#include <iomanip>
#include <time.h>
#include <bitset>

#define LL long long
#define INF 0x3f3f3f3f
#define ls nod<<1
#define rs (nod<<1)+1



const int maxn = 5010;
const LL mod = 1e9 + 7;

std::vector<int> graph[maxn];
int id[maxn][maxn];
int fa[maxn],dep[maxn];
int from[maxn],to[maxn],w[maxn];
int val[maxn][maxn];
int cst[maxn];

void dfs(int u,int f) {
    fa[u] = f;
    dep[u] = dep[f] + 1;
    for (int i = 0;i < graph[u].size();i++) {
        int v = graph[u][i];
        if (v == f)
            continue;
        dfs(v,u);
    }
}


void dfs2(int u,int f) {
    for (int i = 0;i < graph[u].size();i++) {
        int v = graph[u][i];
        if (v == f)
            continue;
        cst[id[u][v]] = val[u][v];
        dfs2(v,u);
    }
}


int main() {
    int n;
    std::cin >> n;
    for (int i = 1;i <= n-1;i++) {
        int u,v;
        std::cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
        id[u][v] = id[v][u] = i;
    }
    dfs(1,0);
    int m;
    std::cin >> m;
    for (int i = 1;i <= m;i++) {
        std::cin >> from[i] >> to[i] >> w[i];
        if (dep[from[i]] < dep[to[i]])
            std::swap(from[i],to[i]);
        int u = from[i],v = to[i];
        while (dep[u] != dep[v]) {
            int f = fa[u];
            if (val[u][f] <= w[i])
                val[u][f] = val[f][u] = w[i];
            u = f;
        }
        while (u != v) {
            int fu = fa[u],fv = fa[v];
            if (val[u][fu] <= w[i])
                val[u][fu] = val[fu][u] = w[i];
            if (val[v][fv] <= w[i])
                val[v][fv] = val[fv][v] = w[i];
            u = fu,v = fv;
        }
    }
    for (int i = 1;i <= m;i++) {
        int x = INF;
        int u = from[i],v = to[i];
        if (dep[u] < dep[v])
            std::swap(u,v);
        while (dep[u] != dep[v]) {
            int f = fa[u];
            x = std::min(x,val[u][f]);
            u = f;
        }
        while (u != v) {
            int fu = fa[u],fv = fa[v];
            x = std::min(x,val[u][fu]);
            x = std::min(x,val[v][fv]);
            u = fu,v = fv;
        }
        if (x != w[i]) {
            std::cout << -1 << std::endl;
            return 0;
        }
    }
    dfs2(1,0);
    for (int i = 1;i <= n-1;i++) {
        if (!cst[i])
            std::cout << 1000000  << " ";
        else
            std::cout << cst[i] << " ";
    }
    return 0;
}

 

Codeforces Round #617 (Div. 3)

标签:ORC   贪心   version   题目   review   define   max   style   with   

原文地址:https://www.cnblogs.com/-Ackerman/p/12317790.html

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