Codeforces Round #455 (Div. 2)-A,B,C,D,E

Keywords: less Python Programming

http://codeforces.com/contest/909
It's a F problem. I'll try to make it up again if I have a chance. F is a question of thought.
It has been patched off and on for a long time.
The game was A and B.
A
Give you two names. Ask a name to be made up of these two prefixes in the order of splicing and the dictionary order is the smallest.
To turn the problem into a minimum length, the last name should be initials only.
Then, in the first name, look for the smaller letters than the first letters of the two names.

#include <bits/stdc++.h>
using namespace std;
int main()
{  string a,b;
   cin>>a>>b;
   string w;
   w+=a[0];
   for(int i=1;i<a.length();i++){
      if(a[i]<b[0])
        w+=a[i];
        else break;
   }
   w+=b[0];
   cout<<w<<endl;
    return 0;
}

B Given a length of n, find all lines whose length between 1 and N is less than or equal to n, and how many lines can be added up.
The data range is 100. You know what it means when you look at the picture. Violence

#include <bits/stdc++.h>
using namespace std;
int sum[200];
int main()
{    int m;
      scanf("%d",&m);
      for(int i=1;i<=m;i++){
          for(int j=i;j<=m;j++){
              for(int k=i;k<=j;k++)
                 sum[k]++;
          }
      }
      int max1=-1;
      for(int i=1;i<=m;i++)
         max1=max(sum[i],max1);
         cout<<max1<<endl;





    return 0;
}

c python method is good.
Give two letters. s is a normal statement and f is a circular statement. Ask how many indentations are there.
The initial idea is to count the length of f strings and then fast power qwq
It's a bit mysterious. I've forgotten why I wrote this now.
Positive solution: dp, when f, the state transition equation is
dp[i][j]=dp[i-1][j-1]; / / with line i, the state when line I is indented to J.
If there is f, indent and add 1. Strict indentation.
When s.
Large indentation moves toward small indentation (the larger the indentation, the greater the distance from the left).
Find the suffix sum again.

    #include <bits/stdc++.h>
using namespace std;
/* Dynamic programming problem.
   One thing to consider is that our final conclusion must be sentence bounded.
   For F statements, just take the indentation matrices of the previous line.
   For the S statement, we just sum it.

*/
const int maxn=5006;
int dp[maxn][maxn];
typedef long long ll;
const ll mod=1e9+7;
int m;
char ch;
int main()
{   scanf("%d",&m);
    getchar();
    dp[0][0]=1;
    for(int i=1;i<=m;i++){
        //scanf(" %c",&ch);
        ch=getchar();
        if(ch=='f'){
            for(int j=0;j<=i-1;j++){
              dp[i][j+1]=dp[i-1][j];
            }
        }
        else{
           ll sum=0;
           for(int j=m;j>=0;j--){
              sum=(sum+dp[i-1][j])%mod;
              dp[i][j]=sum;
           }
        }
        getchar();
    }
    //cin>>ch;
    /*for(int i=0;i<=m;i++){
       for(int j=0;j<=m;j++){
           printf("%d ",dp[i][j]);
       }
       cout<<endl;
    }
    /*for(int i=m;i>=0;i--){
      if(dp[m][i]!=0){
         printf("%lld\n",dp[m][i]);
         break;
      }
    }*/
    printf("%d\n",dp[m][0]);
    return 0;
}

D: Given a string, if the position of the first string and its neighbors do not want to wait, delete them. One does not want to wait for two deletions, and the other two do not want to wait for three deletions.
Thought: If violence is really a bit. What do you do with Question D?
And I found that list does not delete the O (1) function of the node. Does the secret channel write a circular list for itself? Finally, I didn't write it. It shouldn't feel like this.
Maintain a vector. Represents each paragraph. Then violence.

 #include <bits/stdc++.h>
using namespace std;
/* Come on. For the future
  Violence,
    I don't miss you.
*/
vector<pair<char,int> >v;
vector<pair<char,int> >s;
string ss;
int main(){
     cin>>ss;
    long long ans=0;
      v.push_back(make_pair(ss[0],1));
     for(int i=1;i<ss.length();i++){
         //if(v.size()==0)
           //v.push_back(make_pair(ss[i],1));
          if(ss[i]==v[v.size()-1].first)
         {  v[v.size()-1].second++;
         }
         else
            v.push_back(make_pair(ss[i],1));
     }
     /*for(int i=0;i<v.size();i++){
       cout<<v[i].first<<" "<<v[i].second<<"::";
     }*/
     //cout<<endl;
      s.clear();
      while(v.size()>1){
           s.clear();
           for(int i=0;i<v.size();i++){
              if(i==0||i==(v.size()-1))
                 v[i].second-=1;
              else
                 v[i].second-=2;
           }
           ans++;
           int aa=0;
           while(v[aa].second<=0&&aa<v.size()) aa++;
           //This loop is re worked without AA < v. size (). For example, ababababa. For the first time, this cycle will not stop.
           s.push_back(v[aa]);
           for(int i=aa+1;i<v.size();i++){
               if(v[i].second<=0){
                 continue;
               }
               else{
                    //if(s.size()==0) s.push_back(v[i]);
                  if(v[i].first==s[s.size()-1].first){
                   s[s.size()-1].second+=v[i].second;
                 }
                 else
                  s.push_back(v[i]);
               }
           }
           /*for(int i=0;i<s.size();i++){
       cout<<s[i].first<<" "<<s[i].second<<"::";
     }
     cout<<endl;
     */
          v=s;
      }
       printf("%lld\n",ans);
    return 0;
}          

