Harbin University of technology 21 freshmen Programming Competition (synchronous competition)

The topic is very friendly and I like it very much. (try to make up for the previous individual games these days)

A:Exam week break defense

Code:

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

int main() {
    int n;
    scanf("%d", &n);
    if(n == 1) printf("You were studying Higher mathematics!");
    else if(n == 2) printf("You were studying Linear algebra!");
    else if(n == 3) printf("You were studying Principle of computer composition!");
    else if(n == 4) printf("You were studying Database system concept!");
    else if(n == 5) printf("You were playing games!");
    printf("\n");
    return 0;
}

B:coffee shop

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    ll n, pri;
    scanf("%lld%lld", &n, &pri);
    ll num = pri / 5;
    if(num < n) printf("%lld %lld\n", num, pri - num * 5);
    else printf("%lld %lld\n", n, pri - n * 5);
    return 0;
}

C:kiki and bob play with stones

realization:

Template problem of bash game.

Code:

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

int main() {
    int n;
    scanf("%d", &n);
    if(n % 4 == 0) printf("bob\n");
    else printf("kiki\n");
    return 0;
}

D:Monkey King kiki peach

Tag:

  • greedy
  • simulation

realization:

If the monkey king wants to get as many peaches as possible, the best situation is to get the number of monkeys minus one number of peaches, that is \ (n-1 \).
If \ (r-l\geq n \), the monkey king can take just \ (k*n+(n-1),k\geq 1 \). Of course, the premise is \ (k\geq n \) (the topic has been explained).
Otherwise, we traverse the possible values of \ (k \) and find the maximum value of the remainder of \ (n \).

Code:

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

int main() {
    int n, l, r;
    scanf("%d%d%d", &n, &l, &r);
    if(r - l >= n) printf("%d\n", n - 1);
    else {
        int maxn = -1;
        for(int i = l; i <= r; i++)
            maxn = max(maxn, i % n);
        printf("%d\n", maxn);
    }
    return 0;
}

E:A split of two

Tag:

  • Bit operation

realization:

Represent a number in binary. If a bit is 1, it is converted to decimal when the answer is finally output.

Code:

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

int main() {
    ll n;
    scanf("%lld", &n);
    vector<int> ans;
    if(n & 1) printf("-1\n");
    else {
        while(n) {
            ans.push_back(n & 1);
            n >>= 1;
        }
        int flag = 0;
        for(ll i = ans.size() - 1; i >= 0; i--) {
            if(ans[i]) {
                if(flag) printf(" ");
                printf("%lld", 1 << i);
            }
            flag = 1;
        }
        printf("\n");
    }
    return 0;
}

F:Construct string

realization:

Compare the character sizes of the beginning and end of S, and the small one is added to the end of T first. If the size of the first and last characters is the same, then we continue to observe the size of the two characters close to the middle at the first and last ends

Code:

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

int main() {
    int n;
    string s;
    cin >> n;
    cin >> s;
    int l = 0, r = n - 1;
    string res;
    while(l <= r) {
        if(s[l] < s[r]) res += s[l++];
        else if(s[l] > s[r]) res += s[r--];
        else {
            int i = l, j = r;
            bool flag = false;
            while(i <= j) {
                if(s[i] < s[j]) {
                    flag = true;
                    break;
		} else if(s[i] > s[j]) {
                    break;
                }
                i++, j--;
            }
            if(flag) res += s[l++];
            else res += s[r--];
        }
    }
    cout << res << '\n';
    return 0;
}

G:Signal journey

realization:

cf original title (I'm glad I brushed it before, but I still remember ~)
If A,B and BUG are on a straight line, the shortest distance between two points plus 2 (the shortest distance around BUG).

Code:

#include<bits/stdc++.h>
using namespace std;
int main() {
	int t;
	int xa, ya, xb, yb, xf, yf;
	scanf("%d", &t);
	while(t--) {
	    scanf("%d%d", &xa, &ya);
	    scanf("%d%d", &xb, &yb);
	    scanf("%d%d", &xf, &yf);
	    int ans = abs(xa - xb) + abs(ya - yb);
	        if(xa == xb || ya == yb) {
		    if((ya == yf && min(xa, xb) < xf && xf < max(xa, xb)) || (xa == xf && min(ya, yb) < yf && yf < max(ya, yb)))
			ans += 2;
		}
	    printf("%d\n", ans);
	}
	return 0;
}

H:Roll Me

realization:

The title says that two small balls collide elastically, so we don't have to consider the change of velocity direction after the collision of small balls.
It can be directly considered that there is only one ball sliding on the chute, which will make the problem more intuitive..
If the ball is on the left of the chute, the shortest time is the time to slide left and the longest time is the time to slide right. On the contrary, the same is true.
The question is to ask the shortest (long) time for all small balls to fall. We take the maximum every time.

Code:

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

int main() {
    int l, n, x;
    scanf("%d%d", &l, &n);
    int minn = 0, maxn = 0;
    for(int i = 0; i < n; i++) {
        scanf("%d", &x);
        if(x < l / 2) minn = max(minn, x);
        else minn = max(minn, l - x);
        if(x < l / 2) maxn = max(maxn, l - x);
        else maxn = max(maxn, x);
    }
    printf("%d %d\n", minn, maxn);
	return 0;
}

I:kiki watches the game

realization:

Conditions for a flat score:
The minimum value of the current recorded score is greater than the maximum value of the last recorded score.
The maximum number of times the score is flat is the difference between the two, because the minimum value of the current recorded score means that both teams have reached the score.
If the difference between the two is greater than or equal to 0, and the scores of the two teams recorded last time are not equal, the number of times the scores are equal shall be increased by one.
Because we have to add the situation that the two teams' flat score is the maximum score recorded last time.

Code:

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

int main() {
    int n;
    scanf("%d", &n);
    int ans = 1;
    int a, b, prea = 0, preb = 0;
    for(int i = 0; i < n; i++) {
        scanf("%d%d", &a, &b);
        int val = min(a, b) - max(prea, preb);
        if(val >= 0) {
            ans += val;
            if(prea != preb) ans++;
        }
        prea = a, preb = b;
    }
    printf("%d\n", ans);
    return 0;
}

J:Jump

Code:

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

int main() {
    ll n, x, ans = 1;
    scanf("%lld", &n);
    for(ll i = 1; i <= n; i++) {
        scanf("%lld", &x);
        if(i > ans) break;//Indicates that the i-th tile cannot be reached
        ans = max(ans, i + x);
    }
    printf("%lld\n", min(ans, n));//It may cross the border
    return 0;
}

To be continue~

Added by mikosiko on Sun, 23 Jan 2022 09:53:39 +0200