1. 程式人生 > 其它 >第46屆ICPC 東亞洲區域賽(澳門)(熱身賽)

第46屆ICPC 東亞洲區域賽(澳門)(熱身賽)

A. K-skip Permutation

連結:https://ac.nowcoder.com/acm/contest/31453/A
來源:牛客網

題目描述

For a permutation P=p1,p2,⋯ ,pnP=p1,p2,⋯,pn of nn, let f(P,k)f(P,k) be the number of ii satisfying 1≤i<n1≤i<n and pi+k=pi+1pi+k=pi+1.

Given two integers nn and kk, your task is to find a permutation PP of nn such that f(P,k)f(P,k) is maximized.

Recall that in a permutation of nn, each integer from 11 to nn (both inclusive) appears exactly once.

輸入描述:

There is only one test case in each test file.

The first and only line contains two integers nn and kk (1≤n,k≤1061≤n,k≤106).

輸出描述:

Output one line containing nn integers indicating a permutation PP of nn that maximizes f(P,k)f(P,k). If there are multiple valid answers you can output any of them.

Please, DO NOT output extra spaces at the end of the line, or your answer may be considered incorrect!

示例1

輸入

複製

3 1

輸出

複製

1 2 3

示例2

輸入

複製

7 3

輸出

複製

2 5 1 4 7 3 6

示例3

輸入

複製

3 7

輸出

複製

1 3 2

找出模k=0的所有數,模k=1的所有數...分別從小到大輸出即可。因為屬於不同組的兩個數對答案顯然沒有貢獻。證明如下:設\(x=pk+a,y=qk+b\),且\(x<y\),則\(y-x=(b-a)+(q-p)k\)。因為\(b-a\neq0\)(兩個數屬於不同組),因此做差後不可能等於\(k\)

#include <bits/stdc++.h>
using namespace std;
bool v[2000005];
vector<int> ans[1000005];
int main() {
    int n, k;
    cin >> n >> k;
    for(int i = 1; i <= n; i++) {
        ans[i % k].push_back(i);
    }
    for(int i = 0; i < k; i++) {
        for(auto x : ans[i]) cout << x << " ";
    }
    return 0;
}

B. Hotpot

連結:https://ac.nowcoder.com/acm/contest/31453/B
來源:牛客網

題目描述

Sichuan hotpot is one of the most famous dishes around the world. People love its spicy taste.

There are nn tourists, numbered from 00 to (n−1)(n−1), sitting around a hotpot. There are kk types of ingredients for the hotpot in total and the ii-th tourist favors ingredient aiai most. Initially, every tourist has a happiness value of 00and the pot is empty.

The tourists will perform mm moves one after another, where the ii-th (numbered from 00 to (m−1)(m−1)) move is performed by tourist (imod  n)(imodn). When tourist tt moves:

  • If ingredient atat exists in the pot, he will eat them all and gain 11 happiness value.
  • Otherwise, he will put one unit of ingredient atat into the pot. His happiness value remains unchanged.

Your task is to calculate the happiness value for each tourist after mm moves.

輸入描述:

There are multiple test cases. The first line of the input contains an integer TT (1≤T≤1031≤T≤103) indicating the number of test cases. For each test case:

The first line contains three integers nn, kk and mm (1≤n≤1051≤n≤105, 1≤k≤1051≤k≤105, 1≤m≤1091≤m≤109) indicating the number of tourists, the number of types of ingredients and the number of moves.

The second line contains nn integers a0,a1,⋯ ,an−1a0,a1,⋯,an−1 (1≤ai≤k1≤ai≤k) where aiai indicates the favorite ingredient of tourist ii.

It's guaranteed that neither the sum of nn nor the sum of kk of all the test cases will exceed 2×1052×105.

輸出描述:

For each test case output nn integers h0,h1,⋯ ,hn−1h0,h1,⋯,hn−1 in one line separated by a space, where hihi indicates the happiness value of tourist ii after mm moves.

Please, DO NOT output extra spaces at the end of each line, or your answer might be considered incorrect!

示例1

輸入

複製

4
3 2 6
1 1 2
1 1 5
1
2 2 10
1 2
2 2 10
1 1

輸出

複製

0 2 1
2
2 2
0 5

首先可以知道每類對應的數量不是0就是1,且經過偶數輪後所有類的數量都會變成0。因此可以求出經過前兩輪後每類材料的答案。設總輪數為round = m / n,剩餘的操作次數為res = m % n,如果m < n的話直接暴力跑;如果round為奇數且不為1(為1的話直接暴力跑),則先跑兩輪求出貢獻,然後把每類材料的答案乘以round / 2,剩下一輪和res暴力跑;如果round為偶數直接跑兩輪然後把每類材料的答案乘以round / 2,res暴力跑。程式碼寫的比較醜~