E: Given a digraph (forward trend graph?) It represents the dependencies of each task, and a-connected b represents b-dependent a. When A is completed, b can complete.
Some tasks can only be done on the main processor, others can only be done on the coprocessor. When a task relies on tasks that are completed or all on one processor, we put them on one processor.
Ask how many coprocessors you need at least.
One is a violence-like method, which records the number of events on which each task depends, then stores a main processor queue and a coprocessor queue, and loops the bfs.

#include <bits/stdc++.h>

using namespace std;

int main()
{
int n,mm;
cin>>n>>mm;
vector<int>typ(n);
vector<vector<int> >g(n);
for(int i=0;i<n;++i) cin>>typ[i];
vector<int>cnt(n,0);
for(int i=0;i<mm;++i)
{
    int x1,x2;
    cin>>x1>>x2;
    g[x1].push_back(x2);
    cnt[x2]++;
}
queue<int> c,m;
for(int i=0;i<n;++i)
{
    if(cnt[i]==0)
    {
        if(typ[i]==0) m.push(i);
        else c.push(i);
    }
}
//cout<<c.size()<<' '<<m.size()<<'\n';
int ans=0;
while(!c.empty()||!m.empty())
{
    while(!m.empty())
    {
        int x=m.front();
        for(int i=0;i<g[x].size();++i)
        {
            cnt[g[x][i]]--;
            if(cnt[g[x][i]]==0)
            {
                if(typ[g[x][i]]==0) m.push(g[x][i]);
                else c.push(g[x][i]);
            }
        }
        m.pop();
    }
    if(!c.empty()) ++ans;
    while(!c.empty())
    {
        int x=c.front();
        for(int i=0;i<g[x].size();++i)
        {
            cnt[g[x][i]]--;
            if(cnt[g[x][i]]==0)
            {
                if(typ[g[x][i]]==0) m.push(g[x][i]);
                else c.push(g[x][i]);
            }
        }
        c.pop();
    }
}
cout<<ans;
return 0;
}

Another version

#include <bits/stdc++.h>
using namespace std;
/* 1 Their recursive relationships can be found.
    U For roots, v is dependent (U - > v has one side).
    Then T (U) = max (T (s) +S, T (u);
    When u is on the coprocessor and V is on the main processor. S is 1.
     Otherwise, S is 0..
   2 Write in topsort.
     Actually, it's the recursive relationship.
   PS: I wrote a topsort before. But I link all the leaf nodes together.
   Because I feel that even if there is no adjacency, it may be on a main processor.
   And I use the equality of u and V to judge the number of coprocessors. That would add up.
   But it destroys the original neighbourliness. Example 1 is not right.
     Six situations were analyzed:
     1 U,V They are coprocessors and do not increase. (You can put it on a coprocessor)
     2 u For the sake of harmony, v is the main factor, plus 1
     3 u For co-operation, V part is co-operation, and V part must be on a co-processor.
     4 u Mainly, v is a consortium, not added. v together
     4 u v is the main factor, but not the main factor. v together
     6 u v is the main association. v together
   Then maintain the largest T.
   Note that there are multiple roots. Maintain them by memory.
Not memorizing will lead to T.
*/
const int maxn=1e5+200;
int t[maxn];
vector<int>g[maxn];
int typ[maxn];
int du[maxn];
bool vis[maxn];
void dfs(int u,int pre){
     int sum=0;
     //cout<<u<<" "<<g[u].size()<<" "<<t[u]<<endl;
     if(!g[u].size()&&typ[u]==1&&!vis[u])
        {t[u]++;
        //cout<<u<<"zuobiao"<<endl;
        vis[u]=true;
        return;}
        vis[u]=true;
     for(int i=0;i<g[u].size();i++){
         if(g[u][i]==pre) continue;
         int to=g[u][i];
         if(!vis[to])
             dfs(to,u);
         if(typ[to]==0&&typ[u]==1)
       t[u]=max(t[u],t[to]+1);
         else
         t[u]=max(t[u],t[to]);
     }
      //else if(sum<g[u].size()&&typ[u]==1)
        //t[u]=1;
       return;
}
int main()
{    int m,n,a,b;
     scanf("%d%d",&m,&n);
     for(int i=0;i<m;i++){
         scanf("%d",&typ[i]);
     }
     for(int i=0;i<n;i++){
         scanf("%d%d",&a,&b);
         g[a].push_back(b);
         du[b]++;
     }
     int aim=0;
     for(int i=0;i<m;i++){
         if(!du[i]){
         aim=i;
         dfs(i,-1);
         //break;
         }
     }
     int maxx=-1;
     for(int i=0;i<m;i++){
       maxx=max(maxx,t[i]);
       //cout<<t[i]<<" ";
       }
     printf("%d\n",maxx);
    return 0;
}

Posted by djw821 on Sat, 22 Dec 2018 13:33:06 -0800