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;
}