#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a[100004], b[100005], tans[100005];
int main() {
    int t;
    cin >> t;
    while(t--) {
        cin >> n >> k >> m;
        for(int i = 0; i < n; i++) {
            cin >> a[i];
            tans[i] = 0;
        }
        for(int i = 0; i <= k; i++) b[i] = 0;
        int round = m / n, res = m % n;
        if(round) {
            for(int i = 0; i < n; i++) {
                if(b[a[i]]) {
                    tans[i]++; b[a[i]]--;
                } else {
                    b[a[i]]++;
                }
            }
            if(round & 1) {
                if(round > 1) {
                    for(int i = 0; i < n; i++) {
                        if(b[a[i]]) {
                            tans[i]++; b[a[i]]--;
                        } else {
                            b[a[i]]++;
                        }
                    }
                    for(int i = 0; i < n; i++) tans[i] *= (round / 2);
                    for(int i = 0; i < n; i++) {
                        if(b[a[i]]) {
                            tans[i]++; b[a[i]]--;
                        } else {
                            b[a[i]]++;
                        }
                    }
                }
            } else {
                for(int i = 0; i < n; i++) {
                    if(b[a[i]]) {
                        tans[i]++; b[a[i]]--;
                    } else {
                        b[a[i]]++;
                    }
                }
                for(int i = 0; i < n; i++) tans[i] *= (round / 2);
            }
            for(int i = 0; i < res; i++) {
                if(b[a[i]]) {
                    tans[i]++; b[a[i]]--;
                } else {
                    b[a[i]]++;
                }
            }
            for(int i = 0; i < n; i++) cout << tans[i] << " ";
            cout << endl;
        } else {
            for(int i = 0; i < m; i++) {
                if(b[a[i]]) {
                    tans[i]++; b[a[i]]--;
                } else {
                    b[a[i]]++;
                }
            }
            for(int i = 0; i < n; i++) cout << tans[i] << " ";
            cout << endl;
        }
    }
}

C. Wandering Robot

連結:https://ac.nowcoder.com/acm/contest/31453/C
來源:牛客網

題目描述

DreamGrid creates a programmable robot to explore an infinite two-dimension plane. The robot has a basic instruction sequence a1,a2,…ana1,a2,…an and a "repeating parameter" kk, which together form the full instruction sequence s1,s2,…,sn,sn+1,…,snks1,s2,…,sn,sn+1,…,snk and control the robot.

There are 4 types of valid instructions in total, which are U' (up), D' (down), L' (left) and R' (right). Assuming that the robot is currently at (x,y)(x,y), the instructions control the robot in the way below:

  • U: Moves the robot to (x,y+1)(x,y+1).
  • D: Moves the robot to (x,y−1)(x,y−1).
  • L: Moves the robot to (x−1,y)(x−1,y).
  • R: Moves the robot to (x+1,y)(x+1,y).

The full instruction sequence can be derived from the following equations

{si=aiif 1≤i≤nsi=si−notherwise{si=aisi=si−nif 1≤i≤notherwise

The robot is initially at (0,0)(0,0) and executes the instructions in the full instruction sequence one by one. To estimate the exploration procedure, DreamGrid would like to calculate the largest Manhattan distance between the robot and the start point (0,0)(0,0) during the execution of the nknk instructions.

Recall that the Manhattan distance between (x1,y1)(x1,y1) and (x2,y2)(x2,y2) is defined as ∣x1−x2∣+∣y1−y2∣∣x1−x2∣+∣y1−y2∣.

輸入描述:

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and kk (1≤n≤105,1≤k≤1091≤n≤105,1≤k≤109), indicating the length of the basic instruction sequence and the repeating parameter.

The second line contains a string A=a1a2…anA=a1a2…an (∣A∣=n∣A∣=n, ai∈{’L’,’R’,’U’,’D’}ai∈{’L’,’R’,’U’,’D’}), where aiai indicates the ii-th instruction in the basic instriction sequence.

It's guaranteed that the sum of ∣A∣∣A∣ of all test cases will not exceed 2×1062×106.

輸出描述:

For each test case output one line containing one integer indicating the answer.

示例1

輸入

複製

2
3 3
RUL
1 1000000000
D

輸出

複製

4
1000000000

備註:

For the first sample test case, the final instruction sequence is "RULRULRUL" and the route of the robot is (0, 0) - (1, 0) - (1, 1) - (0, 1) - (1, 1) - (1, 2) - (0, 2) - (1, 2) - (1, 3) - (0, 3). It's obvious that the farthest point on the route is (1, 3) and the answer is 4.

忘了是哪場比賽的原題了,可以發現對於字串的每一個操作所處的位置一定在一條直線上,因此對於每個位置求出直線的兩個端點計算對答案的貢獻即可。

#include <bits/stdc++.h>
#define int long long
using namespace std;
int tx[1000005], ty[1000005];
signed main() {
	int t;
	cin >> t;
	while(t--) {
		int n, k;
		cin >> n >> k;
		string s;
		cin >> s;
		int x = 0, y = 0;
		int ex, ey;
		for(int i = 0; i < n; i++) {
			if(s[i] == 'U') y++;
			else if(s[i] == 'D') y--;
			else if(s[i] == 'L') x--;
			else x++;
			tx[i] = x, ty[i] = y;
		}
		ex = x, ey = y;
		int ans = 0;
		for(int i = 0; i < n; i++) {
            ans = max(ans, abs(tx[i]) + abs(ty[i]));
            ans = max(ans, abs(tx[i] + (k - 1) * ex) + abs(ty[i] + (k - 1) * ey));
		}
		cout << ans << endl;
	}
	return 0;
}