Summer Training 1 Session 3

Violence + Greedy + Simulation + Minimum Spanning Tree

T1

Jxq and ztz play table tennis. The rules of table tennis are as follows: In a game, the side that scores 11 points first is the winner, and after 10 ties, the side that scores 2 more points first is the winner. Now give you the scores of two people in the game, please judge their scores and win or lose. Input format: The input data contains multiple lines. Each line has a character string "jxq" or "ztz", which means jxq gets one point or ztz gets one point; the second line has a character string "jxq" or "ztz", which means winning One side.

Simulate, pay attention to the details. There are two situations to win, 1. After reaching 10, it takes two more goals to win. 2. First 11 points are scored, and the other person does not have a score.

#include<bits/stdc++.h>
using namespace std;
int tnum,onum;
string s;
int main()
{
    
    
    while(cin>>s)
    {
    
    
        if(s=="jxq")tnum++;
        else onum++;
        if((tnum==11&&onum<10)||(tnum>11&&tnum-onum>=2))//先到达11分||到达10平后多2分
        {
    
    
            cout<<onum<<":"<<tnum<<endl;
            cout<<"jxq";
            return 0;
        }
        if((onum==11&&tnum<10)||(onum>11&&onum-tnum>=2))
        {
    
    
            cout<<onum<<":"<<tnum<<endl;
            cout<<"ztz";
            return 0;
        }
    }
}


T2

Tuyang Middle School is off for summer vacation! Roolz has some new plans for this summer vacation. He realized the preciousness of life during the tomb robbery some time ago, and decided to change a down-to-earth way to make money, that is, to become a little postman!
Roasted Lezi was arranged to deliver the letter in a long alley. The direction of this alley is unconventional and you can think of it as a straight line. In order to sleep more in the early morning every day, Roots plans to rent a place in this alley, so that he can get up as late as possible while finishing the work before the specified time. There are n houses on this alley from left to right. The distance between house B and the first house is Di, so that Di is different from each other, that is, no two houses are in the same position. Roast Lezi needs to choose one of these n houses to live in.
Every morning, every house in this alley will receive a letter, and Roast needs to send a letter to each of them. Please also note that Baking Lezi is rather stupid. He only brings all the letters from one house every time he sends a letter. If he wants to send it away, he needs to go back to his home and bring the letter before sending it.
Roasted Lezi set out from his residence and returned to his residence after delivery.
As your little friend of Roa Lezi, please tell Roa Lezi how he should choose his residence so that the sum of the distances he needs to travel each morning to send the letter is the smallest, and find this minimum sum of distances.

Choose greedy, choose the middle place as the starting point, distance and minimum. Then use the middle loop to find the values ​​on both sides

#include<bits/stdc++.h>
using namespace std;
unsigned long long a[150000];
int n;
int main()
{
    
    
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    long long a1=0,a2=0;
    for(int i=1;i<=n/2;i++)a1+=a[n/2+1]-a[i];//到左边
    for(int i=n/2+2;i<=n;i++)a2+=a[i]-a[n/2+1];//到右边
    cout<<(a1+a2)*2;
    return 0;
}


T3

The fantasy world where Ava cat lives has several fantasy kingdoms, and each fantasy kingdom has exactly two fantasy cities. These cities have just been built. So the road in it is not perfect, there was not a road in the beginning.
The fantasy world is divided into north and south parts by a long fantasy river, in which there are n fantasy kingdoms. Each fantasy kingdom has a city on the north and south, but their relative positions may not be simply arranged in order. Specifically, in the north of the fantasy world, there are n cities from west to east, which belong to the fantasy kingdom A1, A2...An. In the south of the fantasy world, there are n cities from west to east, which belong to the fantasy kingdom B1, B2...Bn. According to the above regulations, it is obvious that A and B are both a permutation from 1 to n. Because the number of each kingdom will and will only appear once in the north and south.
With a wave of his hand, Aka, the ruler of the fantasy world, built a road between the two cities in each fantasy kingdom. These built roads may intersect, and the intersecting will bring about friendly exchanges between the two kingdoms. If the roads of two kingdoms intersect, we call the two kingdoms friendly.
Now Ava wants to know how many kingdoms she can find if she wants to find some kingdoms among all n fantasy kingdoms so that every two of these kingdoms are friendly. That is, she wants to know, when the kingdom set S satisfies the kingdoms represented by any two elements are friendly, what is the maximum size of the set S

If you want to avoid crossing, then the abscissa 1 and abscissa 2 of the point must be on its left, so use an array to store it backwards, and then use an array to indicate the position of b in a, and then enumerate You can join if the location meets the requirements

#include<bits/stdc++.h>
using namespace std;
#define maxn 150000
int n,a[maxn],b[maxn],c[maxn],v[maxn],f[maxn];
int main() 
{
    
    
     
    cin>>n;
     
    for(int i=1;i<=n;i++)cin>>a[i],v[a[i]]=i;
    for(int i=1;i<=n;i++)cin>>b[n-i+1],c[n-i+1]=v[b[n-i+1]];//存入位置
     
    int len=1;
    f[1]=c[1];
    for(int i=2;i<=n;++i) 
    {
    
    
        if(c[i]>f[len])f[++len]=c[i];//加入
        else
        {
    
    
            for(int j=1;j<=len;j++)
            if(f[j]>c[i])
            {
    
    
                f[j]=c[i];
                break;
            }
        }
    }
    cout<<len;
    return 0;
}


T4

Roast Lezi got tired of the messenger and decided to raise pigs again.
Roolz was sent to a village specialized in raising pigs to learn pig raising skills. The village is currently improving its facilities. The transportation of pig food originally carried out manually is now being changed to pipeline transportation, but the cost of building a new pipeline is relatively high. Has been hesitant.
Roa Lezi is an enthusiastic and good boy. He came to the village chief's home to discuss this matter with the village chief.
The village chief told Roast Lezi that the pig farms in the village can build several pig food production centers, but the construction cost of each pig food production center is very high, and each requires P units of gold coins. But at the same time, some pipelines can be built between every two pig farms. The price of constructing each pipeline may be different. A pig farm must have pig food supply, so if it is not a pig food production center, It must be possible to reach at least one pig food production center through some pipelines.
Roasted Lezi decided to help the village chief calculate the minimum number of gold coins required

Consider building a pig food production center at the n+1th point. The distance from each point to the pig food production center is the cost of construction. This satisfies that there must be a pig food production center. If you run the minimum spanning tree, you can calculate The cost of Unicom n+1 points

#include<bits/stdc++.h>
using namespace std;
int a[550][550],mins[550],vis[550];
int n,m;
int main()
{
    
    
    cin>>n>>m;
    memset(mins,0x7f,sizeof(mins));
     
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    cin>>a[i][j];
     
    for(int i=1;i<=n;i++)//建设n+1的点
    a[i][n+1]=a[n+1][i]=m;
  
    n++;
    mins[1]=0;
    for(int i=1;i<=n;i++)//prim跑最小生成树
    {
    
    
        int k=0;
        for(int j=1;j<=n;j++)
        if(vis[j]==0&&(mins[j]<mins[k]))k=j;
        vis[k]=1;
        for(int j=1;j<=n;j++)
        if(vis[j]==0&&(mins[j]>a[k][j]))
        mins[j]=a[k][j];
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    ans+=mins[i];
    cout<<ans;
    return 0;
 } 

Guess you like

Origin blog.csdn.net/yhhy666/article/details/108227987