思维题 (C~E)

Posted 钟钟终

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了思维题 (C~E)相关的知识,希望对你有一定的参考价值。

https://codeforces.com/contest/1675
C. Detective Task
找第一个0和最后一个1 然后讨论下
稍微卡住我的是没有0全是1得情况,答案为cout<<s.size()-k2<<endl;,最后一个1后面的问好也要算进去,所以不是1.

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()

    int t;cin>>t;
    while(t--)
    
        string s;cin>>s;
        int len=s.length();
        if(len==1)
        
            cout<<1<<endl;continue;
        
        int k1=s.find('0'),k2=s.find('1');
        if(k1==-1&&k2==-1)
        
            cout<<len<<endl;continue;
        
        k1=k2=-1;
        for(int i=0;i<len;i++)
        
            if(s[i]=='0'&&k1==-1)
                k1=i;
            else if(s[i]=='1')
                k2=i;
        
        //cout<<"/"<<k1<<" "<<k2<<endl;
        if(k1!=-1&&k2!=-1)
            cout<<k1-k2+1<<endl;
        else if(k1!=-1&&k2==-1)
            cout<<k1+1<<endl;
        else if(k1==-1&&k2!=-1)
            cout<<s.size()-k2<<endl;
    
    return 0;


D. Vertical Paths
若真要去建图反而会做的很麻烦,深搜遍历记录路径;
只需要从根节点用while遍历上去即可,思路倒是很简单,主要是代码得写法。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5;
int n,p[N],ans[N];
map<int,int>mp;
bool vis[N];
int main()

    int t;cin>>t;
    while(t--)
    
        mp.clear();
        memset(vis,0,sizeof(vis));
        cin>>n;
        for(int i=1;i<=n;i++)
        
            cin>>p[i];
            mp[p[i]]++;
        
        if(n==1)
        
            cout<<1<<endl<<1<<endl<<1<<endl;;continue;
        
        cout<<n-mp.size()<<endl;
        for(int i=1;i<=n;i++)
        
            int g=i;
            if(!mp[g])
            
                int cnt=0;
                while(!vis[g])
                
                    ans[++cnt]=g;vis[g]=1;
                    g=p[g];
                
                cout<<cnt<<endl;
                for(int j=cnt;j>=1;j--)
                    cout<<ans[j]<<" ";
                cout<<endl;
            
        
    
    return 0;


E. Replace With the Previous, Minimize
根据题意将所有相同的字符往前变大一个字符,直接贪心将从字符串开始处选定字符向前递减,每次都对第一个字符操作,直至变为a,在操作下一个字符。

以上是关于思维题 (C~E)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #451 (Div. 2)A,B,C,D,EC题:模拟 D题:尺取+贪心 E题:思维+优先队列维护最值

CODE FESTIVAL 2017 qual A C Palindromic Matrix(思维题)

Codeforces A. Sweet Problem(思维题)

[xsy1140]求值

Codeforces 1137D - Cooperative Game - [交互题+思维题]

Codeforces Round #480 (Div. 2) C 贪心 D 数字思维 E 树上倍增