Part of the questions of the 2020 US group written test (4.9)

The US group written exams are all big questions, and they are more suitable for me. There are five questions in total.

first question

Title

Give you a date package day of the week and hours and minutes, then a time n, ask n minutes before the day of the week and hours and minutes.

analysis

The data is not too violent at all. First calculate the current total minutes, then subtract n, and continue to +1440 until it is positive. Pay attention to the format, and add 0 when outputting.

Code

#include <cstdio>
#include <iostream>
using namespace std;

int x, h, m, n, t;

int main() {
	scanf("%d",&x);
	scanf("%d:%d",&h,&m);
	scanf("%d",&n);
	t=h*60+m;
	t-=n;
	while (t<0) {
		t+=1440;
		x--; if (x==0) x=7;
	}
	printf("%d\n",x);
	h=t/60; m=t%60;
	if (h<10) printf("0%d:",h);
	else printf("%d:",h);
	if (m<10) printf("0%d\n",m);
	else printf("%d\n",m);
	return 0;
}

The second question

Title

There are 1e5 people running, giving an array a, a [i] means who is the i-th departure person, and array b, b [i] means who is the i-th arrival person. How many people ask for more than others?

analysis

I seriously doubt that the data of this question is false. I thought about it for a while, and I did n’t have much idea, and then I prepared to do it violently. O (n ^ 2) complexity, I did n’t expect to pass it directly, which is very strange.
Use one map to record the sequence of the departure of the person with the number i, and another map to record the sequence of the arrival of the person with the number i. Double loop to determine whether the i-th person surpasses the others, and traverse the j-person. If the i-th person starts later than j and ends earlier than j, then it surpasses others, and the answer is +1.

#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

int n, ans=0;
int a[100005];
int b[100005];
map<int, int> st;
map<int, int> en;

int main() {
	scanf("%d",&n);
	for (int i=1; i<=n; ++i) {
		scanf("%d",&a[i]);
		st[a[i]] = i;
	}
	for (int i=1; i<=n; ++i) {
		scanf("%d",&b[i]);
		en[b[i]] = i;
	}
	for (int i=1; i<=n; ++i) {
		for (int j=1; j<=n; ++j) {
			if (i==j) continue;
			if (st[i] > st[j] && en[i] < en[j]) {
				ans++;
				break;
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}

Question 3

Title

It seems that given n and k, find a minimum x such that x / k + x / k 2 + . . . + x / k m > = n x/k + x/k^{2} + ... + x/k^{m} >= n

analysis

The dichotomy is x, the complexity of the judgment is lgn, and the dichotomy is also lgn. It was quickly calculated.

Code

#include <cstdio>
#include <iostream>
typedef long long ll;
using namespace std;

ll n, k;

bool judge(ll x) {
	ll t=1, s=0;
	while(x/t) {
		s+=x/t;
		t*=k;
	}
	return s>=n;
}

int main() {
	scanf("%lld%lld",&n, &k);
	ll l=1, r=n, m;
	while(l<r) {
		m=(l+r)/2;
		if (judge(m)) r=m-1;
		else l=m+1;
	}
	while(!judge(l)) l++;
	printf("%lld\n",l);
	return 0;
}

Fourth question

Title

There is a regular tetrahedron with S at the top and ABC at the bottom. There are a total of six roads SA, SB, SC, AB, AC, BC. Starting from S, ask how many roads can return to S after k steps.

analysis

A relatively simple dp. In dp [i] [j], i represents the step i, j represents the vertex reached after step i, and 0123 represents SABC. Then the state transition is also very simple, depending on the code.

Code

#include <cstdio>
#include <iostream>
#include <cstring>
const int MOD = 1e9+7;
using namespace std;

int k;
int dp[1000005][10];

int main() {
	memset(dp,0,sizeof(dp));
	scanf("%d",&k);
	dp[0][0]=1; dp[1][1]=1; dp[1][2]=1; dp[1][3]=1;
	for (int i=1; i<=k; ++i) {
		dp[i][0] = ((dp[i-1][1]%MOD+dp[i-1][2]%MOD)%MOD+dp[i-1][3]%MOD)%MOD;
		dp[i][1] = ((dp[i-1][0]%MOD+dp[i-1][2]%MOD)%MOD+dp[i-1][3]%MOD)%MOD;
		dp[i][2] = ((dp[i-1][0]%MOD+dp[i-1][1]%MOD)%MOD+dp[i-1][3]%MOD)%MOD;
		dp[i][3] = ((dp[i-1][0]%MOD+dp[i-1][1]%MOD)%MOD+dp[i-1][2]%MOD)%MOD;
	}
	printf("%d\n",dp[k][0]%MOD);
	return 0;
}

Question 5

Title

Give a set of k size strings, all in the set at the beginning, and then dynamically operate, one operation is to add the xth string (in k), the second is to delete, the third is to query the string S . The result of the query is the matching sum of S as the mother string and the strings in the set as substrings.

analysis

The emm looks like an AC automaton, but I do n’t know how to write a violent, actually WA, and then changed to WA. . . Could it be that I read the title wrongly and I don't want to write it later, it is over.

Published 142 original articles · praised 33 · 40,000+ views

Guess you like

Origin blog.csdn.net/Radium_1209/article/details/105421354