Blue Bridge Cup - 2021 12th C/C++ Zhenti [Provincial Competition] [Group B]

content

card

straight line

cargo placement

path

space

Weighing with weights

time display 

Yang Hui triangle number 

 two-way sorting

bracket sequence 


card

Ideas: At first glance, this question gives people a very difficult feeling, but it is actually very simple, that is, traversing each bit of a number.

#include<iostream>
using namespace std;
int main()
{
	int a[10] = { 0 };
	int t = 0;
	for (int i = 0; i <= 9; i++)
	{
		a[i] = 2021;
	}
	for ( t = 1; t <= 9900; t++)
	{
		int i = t;
		while (i > 0)
		{
			a[i % 10]--;
			if (a[i % 10] <= 0)
			{
				cout << t << endl;
				return 0;
			}
			i = i/ 10;
		}
	}
	
}

Answer: 3181 

straight line

Two-point line equation:
(y1-y2) * x +(x2-x1) * y +( x1 * y2 - x2 * y1)=0

Idea: first store all the coordinates, traverse all the coordinate groups to obtain A, B, C of the straight line Ax+By+C=0 and use gcd to divide and finally use set to deduplicate, and finally add perpendicular to the x-axis and y number of axes.

#include<iostream>
#include<set>
using namespace std;
typedef pair<double, double> PII;
typedef pair<PII, double> PIII;
set<PIII> ss;
int gcd(int a, int b)
{
	if (b == 0) return a;
	return gcd(b, a % b);
}
int main()
{
	for (int x1 = 0; x1 <= 19; x1++)
	{
		for (int y1 = 0; y1 <= 20; y1++)
		{
			for (int x2 = 0; x2 <= 19; x2++)
			{
				for (int y2 = 0; y2 <= 20; y2++)
				{
					if (x1 != x2 && y1 != y2)
					{
						double a = x2 - x1;
						double b = y1 - y2;
						double c = y2 * x1 - x2 * y1;
						double m = gcd(gcd(a, b), c);
						ss.insert({ { a / m,b / m }, c / m });

						//ss.insert(a);
					}
				}
			}
		}
	}
	cout << ss.size() + 20 + 21;
	return 0;
}

Answer: 40257 

cargo placement

Idea: This question is a fill-in-the-blank question, a direct pure violence method, but to be slightly optimized, we can reduce the for loop or narrow the scope of the loop. This question is broken down into three factors (a, b, c), to keep a<=b<=c; the scope of the loop can be narrowed by sqrt

#include<iostream>
using namespace std;
#define n 2021041820210418
//n=a*b*c
typedef long long ll;
int ants = 0;
int main()
{
	for (ll a = 1; a * a * a <= n; a++)
	{
		if (n % a == 0)
		{
			for (ll b = a; a * b * b <= n; b++)
			{
				if (n / a % b == 0)
				{
					ll c = n / a / b;
					if (a == b && a == c)ants = 0;
					else if (a == b || a == c || c == b) ants += 3;
					else ants += 6;
				}
			}
		}
	}
	cout << ants << endl;
	return 0;
}

This is a solution I saw on the Internet. Put a, b, c into a set. I have used this set frequently in recent years, so practice as much as possible. 

#include<iostream>
#include<cmath>
#include<set>
using namespace std;
#define n 2021041820210418
//n=a*b*c
int ants = 0;
typedef long long ll;
int main()
{
	ll end = sqrt(n);
	for (ll a = 1; a <= end; a++)
	{
		if (n % a == 0)
		{
			ll nn = n / a;
			ll end1 = sqrt(nn);
			for (ll b = a; b <= end1; b++)
			{
				if (nn % b == 0)
				{
					ll c = nn / b;
					if (c >= b)
					{
						set<int> s;
						s.insert(a);
						s.insert(b);
						s.insert(c);
						if (s.size() == 1)ants += 1;
						else if (s.size() == 2) ants += 3;
						else if(s.size()==3) ants += 6;
					}
				}
			}
		}
	}
	cout << ants << endl;
	return 0;
}

Answer: 2430 

path

 Idea: Just use the Floyd algorithm to run this question. Anyway, you are not afraid of filling in the blanks if the question is overtime. The three-layer loop will wait for dozens of seconds. Dijkstra is too troublesome to write.

