zoukankan      html  css  js  c++  java
  • CF/div2c/贪心

    题目链接【http://codeforces.com/contest/749/problem/C】

    题意:给出一个长度为n序列包含D和R,每一轮操作的先后顺序是1—n,规则是每一轮每个人有一次机会杀掉一个人,人死不能复生。问到最后剩下的人是D类人还是R类人。

    思路:贪心+模拟。如果某个人有机会杀人,要杀掉还有机会杀人的不同类人,如果没有就杀掉位子靠前的已经用过杀人机会的不同类人。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=200050;
    char s[maxn];
    int n;
    set<int>sr,sd;
    int main ()
    {
        while(~scanf("%d",&n))
        {
            sr.clear(); sd.clear();
            scanf("%s",s+1);
            for(int i = 1;i <= n;i++)
            {
                if(s[i] == 'R')
                    sr.insert(i);
                else
                    sd.insert(i);
            }
            while(sd.size()&&sr.size())
            {
                set<int>::iterator r = sr.begin();
                set<int>::iterator d = sd.begin();
                while(r != sr.end() && d != sd.end())
                {
                    if(*r > *d)
                    {
                        set< int > :: iterator  t= r;
                        r++;    d++;
                        sr.erase(*t);
                    }
                    else
                    {
                        set<int>::iterator t=d;
                        d++;    r++;
                        sd.erase(*t);
                    }
                }
                while(r!=sr.end())
                {
                    if(sd.size())
                    {
                        sd.erase(*sd.begin());
                        r++;
                    }
                    else    break;
                }
                while(d!=sd.end())
                {
                    if(sr.size())
                    {
                        sr.erase(*sr.begin());
                        d++;
                    }
                    else    break;
                }
            }
            if(sr.size())
                printf("R
    ");
            else
                printf("D
    ");
        }
        return 0;
    }
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int maxn = 200050;
    queue<int>qud,qur;
    char s[maxn];
    int n;
    int main ()
    {
        while(~scanf("%d",&n))
        {
            while(!qud.empty()) qud.pop();
            while(!qur.empty()) qur.pop();
            scanf("%s",s+1);
            for(int i=1;i<=n;i++)
            {
                if(s[i]=='R')
                    qur.push(i);
                else
                    qud.push(i);
            }
            while(!qur.empty()&&!qud.empty())
            {
                int r=qur.front();qur.pop();
                int d=qud.front();qud.pop();
                if(r>d)
                    qud.push(d+n);
                else
                    qur.push(r+n);
            }
            if(qur.empty())
                printf("D
    ");
            else
                printf("R
    ");
        }
        return 0;
    
    }
    想的太多,做的太少。
  • 相关阅读:
    文件管理
    字符编码
    字典练习,统计字符串中单词出现次数
    字典有关练习:购物车
    列表及列表操作方法
    字符串及用法
    变量,程序交互,基本数据类型
    /usr/bin/ld: i386:x86-64 architecture of input file `command.o' is incompatible with i386 output
    混合云存储系统开发总结
    小记6月27
  • 原文地址:https://www.cnblogs.com/pealicx/p/6207555.html
Copyright © 2011-2022 走看看