本文主要是介绍atcoder ABC 359-B题详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
atcoder ABC 359-B题详解
Problem Statement
There are 2N people standing in a row, and the person at the i-th position from the left is wearing clothes of color Ai. Here, the clothes have N colors from 1 to N, and exactly two people are wearing clothes of each color.
Find how many of the integers i=1,2,…,N satisfy the following condition:
There is exactly one person between the two people wearing clothes of color i.
Constraints
2≤N≤100
1≤Ai≤N
Each integer from 1 through N appears exactly twice in A.
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A1 A2 … A2N
Output
Print the answer.
Sample Input 1
3
1 2 1 3 2 3
Sample Output 1
2
There are two values of i that satisfy the condition: 1 and 3.
In fact, the people wearing clothes of color 1 are at the 1st and 3rd positions from the left, with exactly one person in between.
Sample Input 2
2
1 1 2 2
Sample Output 2
0
There may be no i that satisfies the condition.
Sample Input 3
4
4 3 2 3 2 1 4 1
Sample Output 3
3
思路分析:
本题需要求解中间的那个数边上的两个数一样的个数,可以开vector直接来求解。
code:
#include <iostream>
#include <vector>
using namespace std;
vector<int>v;
int n;
int a;
int ans=0;
int main(){cin>>n;for(int i=0;i<2*n;i++){cin>>a;v.push_back(a);}for(int i=0;i<2*n;i++){if(v[i]==v[i+2]){ans++;}}cout<<ans;
}
这篇关于atcoder ABC 359-B题详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!