#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
#define INT 0x3f3f3f3f
ll Edge[2022][2022];
int gcd(int a, int b)//最大公约数
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int lcm(int a, int b)//最小公倍数
{
	int c = a * b;
	return c / gcd(a, b);
}
int main()
{
	//初始化
	memset(Edge, INT, sizeof(Edge));
	for (int i = 1; i <= 2021; i++)
	{
		for (int j = 1; j <= 2021; j++)
		{
			if (i == j)Edge[i][j] = 0;
			else {
				if (abs(i - j) <= 21)//判断差的绝对值是否小于等于21
				{
					Edge[i][j] = lcm(i, j);
				}
			}
		}
	}
//floyd算法核心
	for (int k = 1; k <= 2021; k++)
	{
		for (int i = 1; i <= 2021; i++)
		{
			for (int j = 1; j <= 2021; j++)
			{
				if (Edge[i][j] > Edge[i][k] + Edge[k][j])
				{
					Edge[i][j] = Edge[i][k] + Edge[k][j];
				}
			}
		}
	}
	cout << Edge[1][2021];
	return 0;
}

Answer: 10266837 

space

Ideas:

1MB=1024KB  1KB=1024B   1B=8b

  • Answer: ((256*1024)*1024) / 4== 67108864

Weighing with weights

input sample

3
1 4 6 

Sample output

10 

Ideas:

code

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110, M = 200010, B = M / 2;
int n, m;
int w[N];
bool f[N][M];
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin>>w[i], m += w[i];
	f[0][B] = true;
	for (int i = 1; i <= n; i++)
	{
		for (int j = -m; j <= m; j++)
		{
			f[i][j+B] = f[i - 1][j+B];
			if (j - w[i] >= -m) f[i][j+B] |= f[i - 1][j - w[i]+B];
			if (j + w[i] <= m)f[i][j+B] |= f[i - 1][j + w[i]+B];

		}
	 }
	int res = 0;
	for (int j = 1; j <= m; j++)
	{
		if (f[n][j + B])
		{
			res++;
		}
	}
	cout<<res<<endl;
	return 0;
}

I also saw a particularly easy-to-understand idea: at that time, when a weight was found to be a negative number, and when adding and subtracting with the subsequent state, the positive number and subtraction can also represent a negative number.
If there are weights 2 1 3
, the first two The states that can be pieced together 1 2 3 -1,
3 + (-1) and 3 - 1 have the same effect, so the negative weight state is discarded and the final result is unchanged.

#include<iostream>
using namespace std;
int dp[105][100005];
int main()
{
	int n;
    cin >> n;
	dp[0][0] = 1;
	for (int i = 1;i <= n;i++)
	{
		int a;cin >> a;
		for (int j = 0;j <= 100000;j++)
		{
			if (dp[i-1][j])
			{
				dp[i][j] = 1;
				dp[i][j + a] = 1;
				dp[i][abs(j - a)] = 1;
			}
		}
	}
	int ans = 0;
	for (int i = 1;i <= 100000;i++)if (dp[n][i]) ans++;
	cout << ans;
}

time display 

 input sample

 2
46800999
1618708103123

Sample output

13:00:00
01:08:23 

Ideas: Read clearly that the title says milliseconds, and replace /1000 with seconds, because only the hours, minutes, and seconds of the last day are required, so %24*60*60 is to remove all the days before the last day; the rest is to find the hours, minutes, and seconds. , very simple

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

int main()
{
    long long int n;
    cin>>n;
    n=n/1000;
    n=n%86400;//去掉除了最后一天的前面的天数;24*60*60
    int h,m,s;
    h=n/3600;//求得最后一天的小时
    n=n%3600;
    m=n/60;//分钟
    s=n%60;//秒数
    printf("%02d:%02d:%02d",h,m,s); //输出时间常用的形式,不用判断了
    return 0;
}

Yang Hui triangle number 

input sample

2
3

Sample output

8
13 

Ideas: This question mainly tests mathematical thinking; first of all, through observation, Yang Hui's triangle is symmetrical on the left and right, and the question requires to find the first place where the number N appears, so we only need to look at the left side.

If we just enumerate by observing rows and columns, the data given above is 1 billion, which is a huge amount of data. Can we analyze this problem obliquely? here is the image

 We will find that the first oblique line is 1=C(0,0), and the second oblique line 2=C(2,1),,,,,; so that the first number of the i-th oblique line is C (2*i,i);

Next, let's determine which row is 1 billion. You can use the computer that comes with your computer to calculate it during the exam. It is calculated that you only need to enumerate the first 16 rows.

We start the enumeration from the 16th oblique line, and return to its position when a number equal to n occurs. Here, we can determine the position of n as follows: r is the line, k is the oblique line, and then calculated by r*(r+1)/2 The total number of lines in front of it plus the number k+1 in front of the line where it is located. During the search process, we found that the oblique lines are in ascending order, and we can use the binary method to search to avoid timeout.

 If we push 20;r=6,k=3;n=6*(6+1)/2+3+1=25

