Solution to 45 A-E questions of Niuke monthly competition

Posted by jameslynns on Fri, 04 Mar 2022 22:52:21 +0100

preface

link
Niuke Xiaobai monthly race 45

Finish five questions for the first time hh

Try to write a solution

The codes are directly copy the game time codes, which may be messy

I'll try to write notes hh
If you have any questions, please point them out hh

A

This problem is difficult to read...

If n > = x
Then you can jump horizontally repeatedly, ans = n*x

If n < x, he will fall and die after the first jump!
There is no other choice, because this is a cliff. You can't skip without jumping, and you can't jump over it

Therefore, reading in for interpretation can reduce the complexity o(1)

#include<bits/stdc++.h>
#define endl "\n"
#define B 0x3f3f3f3f
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
typedef pair<int,int> PII;

int main()
{
	ll a,b;
	cin>>a>>b;
	if (a<b)
    {
        b=1;
    }
	a*=b;
	cout<<a;
	return 0;
}

B

Find a regular problem and simulate it a little
Found if input is 5

Then the first layer dfs(1) ans+=1

Then enter the second layer, dfs(1+2) ans+=3

... dfs(3+2) ans += 5

dfs(5+2) ans+=7
dfs(7+2) ans+= 9

It is found that the number added each time is 1 3 5 7 9 11
Summation formula of direct arithmetic sequence

ans = (1+2*n-1) * n / 2 = n * n

The law is direct square, complexity o(1)

#include<bits/stdc++.h>
#define endl "\n"
#define B 0x3f3f3f3f
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
typedef pair<int,int> PII;

int main()
{
	ll a,b;
	cin>>a;
	b=(a)*a;
	cout<<b;
	return 0;
}

C

Is it greed? Or recursive?

In order to get the most scores, we must turn small candy into big candy as much as possible

In that way, the big candy can continue to synthesize, so that the score continues to increase

Note that three or four can be synthesized

Then, try to use the synthesis of three as much as possible, for example
There are 12 grade 1 candies, 12 = 3 * 4 = 4 * 3

If the synthesis of 3 is used, four grade 2 candies will be obtained,

If 4 is used, only 3 grade 2 candies can be obtained. This is not conducive to maximizing the score

Both methods get 12 points, but there is an extra level 2

It is possible that level 2 can continue to synthesize to level 3, making the score larger

Give the code first, and then explain it with notes

#include<bits/stdc++.h>
#define endl "\n"
#define B 0x3f3f3f3f
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
typedef pair<int,int> PII;

ll ar[10];
ll ans;
int main()
{
	For(i,1,8)cin>>ar[i];
	For(i,1,8)
	{
		ll a = ar[i]/3;
        // a is the quantity of candy of current grade synthesized according to 3
        // b is the remainder
		ll b=ar[i]%3;
		if (a)
		{
        //a synthesis, then the number of high-grade candy will increase
			ar[i+1]+=a;
            //If ar[i] = 5
            // a=1,b=2. At this time, according to the synthesis method of 3, there will be two candies left
            //We can only add one so that it can be carried out in the way of 4,
           //You can't use both of the rest, so I wrote a min,
           //In fact, when discussing with friends after the game, only a special judgment of 5 is enough hh
			b=min(b,a);
			ans+=(a*3+b)*i;
            // Remember to open LL when calculating the score
		}
	}
	cout<<ans;
	
	return 0;
}

Only 8 numbers need to be traversed, so the complexity is o (1)

D

Should it also be a regular problem?
I'm looking for law greed + fast power calculation.. Or is the positive solution DP?

thinking

Conclusion 2^(cnt) is the answer

cnt is the number of parentheses successfully paired

I found this conclusion by looking for rules during the game
There was no proof at the time

Find the law first

(1) without cutting
() 2, either cut in the middle or not

Next, we will use 1 to represent (), () () to write 11 directly

() there are 4

No cutting 111

Only cut once

[1] 1,11 (), ()()

[2] 11,1 ()(), ()

Cut twice 1, 1, 1 (), (), ()

There are 4 kinds in total

() there are eight ways to do this

Do not cut 1111

1🔪
[1] 1,111

[2] 11,11

[3] 111,1

2🔪
[1] 1,1,11

[2] 1,11,1

[3] 11,1,1

3🔪

1,1,1,1

So there are eight

Here, we can find the law

(1) 2 ^ 0 = 1 parentheses for successful pairing

() 2 pairs, 2 ^ 1 = 2

()()() , 2^3 = 8

()()()(), 2^4 = 16

So the answer is to the power of 2^ cnt

Of course, the number E7 + 7 will be very large

#include<bits/stdc++.h>
#define endl "\n"
#define B 0x3f3f3f3f
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
typedef pair<int,int> PII;

