Training Resume 2.7

Posted by partypete on Mon, 07 Feb 2022 18:53:22 +0100

Question A: Gifts

Time Limit: 1.000 Sec Memory Limit: 512 MB
Submit state

Title Description

In an n × N on the grid, m gifts are placed, each with a value vi(1 < i < m), you can choose a gift, and then choose:
(1) Take away all gifts listed with it, or (2) Take away all gifts that go with it
What is the maximum value of the gift you can get?

input

The first row has two positive integers n,m.
Then m rows, three integers xi,yi,vi in each row, indicate that the ith gift is on the XIth row and on the grid in Yi column (different gifts may be in the same grid), and that the value is vi.

output

An integer representing the sum of the greatest gift values available.

Sample input Copy

6 7 
1 3 1 
2 2 2 
2 4 4 
3 3 6 
3 6 3 
5 2 5 
5 4 6

Sample Output Copy

11

Tips

Example 1 Interpretation

Select any gift on line 5 and finish line 5.
[Data Range]
For 40% of the data, 1 < n,m < 100
For 70% of the data, 1 < n,m < 1000, 0 < VI < 10000
For 100% of the data, 1 < n < 1000, 1 < m < 105, 1 < xi,yi < n, vi < 106

I calculated the question directly in rows and columns and should have a simpler way than size

But it's still wrong once because of the range of the data.

#include<bits/stdc++.h>
using namespace std;
struct maap
{
    int x;
    int y;
    int v;
};
int main()
{
    int n,m;
    cin>>n>>m;
    maap a[m];
    long long x[n]={0},y[n]={0};
    for(int i=0;i<m;i++)
    {
        cin>>a[i].x>>a[i].y>>a[i].v;
    }
    for(int i=0;i<m;i++)
    {
        x[a[i].x]+=a[i].v;
        y[a[i].y]+=a[i].v;
    }
    sort(x,x+n);
    sort(y,y+n);
    long long maxx=max(x[n-1],y[n-1]);
    cout<<maxx<<endl;
    return 0 ;
}

 

Question B: Number Game

Time Limit: 1.000 Sec Memory Limit: 512 MB
Submit state

Title Description

One day, Xiao Ming raised a question for Jiajia.
Given a positive integer n, Jiajia can do the following three operations:
1. Subtract n from 1
2. If n is a multiple of 2, divide n by 2
3. If n is a multiple of 3, divide n by 3
Ask Jiajia to change n to zero in at least a few steps.
To test the knowledge level of Jiajia, Xiao Ming gives T numbers n1,n2,..., nT so that Jiajia can give an answer to each number. However, Jiajia is only thinking about what to eat in the evening. He wants you to be smart enough to help him solve this problem. He promises to invite you to eat after solving this problem, so you have to take on this task.

input

The first line has a positive integer T, which means there are T numbers in total.
Next T lines, each with a positive integer ni

output

There are T lines, one number per line, and the answer to ni for behavior i.

Sample input Copy

2 
7 
10

Sample Output Copy

4
4

Tips

[Sample explanation]
7->6->2->1->0
10->9->3->1->0

[Data Range]
For 40% of the data, T < 10, ni < 30
For 70% of the data, T < 20, ni < 1000
For 100% of the data, T < 20, ni < 1000000

This question was not to be done at that time. After that, I asked my classmates to do it again. Here's my classmate's code.

This idea is as follows:

It's easy to roll out the number of steps needed to get 0, 1, 2, 3 to 0 first.

Attached is my classmate's code

#include<bits/stdc++.h>
using namespace std;
int a[1000010]={0,1,2,2};
int main()
{
    for(int i=4;i<1000010;i++)
    {
        int k=0x3f3f3f;
        if(i%2==0)k=min(k,a[i/2]);
        if(i%3==0)k=min(k,a[i/3]);
        k=min(k,a[i-1]);
        k=k+1;
        a[i]=k;
    }
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        cout<<a[n]<<endl;
    }
    return 0 ;
}

Question E: Friends

Time Limit: 1.000 Sec Memory Limit: 128 MB
Submit state

Title Description

