Ahoi1997 floating flag vijos1097 merging fruit (noip2007)

Source: Internet
Author: User

In order to prevent the feeling from falling, we slowed down the pace of solving the questions of the Changle training...

A few days ago, VJ had been dead for a long time, And rqnoj was set up. The quality of the questions in it was too bad, and the question number on the first page was not continuous !!

In the evening, after VJ is repaired, I have a question. Here are some questions about the ahoi I have been posted... I am also bored with some special questions, so I am too lazy to say...

 

Ahoi flag floating

This is a question similar to the question of arrangement and combination... recursive state

Array f [100] [100] [100] [2]; indicates the number of red flags, the number of yellow flags, the number of color changes, and the color of the flag at the end (0 is yellow, 1 is red)

Then there is how to write the recursive formula:

    for(int k=2;k<=m;k++)       for(int i=1;i<=n;i++)          for(int j=1;j<=n;j++){           for(int l=1;l<=i;l++){          f[i][j][k][0]+=f[i-l][j][k-1][1];           }           for(int l=1;l<=j;l++){          f[i][j][k][1]+=f[i][j-l][k-1][0];           }          }

Let's talk about my own ideas with the for (int l = 1; L <= I; l ++) loop in the loop.

Because it is from the last state pushed down, K-1 this should be no problem, then why the end of the yellow flag is plus the K-1 at the end of the red flag?

In fact, this is also very easy to understand... change the K-1 times, to the end of the Red Flag, change K times, of course, is the end of the yellow flag

What is I-l? Enumeration status... this requires feel.

 

Complete code is attached:

#include<cstdio>#include<iostream>using namespace std;int n,m;int f[100][100][100][2];int main(){    scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++)       for(int j=1;j<=n;j++){            f[i][j][1][0]=f[i][j][1][1]=1;       }    for(int k=2;k<=m;k++)       for(int i=1;i<=n;i++)          for(int j=1;j<=n;j++){           for(int l=1;l<=i;l++){          f[i][j][k][0]+=f[i-l][j][k-1][1];           }           for(int l=1;l<=j;l++){          f[i][j][k][1]+=f[i][j-l][k-1][0];           }          }    cout<<f[n][n][m][1]+f[n][n][m][0];    return 0;}

 

Vijos1097 merge fruit

This question is a bit like a complex question, but it cannot be confused. The requirements of the question are different.

Let's talk about this question... there are also many solutions to this question.

Because I have never played a priority queue before, so I have learned it here.

 

First, the header file # include <queue>

Priority_queue <int> qi; // a normal priority queue, sorted from large to small (default)

Priority_queue <int, vector <int>, greater <int> qi2; // priority queue from small to large

// You can change greater to less, that is, from large to small.

 

There is no difference between calling and queue operations.

But pay attention to the use of Q. Top and Q. Front. Front is not necessarily the top priority.

 

Next, let's look at the following ideas:

Select the minimum two values each time, add them to the queue, and sort them.

 

This idea is a bit similar to greedy thinking... it's easy to prove

Because each merge adds a previous merge to a current pile, the smaller the merged result, the better.

 

Code attached:

 

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<queue>using namespace std;priority_queue<int, vector<int>, greater<int> >q;int n,x,y;long long ans=0;int main(){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&x);q.push(x);}while(!q.empty()){x=q.top();q.pop();if(q.empty()) break; y=q.top();q.pop();q.push(x+y);ans+=(x+y);}cout<<ans;return 0;}

 

Good night ....

 

 

Ahoi1997 floating flag vijos1097 merging fruit (noip2007)

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.