Codeforces Round #303 (Div. 2)

Source: Internet
Author: User
Tags acos bitset

Codeforces Round #303 (Div. 2)

A. Simple question

/*************************************** * *********************************> File Name: a. cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: wednesday ******************************** **************************************** /# include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                Using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
               
                 PLL; int mat [110] [110]; bool OK [110]; int main () {int n; while (cin> n) {for (int I = 1; I <= n; ++ I) {OK [I] = 1; for (int j = 1; j <= n; ++ j) {cin> mat [I] [j] ;}}for (int I = 1; I <= n; ++ I) {for (int j = 1; j <= n; ++ j) {if (mat [I] [j] =-1 | mat [I] [j] = 0) {continue ;} if (mat [I] [j] = 1) {OK [I] = 0;} else if (mat [I] [j] = 2) {OK [j] = 0;} else {OK [I] = OK [j] = 0 ;}} vector
                
                  Ans; ans. clear (); for (int I = 1; I <= n; ++ I) {if (OK [I]) {ans. push_back (I) ;}} int size = ans. size (); printf ("% d \ n", size); if (size = 0) {continue;} printf ("% d", ans [0]); for (int I = 1; I <size; ++ I) {printf ("% d", ans [I]) ;}printf ("\ n ");} return 0 ;}
                
               
              
             
            
          
         
        
       
      
     
    
   
  

B. Simple question

/*************************************** * *********************************> File Name: b. cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: on Wednesday, May 20, 2015, ******************************** **************************************** /# include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                Using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
               
                 PLL; string s, t; int main () {while (cin> s> t) {int len = s. length (); int cnt = 0; for (int I = 0; I <len; ++ I) {if (s [I]! = T [I]) {++ cnt ;}}if (cnt & 1) {printf ("impossible \ n") ;}else {bool flag = 0; for (int I = 0; I <len; ++ I) {if (s [I] = t [I]) {printf ("% c ", s [I]);} else {if (! Flag) {printf ("% c", s [I]);} else {printf ("% c", t [I]);} flag ^ = 1 ;}} printf ("\ n") ;}} return 0 ;}
               
              
             
            
          
         
        
       
      
     
    
   
  

C. Simple dp, Dp [I] [0/1/2] Indicates not to cut down the I-th tree; cut down, down to the left; cut, down to the right

/*************************************** * *********************************> File Name: c. cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: on Wednesday 10, May 20, 2015 ******************************** **************************************** /# include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                Using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
               
                 PLL; PLL sta [100100]; int dp [100100] [3]; int main () {int n; while (~ Scanf ("% d", & n) {memset (dp, 0, sizeof (dp); for (int I = 1; I <= n; ++ I) {scanf ("% d", & sta [I]. first, & sta [I]. second);} dp [1] [1] = 1; if (sta [1]. first + sta [1]. second <sta [2]. first) {dp [1] [2] = 1 ;}for (int I = 2; I <= n; ++ I) {dp [I] [0] = max (dp [I-1] [1], dp [I-1] [2]); dp [I] [0] = max (dp [I-1] [0], dp [I] [0]); if (sta [I]. first-sta [I]. second> sta [I-1]. first) {dp [I] [1] = max (dp [I] [1], max (dp [I-1] [1], dp [I-1] [0]) + 1);} if (sta [I]. first-sta [I]. second> sta [I-1]. first + sta [I-1]. second) {dp [I] [1] = max (dp [I] [1], dp [I-1] [2] + 1 );} if (I <n & sta [I]. first + sta [I]. second <sta [I + 1]. first | I = n) {dp [I] [2] = max (dp [I] [2], max (dp [I-1] [1], dp [I-1] [0]) + 1); if (sta [I-1]. first + sta [I-1]. second <sta [I]. first) {dp [I] [2] = max (dp [I] [2], dp [I-1] [2] + 1 );}}} printf ("% d \ n", max (dp [n] [0], max (dp [n] [1], dp [n] [2]);} return 0 ;}
               
              
             
            
          
         
        
       
      
     
    
   
  

D. Just sort it with greed.

/*************************************** * *********************************> File Name: d. cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: wednesday, 38 seconds ******************************** **************************************** /# include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                Using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
               
                 PLL; int arr [100010]; int main () {int n; while (~ Scanf ("% d", & n) {for (int I = 1; I <= n; ++ I) {scanf ("% d ", & arr [I]);} sort (arr + 1, arr + n + 1); int sum = 0; int ans = 0; for (int I = 1; I <= n; ++ I) {if (sum <= arr [I]) {++ ans; sum + = arr [I] ;}} printf ("% d \ n", ans);} return 0 ;}
               
              
             
            
          
         
        
       
      
     
    
   
  

E. The implementation process of dijkstra algorithm is the process of finding the Minimal Path Tree.
Add a vertex to the set that finds the shortest path.
Consider finding the shortest side for each vertex to reach its edge, and removing the edge having a smaller weight when the shortest path is used, wa hasn't been changed in a place. It's also a dog.

/*************************************** * *********************************> File Name: e. cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: on Wednesday ******************************** **************************************** /# include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                Using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
               
                 PLL; static const int N = 300100; LL d [N]; bool flag [N]; bool vis [N]; int head [N]; int cost [N]; int tot; int use [N]; vector
                
                  Ans; priority_queue <PLL, vector
                 
                   , Greater
                  
                    > Qu; struct node {int w; int id; int nxt; int to;} edge [N <1]; void addedge (int u, int v, int w, int id) {edge [tot]. to = v; edge [tot]. w = w; edge [tot]. nxt = head [u]; edge [tot]. id = id; head [u] = tot ++;} void Dijkstra (int s) {d [s] = 0; cost [s] = 0; memset (flag, 0, sizeof (flag); memset (vis, 0, sizeof (vis); while (! Qu. empty () {qu. pop ();} qu. push (make_pair (d [s], s); while (! Qu. empty () {PLL tmp = qu. top (); qu. pop (); int u = tmp. second; LL dist = tmp. first; if (flag [u]) {continue;} flag [u] = 1; vis [use [u] = 1; for (int I = head [u]; ~ I; I = edge [I]. nxt) {int v = edge [I]. to; int w = edge [I]. w; int id = edge [I]. id; if (d [v]> dist + w) {use [v] = id; d [v] = dist + w; cost [v] = w; qu. push (make_pair (d [v], v);} else if (d [v] = dist + w & cost [v]> w) {cost [v] = w; use [v] = id ;}}} int main () {int n, m; scanf ("% d ", & n, & m); memset (head,-1, sizeof (head); memset (vis, 0, sizeof (vis); for (int I = 1; I <= n; ++ I) {d [I] = (1LL <60); cost [I] = d [I];} tot = 0; int u, v, w, s; for (int I = 1; I <= m; ++ I) {scanf ("% d", & u, & v, & w); addedge (u, v, w, i); addedge (v, u, w, I);} scanf ("% d", & s); Dijkstra (s); LL sum = 0; for (int I = 1; I <= n; ++ I) {for (int j = head [I]; ~ J; j = edge [j]. nxt) {if (vis [edge [j]. id]) {sum + = edge [j]. w ;}}} cout <sum/2 <endl; if (! Sum) {return 0 ;}for (int I = 1; I <= m; ++ I) {if (vis [I]) {printf ("% d ", i) ;}} printf ("\ n"); return 0 ;}
                  
                 
                
               
              
             
            
          
         
        
       
      
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.