After six years of hard work, Xiao Ming was finally admitted to a well-known middle school. Good Xiao Ming is always interested in strange things. This time he wants to know who has the most friends in this new school. Since everyone just reported, Xiao Ming only knows if there is a friendship between them. (

input

The first row has two integers n and m, n for the total number and m for the total number of relationships. *
The next n rows have two integers a and b separated by spaces, indicating that a and b are friends and that a and b are integers from 1 to n. No duplicate friendships will be given. (

output

Only one line, representing the friends owned by the people with the most friends, separates each two integers by a space and outputs them from smallest to largest in dictionary order. If there are multiple people with the most friends, output the answer of the person with the lowest dictionary order, see the example. (

Sample input Copy

3 3 
1 2 
2 3 
1 3

Sample Output Copy

2 3

Tips

1, 2, 3 all have 2 friends, so you can output a friend with a smaller dictionary order.
Friends of 1 are 2 and 3, which output numbers in dictionary order from small to large, separated by spaces.

50% data, 1<=n<=10
80% of data, 1<=n<=1000
100% data, 1<=n<=10000, m<=500000

I don't seem to be able to solve this problem in any way, but after I finished, I saw a lot of very simple methods. This seems to be the topic of NOIP.

I'm taking a long time. Don't choose to whine

#include<bits/stdc++.h>
using namespace std;
int n,m,j=0;
int b[10010]={0},d[10010]={0},a[1000010];
struct friends
{
    int f;
    int cnt;
}c[10010];
int cmp(friends a,friends b)
{
    if(a.cnt!=b.cnt)
    {
        return a.cnt>b.cnt;
    }
    else{
        return a.f<b.f;
    }
}
int main()
{
   cin>>n>>m;
   for(int i=1;i<=2*m;i++)
   {
       cin>>a[i];
       b[a[i]]++;
   }
   for(int i=1;i<=n;i++)
   {
       c[i].f=i;
       c[i].cnt=b[i];
   }
   sort(c+1,c+n+1,cmp);
   for(int i=1;i<=2*m;i++)
   {
       if(a[i]==c[1].f)
       {
           if(i%2!=0)
           {
               d[j++]=a[i+1];
           }
           else{
            d[j++]=a[i-1];
           }
       }
   }
   sort(d,d+j);
   for(int i=0;i<j;i++)
   {
       cout<<d[i]<<" ";
   }
    return 0 ;
}

Question G: Score Statistics 2

Time Limit: 1.000 Sec Memory Limit: 128 MB
Submit state

Title Description

After counting his friends, Xiao Ming is interested in everyone's graduating school, but he thinks that simply counting the number of students is a very boring thing, so he designs an algorithm. The first student who graduates from the same school will get 1 point, the second two points, the third four points..., the second will get 2i-1 points, the total score is the score of this elementary school. Xiao Ming wants to know how many points the school with the highest score has.

input

The first row has two integers, N and m, n for the total number and m for the number of known collegiate relationships.
Next n rows, each row has two integers a and b separated by spaces, indicating that a and b are from the same school and that a and b are integers between 1 and n. No duplicate information will be given.

output

Only one line, the highest score in all schools. The final score may be very high, you only need to output the last 100 bits, less than 100 bits please output directly.

Sample input Copy

5 3
1 2
3 4 
1 3

Sample Output Copy

15

Tips

1, 2, 3, 4 come from the same school and get a score of 1+2+4+8=15

60% of data, 1<=n<=10
80% of data, 1<=n<=70
100% data, 1<=n<=10000, 1<=m<=100000

This topic was not going to be checked and collected at that time + the blog I read with High Precision Student Sale really talked thoroughly; I also wanted to find several exercises and collected.

Attached code:

#include<bits/stdc++.h>
using namespace std;
int pre[10010],a[10010],m1;
int find(int x)
{
    if(pre[x]==0)return x;
    return pre[x]=find(pre[x]);
}
void join(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    {
        pre[fx]=fy;
        a[fy]+=a[fx];
    }
    m1=max(m1,max(a[fx],a[fy]));
}
int main()
{
    int n,m,x,y,k=0;
    cin>>n>>m;
     for(int i=1;i<=n;i++)
    {
        a[i]=1;
    }
    for(int i=0;i<m;i++)
    {
        cin>>x>>y;
        join(x,y);
    }
    int b[110]={0},c[110]={0};
    b[100]=1;
    c[100]=1;
    for(int i=2;i<=m1;i++)
    {
        k=0;
        for(int j=100;j>=1;j--)
        {
            b[j]=b[j]*2+k;
            k=b[j]/10;
            b[j]=b[j]%10;
        }
        k=0;
        for(int j=100;j>=1;j--)
        {
            c[j]=b[j]+c[j]+k;
            k=c[j]/10;
            c[j]=c[j]%10;
        }
    }
    int q=1;
    while(c[q]==0&&q<100){q++;}
    for(int i=q;i<=100;i++)
    {
        cout<<c[i];
    }
    return 0 ;
}

Question I: Order a cow

Time Limit: 1.000 Sec Memory Limit: 128 MB
Submit state

Title Description

There are thousands of cows on Farmar John's farm. The huge herd adds a lot of trouble to the FJ. Although he numbered each cow from the beginning, the FJ is often dizzied by huge numbers at the morning roll call (to be precise, the order of the cow), such as:
FJ: 120,345,999 cows! (
Cow 123459999: To! (
FJ: 120,340 million cows...????...!@#$!@#$ (
Can you help FJ solve this problem? You just need to write a program that reminds FJ of the next cow number when he points to a cow. ^^ (

input

There is only one integer: the number N of the cow FJ just ordered. 1 < N < 1050.

output

Tell FJ the number of the next cow!

Sample input Copy

3

Sample Output Copy

4

I thought it was high precision when I saw it, but I didn't whine so I used a string to fill in the high precision question above, and then I seemed to understand that until I try high precision writing tomorrow

Attached code:

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

int main()
{
    string s;
    cin>>s;
    int cnt=0;
    int l=s.size();
    for(int i=0;i<l;i++)
    {
        if(s[i]=='9')
        {
            cnt++;
        }
    }
    if(s[l-1]=='9')
    {
        for(int i=l-1;i>=0;i--)
        {
            if(s[i]=='9')
            {
                s[i]='0';
            }
            else{
                s[i]++;
                break;
            }
        }
    }
    else{
        s[l-1]++;
    }
    if(cnt==l)
    {
        cout<<"1";
    }
    for(int i=0;i<l;i++)
    {
        cout<<s[i];
    }
    return 0 ;
}

The remaining questions haven't completed the DP dynamic planning. I really don't understand the online analysis or the pain. Try to make up the remaining ones tomorrow. (

 

Topics: C++ Algorithm