string s;
const int N=1e6+10;
int a[N],b[N],sum[N];
//Counting 
// 
ll qmi(ll a,ll b, ll p)
{
	ll ans=1;
    
	while(b)
	{
    
		if (b&1)ans=(ans*a)%p;
		b>>=1;
		a=(a*a)%p;
	}
	return ans;
}
int main()
{
	cin>>s;
	int cnt=0;
	int n=s.size();
	int f=1;
	
	For(i,1,n)
	{
		if (s[i-1]=='(')
		{
			a[i]=a[i-1]+1;
			b[i]=b[i-1];
		}
		else
		{
			b[i]=b[i-1]+1;a[i]=a[i-1];
		}
		if (f && b[i]>a[i])
		{
			f=0;
		}
		else if (b[i]==a[i])
		{
			sum[i]=++cnt;
		}
	}
    //Here we first calculate the number of successful pairings
    //Then there must be some judgment conditions
    //For example, if the right bracket precedes the left bracket, it is impossible to succeed
    // Or, (()) if such a number is not right, it will not succeed
    //These are all output - 1
    // Note - 1, not 0
	ll ans=0,p=1e9+7;
	if (a[n]!=b[n] || !f || cnt==0)
	{
		ans=-1;
	}
	else
	{
    //Here is fast power hot hh
		ans=qmi(2,cnt-1,p);
		//cout<<cnt<<"\n";
	}
	cout<<ans;
	return 0;
}
// () () () () () I actually listed hh in five brackets when I played

We need to traverse the string to find the number of successful matches, so the complexity is o(N)

Here's a little proof of the conclusion

When the string is legal, add a pair of parentheses for each successful match

It can be understood that putting this pair at the back,

xxxx AB x is the previous sequence and ab is a new pair of parentheses

Either cut between xA or not

Moreover, this one size fits all has nothing to do with the number of schemes in the previous xxxx sequence

No aftereffect is greed

Therefore, the answer is xxxx's scheme * 2

In other words, for each additional pair, the number of schemes * 2

Always * 2 * 2 * 2, so it's exponential

E

Hold on, the last question is hot hh

Idea tree DP or DFS?

Start from a point and traverse to each of its edges

Will continue to try whether to add this edge and this point

If, the comfort of an edge + a point is a positive number

Then, he can make the overall comfort greater

That is, we need to add him

Conversely

If the comfort of a point + an edge is negative

That plus him will only reduce the overall comfort

Choose not to add

The final output is OK

Upper code

#include<bits/stdc++.h>
#define endl "\n"
#define B 0x3f3f3f3f
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
typedef pair<int,int> PII;

const int N=1e6+10;
ll ar[N],st[N],h[N],e[N],ne[N],w[N];
ll n,m,idx,a,b,sum,c;
ll dp[N],ans;
//Edging operation
void add(ll a,ll b,ll c)
{
	w[idx]=c;
	e[idx]=b;ne[idx]=h[a];h[a]=idx++;
}

//Core function
void dfs(ll x)
{
//In order to avoid repeating an access point with st []
	if (st[x])return ;
	st[x]=1;
	dp[x]=ar[x];
//A point's own comfort    
	
    
	for (int i=h[x];i!=-1;i=ne[i])
	{
		int j=e[i];//Traverse each exit edge
		if (st[j])continue;
		dfs(j);
        //After this dfs function
        //The value of dp[j] has been calculated
        //Judge
        
        
        //If it is a positive number, it can make the ant more comfortable and add him
		if (dp[j]+w[i]>0)
		{
			dp[x]+=dp[j]+w[i];
		}
	}
	

}
int main()
{
	
	memset(h,-1,sizeof h);
	scanf("%lld",&n);
	ans=-INT_MAX;

//Enter each point
//Note that you need to save the comfort of the maximum point
//Because if all points and edges are comfortable, they are negative
//Then you won't join him
//And we don't have the heart to make ants homeless
//In order to reflect humanitarian care
//I will give one. Which one?
//It's the taller of the most comfortable dwarfs
	For(i,1,n)
	{
		scanf("%lld",ar+i);
		ans=max(ans,ar[i]);
	}
    
    //Read in edge, undirected graph, add 2 times
	For(i,1,n-1)
	{
		scanf("%lld%lld%lld",&a,&b,&c);
		
		add(b,a,c);
		add(a,b,c);
	}

//Traverse
	For(i,1,n)
	{
		if (!st[i])
		{
			dfs(i);
		}
	}

//Record maximum results
	For(i,1,n)
	{
		ans=max(ans,dp[i]);
	}
	cout<<ans;


	return 0;
}

We traverse every point and edge without repetition or dependence, so the complexity is o(N)

Learning algorithms on acwing!

Finish scattering flowers!

Topics: C Algorithm greedy algorithm