code

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int n;
LL C(int a, int b)   //计算C(a,b)
{
    LL res = 1;
    for(int i = a, j = 1; j <= b; i --, j ++)
    {
        res = res * i / j;
        if(res > n)
            return res;     // 大于n已无意义,且防止爆LL
    }
    return res;
}
bool check(int k)
{
   
    int l = 2 * k, r = max(n,l);
    while(l < r)
    {
        int mid = l + r >> 1;
        if(C(mid, k) >= n) r = mid;
        else l = mid + 1;
    }
    if(C(r, k) != n)
        return false;
    cout << 1ll*(r + 1) * r / 2 + k + 1 << endl;
    return true;
}
int main()
{
    cin >> n;
    // 从第16斜行枚举
    for(int i = 16; ; i--)
        if(check(i))
            break;
    return 0;
}

 Video version:

The 12th Blue Bridge Cup C++ Group B explanation _ beep beep_bilibili

 two-way sort

input sample

3 3
0 3
1 2
0 2 

Sample output

3 1 2 

Thought: I feel that the last two questions are very difficult, and they are all about thinking. It's really unfriendly to me, a novice. I can only sort violently and steal a few points. I won't put this code here. I found the video of acw and watched it, the order is clearer, the video is here:

The 12th Blue Bridge Cup C++ Group B Interpretation_bilibili_bilibili

Below I attach a blog written by a big guy, which matches this video. You can go and watch it. I don't need to write it again. The summary is too detailed.

Blue Bridge Cup I. Bidirectional Sorting_Jozky86's Blog-CSDN Blog_Blue Bridge Cup Bidirectional Sorting

Code:

#include <iostream>
#include <cstring>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int n, m;
PII stk[N];
int ans[N];
int main()
{
    scanf("%d%d", &n, &m);
    int top = 0;
    while (m -- )
    {
        int p, q;
        scanf("%d%d", &p, &q);
        if (!p)//操作1 
        {
            while (top && stk[top].x == 0) 
				q = max(q, stk[top -- ].y);//出现连续的操作1,我们取最大 
            while (top >= 2 && stk[top - 1].y <= q) 
			//如果当前的操作1比上一次的操作1范围大,则将上一次操作1和操作2删除 
				top -= 2;
            stk[ ++ top] = {0, q};//存本次最佳操作 
        }
        else if (top)//操作2 &&且操作1已经进行过(操作二第一个用没效果) 
        {
            while (top && stk[top].x == 1) q = min(q, stk[top -- ].y);
            while (top >= 2 && stk[top - 1].y >= q) top -= 2;
            stk[ ++ top] = {1, q};
        }
    }
    int k = n, l = 1, r = n;
    for (int i = 1; i <= top; i ++ )
    {
        if (stk[i].x == 0)//如果是操作1 
            while (r > stk[i].y && l <= r) ans[r -- ] = k -- ;//移动r值 ,并赋值 
        else
            while (l < stk[i].y && l <= r) ans[l ++ ] = k -- ; 
        if (l > r) break;
    }
    if (top % 2)
        while (l <= r) ans[l ++ ] = k -- ;
    else
        while (l <= r) ans[r -- ] = k -- ;

    for (int i = 1; i <= n; i ++ )
        printf("%d ", ans[i]);
    return 0;
}

bracket sequence 

 As usual, this question is still incomplete, and I used a violent search to deceive some points at the test point.

Since we can't do it, we have to learn the methods and ideas of the big guys.

Blog: 12th Blue Bridge Cup Provincial Championship C++ B Group J Question Bracket Sequence_thejohn2020's Blog-CSDN Blog_Blue Bridge Cup Bracket Sequence 

Video: C++ Group B of the 12th Blue Bridge Cup Explained_bilibili_bilibili

code

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
using LL=long long;
const int N = 5005;
int f[N][N];
int mod=1e9+7;
string s;
int n;
LL get(){
    memset(f,0,sizeof f);
    f[0][0]=1;
    for(int i=1;i<=n;i++){
        if(s[i-1]=='('){
            for(int j=1;j<=n;j++)
                f[i][j]=f[i-1][j-1];
        }
        else{
            f[i][0]=(f[i-1][1]+f[i-1][0])%mod;
            for(int j=1;j<=n;j++)
                f[i][j]=(f[i-1][j+1]+f[i][j-1])%mod;
        }
    }
    for(int i=0;i<=n;i++)
        if(f[n][i])
            return f[n][i];
    return -1;
}
int main(){
    cin>>s;
    n=s.size();
    LL x=get();
    reverse(s.begin(),s.end());
    for(int i=0;i<n;i++){
        if(s[i]==')')
            s[i]='(';
        else
            s[i]=')';
    }
    LL y=get();
    cout<<(x*y)%mod;
}

 

Guess you like

Origin blog.csdn.net/m0_58367586/article/details/123882633