AtCoder ABC 306 解析

目录

A - Echo

B - Base 2 

C - Centers

D - Poisonous Full-Course 


A - Echo

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100100 points

Problem Statement

You are given a string SS of length NN consisting of lowercase English letters.
We denote the ii-th character of SS by S_iSi​.
Print the string of length 2N2N obtained by concatenating S_1,S_1,S_2,S_2,\dots,S_NS1​,S1​,S2​,S2​,…,SN​, and S_NSN​ in this order.
For example, if SS is beginner, print bbeeggiinnnneerr.

Constraints

  • NN is an integer such that 1 \le N \le 501≤N≤50.
  • SS is a string of length NN consisting of lowercase English letters.

Input

The input is given from Standard Input in the following format:

NN
SS

Output

Print the answer.


Sample Input 1

8
beginner

Sample Output 1

bbeeggiinnnneerr

It is the same as the example described in the problem statement.


Sample Input 2

Copy

3
aaa

Sample Output 2

Copy

aaaaaa

可以定义一个变量来存储要打印的字符串,但在竞争性编程中,所需的只是正确的输出,因此您可以放松并实现以下内容:

用for语句迭代S的每个字符,并打印两次。

在这里,您必须小心,不要打印不必要的空格或换行符。

#include

using namespace std;

int main(){
  int n;
  string s;
  cin >> n >> s;
  for(int i=0;i

B - Base 2 


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 200200 points

Problem Statement

You are given a sequence A=(A_0,A_1,\dots,A_{63})A=(A0​,A1​,…,A63​) of length 6464 consisting of 00 and 11.

Find A_0 2^0 + A_1 2^1 + \dots + A_{63} 2^{63}A0​20+A1​21+⋯+A63​263.

Constraints

  • A_iAi​ is 00 or 11.

Input

The input is given from Standard Input in the following format:

A_0A0​ A_1A1​ \dots… A_{63}A63​

Output

Print the answer as an integer.


Sample Input 1

1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Sample Output 1 

13

A_0 2^0 + A_1 2^1 + \dots + A_{63} 2^{63} = 2^0 + 2^2 + 2^3 = 13A0​20+A1​21+⋯+A63​263=20+22+23=13.


Sample Input 2 

1 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 0 0 0 1 0 1 0 1 0 1 0 0 0 0

Sample Output 2 

766067858140017173

使用for语句查找问题语句中所述的答案。与下面的示例代码一样,如果您使用位移位运算符,那么实现将非常简洁。请注意,有符号的64位整数(如C++中的整数)不适用于此问题,因为它最多只能表示长263−1。(请使用无符号64位整数或bigint。)

#include

using namespace std;

using ll = unsigned long long;

int main() {
    ll ans = 0;
    for (int i = 0; i < 64; i++) {
        ll a;
        cin >> a;
        ans += a << i;
    }
    cout << ans << endl;
}


C - Centers


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 250250 points

Problem Statement

You are given a sequence A=(A_1,A_2,\dots,A_{3N})A=(A1​,A2​,…,A3N​) of length 3N3N where each of 1,2,\dots1,2,…, and NN occurs exactly three times.

For i=1,2,\dots,Ni=1,2,…,N, let f(i)f(i) be the index of the middle occurrence of ii in AA. Sort 1,2,\dots,N1,2,…,N in ascending order of f(i)f(i).

Formally, f(i)f(i) is defined as follows.

  • Suppose that those jj such that A_j = iAj​=i are j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma)j=α,β,γ (α<β<γ). Then, f(i) = \betaf(i)=β.

Constraints

  • 1\leq N \leq 10^51≤N≤105
  • 1 \leq A_j \leq N1≤Aj​≤N
  • ii occurs in AA exactly three times, for each i=1,2,\dots,Ni=1,2,…,N.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN
A_1A1​ A_2A2​ \dots… A_{3N}A3N​

Output

Print the sequence of length NN obtained by sorting 1,2,\dots,N1,2,…,N in ascending order of f(i)f(i), separated by spaces.


Sample Input 1

3
1 1 3 2 3 2 2 3 1

Sample Output 1 

1 3 2
  • 11 occurs in AA at A_1,A_2,A_9A1​,A2​,A9​, so f(1) = 2f(1)=2.
  • 22 occurs in AA at A_4,A_6,A_7A4​,A6​,A7​, so f(2) = 6f(2)=6.
  • 33 occurs in AA at A_3,A_5,A_8A3​,A5​,A8​, so f(3) = 5f(3)=5.

Thus, f(1) < f(3) < f(2)f(1)


Sample Input 2 

1
1 1 1

Sample Output 2 

1

Sample Input 3

4
2 3 4 3 4 1 3 1 1 4 2 2

Sample Output 3

3 4 1 2

解析:其实可以使用以下算法进行求解。准备一个空数组ans。按A的顺序扫描序列在这里,我们在另一个数组中维护到目前为止,每个数字在我们扫描的零件中发生了多少次。设c是您正在检查的整数。如果这是c的第二次出现,请将c附加到ans的尾部。打印ans。该算法在总共O(N)个时间内工作,这是足够快的。

#include

using namespace std;

int main() {
    int n;
    cin >> n;
    vector cnt(n + 1), ans;
    for (int i = 0; i < 3 * n; i++) {
        int a;
        cin >> a;
        ++cnt[a];
        if (cnt[a] == 2) ans.push_back(a);
    }
    for (int i = 0; i < n; i++) {
        cout << ans[i] << (i == n - 1 ? '\n' : ' ');
    }
}


D - Poisonous Full-Course 

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 400400 points

Problem Statement

Takahashi has decided to enjoy a wired full-course meal consisting of NN courses in a restaurant.
The ii-th course is:

  • if X_i=0Xi​=0, an antidotal course with a tastiness of Y_iYi​;
  • if X_i=1Xi​=1, a poisonous course with a tastiness of Y_iYi​.

When Takahashi eats a course, his state changes as follows:

  • Initially, Takahashi has a healthy stomach.
  • When he has a healthy stomach,
    • if he eats an antidotal course, his stomach remains healthy;
    • if he eats a poisonous course, he gets an upset stomach.
  • When he has an upset stomach,
    • if he eats an antidotal course, his stomach becomes healthy;
    • if he eats a poisonous course, he dies.

The meal progresses as follows.

  • Repeat the following process for i = 1, \ldots, Ni=1,…,N in this order.
    • First, the ii-th course is served to Takahashi.
    • Next, he chooses whether to "eat" or "skip" the course.
      • If he chooses to "eat" it, he eats the ii-th course. His state also changes depending on the course he eats.
      • If he chooses to "skip" it, he does not eat the ii-th course. This course cannot be served later or kept somehow.
    • Finally, (if his state changes, after the change) if he is not dead,
      • if i \neq Ni=N, he proceeds to the next course.
      • if i = Ni=N, he makes it out of the restaurant alive.

An important meeting awaits him, so he must make it out of there alive.
Find the maximum possible sum of tastiness of the courses that he eats (or 00 if he eats nothing) when he decides whether to "eat" or "skip" the courses under that condition.

Constraints

  • All input values are integers.
  • 1 \le N \le 3 \times 10^51≤N≤3×105
  • X_i \in \{0,1\}Xi​∈{0,1}
    • In other words, X_iXi​ is either 00 or 11.
  • -10^9 \le Y_i \le 10^9−109≤Yi​≤109

Input

The input is given from Standard Input in the following format:

NN
X_1X1​ Y_1Y1​
X_2X2​ Y_2Y2​
\vdots⋮
X_NXN​ Y_NYN​

Output

Print the answer as an integer.


Sample Input 1

5
1 100
1 300
0 -200
1 500
1 300

Sample Output 1

600

The following choices result in a total tastiness of the courses that he eats amounting to 600600, which is the maximum possible.

  • He skips the 11-st course. He now has a healthy stomach.
  • He eats the 22-nd course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to 300300.
  • He eats the 33-rd course. He now has a healthy stomach again, and the total tastiness of the courses that he eats amounts to 100100.
  • He eats the 44-th course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to 600600.
  • He skips the 55-th course. He now has an upset stomach.
  • In the end, he is not dead, so he makes it out of the restaurant alive.

Sample Input 2

4
0 -1
1 -2
0 -3
1 -4

Sample Output 2

0

For this input, it is optimal to eat nothing, in which case the answer is 00.


Sample Input 3

15
1 900000000
0 600000000
1 -300000000
0 -700000000
1 200000000
1 300000000
0 -600000000
1 -900000000
1 600000000
1 -100000000
1 -400000000
0 900000000
0 200000000
1 -500000000
1 900000000

Sample Output 3

4100000000

The answer may not fit into a 3232-bit integer type.

考虑用动态编程(DP)来解决这个问题。我们应该维护什么商店?

直截了当地说,这个问题可以通过填写下面的DP表来解决。dp[过程][高桥状态]当他决定是吃还是跳过前i道菜时,他吃过的菜的最大总味道是j(0……他有一个健康的胃,1……他胃不舒服。)如果你不能解决这个问题,考虑到上面“如何定义DP表”,试着在考虑“转换应该如何”的同时实现。这是DP的一个很好的练习。

#include

using namespace std;

long long dp[300005][2];

int main(){
  long long n;
  cin >> n;
  vector x(n),y(n);
  for(long long i=0;i> x[i] >> y[i];
  }

  for(long long i=0;i<=n;i++){
    dp[i][0]=-4e18;
    dp[i][1]=-4e18;
  }
  dp[0][0]=0;

  for(long long i=0;i

本人因实力比较差只能写到d。有不足大家多多指教。

你可能感兴趣的:(atcoder,abc,c++)