#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(int argc, char** argv)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q, m;
int r=0; // result counting variable
vector<int> car;
vector<int> mid;
// for starting from index 1
car.push_back(0);
scanf("%d %d", &q, &m);
// ์๋์ฐจ ์ฐ๋น ๋ฃ๊ธฐ
for(int i=1; i<=q; i++){
int n;
scanf("%d", &n);
car.push_back(n);
}
sort(car.begin(), car.end());
// ์ค์๊ฐ ๋ฃ๊ธฐ
for(int i=1; i<=m; i++){
int num;
scanf("%d", &num);
mid.push_back(num);
}
for(int i=0; i<m; i++){
auto it = find(car.begin(), car.end(), mid[i]);
// if m_i not in car, print result and continue
if( it == car.end()) {
printf("0\n");
continue;
}
int pos = distance(car.begin(), it);
for(int i=1; i<pos; i++){
for(int j=pos+1; j<=q; j++) r++;
}
printf("%d\n", r);
r=0;
}
return 0;
}
์๊ฐ๋๋ ํ์ด๋ก ๊ตฌํํ ์ฝ๋๋ก, ๋ฐ๋ณต๋ฌธ์ด 3๊ฐ๊ฐ ์ค์ฒฉ๋์ด์ ๋ณต์ก๋๊ฐ ๋์ TC์ ํ๋๋ง ํต๊ณผํ๊ณ ๋๋จธ์ง ๋๊ฐ๋ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(int argc, char** argv)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q, m;
int r=0; // result counting variable
vector<int> car;
vector<int> mid;
// for starting from index 1
car.push_back(0);
scanf("%d %d", &q, &m);
// ์๋์ฐจ ์ฐ๋น ๋ฃ๊ธฐ
for(int i=1; i<=q; i++){
int n;
scanf("%d", &n);
car.push_back(n);
}
sort(car.begin(), car.end());
for(int i=0; i<m; i++){
int mid;
scanf("%d", &mid);
auto it = find(car.begin(), car.end(), mid);
// if m_i not in car, print result and continue
if( it == car.end()) {
printf("0\n");
continue;
}
// find index of mid
int pos = distance(car.begin(), it);
if(pos == 1) {
printf("0\n");
continue;
}
// ๊ฐ์ง์๋ mid๋ณด๋ค ์์ ๊ฐ์*mid๋ณด๋ค ํฐ ๊ฐ์
printf("%d\n", (pos-1)*(q-pos));
}
return 0;
}
TC์ 3๊ฐ ์ค ํ ์ธํธ์์ ํ๋์ ํ ์คํธ์ผ์ด์ค๊ฐ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค..........
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(int argc, char** argv)
{
int q, m;
vector<int> car;
vector<int> mid;
// for starting from index 1
car.push_back(0);
scanf("%d %d", &q, &m);
// ์๋์ฐจ ์ฐ๋น ๋ฃ๊ธฐ
for(int i=1; i<=q; i++){
int n;
scanf("%d", &n);
car.push_back(n);
}
sort(car.begin(), car.end());
for(int i=0; i<m; i++){
int mid;
scanf("%d", &mid);
auto it = find(car.begin(), car.end(), mid);
// if m_i not in car, print result and continue
if( it == car.end()) {
printf("0\n");
continue;
}
// find index of mid
int pos = distance(car.begin(), it);
// ๊ฐ์ง์๋ mid๋ณด๋ค ์์ ๊ฐ์*mid๋ณด๋ค ํฐ ๊ฐ์
printf("%d\n", (pos-1)*(q-pos));
}
return 0;
}
pos๊ฐ ์ฒซ ๋ฒ์งธ์ธ ๊ฒฝ์ฐ์๋ ์ถ๋ ฅ ์ pos-1์ผ๋ก 0์ด ๋๋ฏ๋ก ๋ฐ๋ก ๊ณ ๋ ค ์ ํ๋ ๊ฒ์ผ๋ก ์์ ํ๋ค.
[Softeer] [HSAT 7ํ ์ ๊ธฐ ์ฝ๋ฉ ์ธ์ฆํ๊ฐ ๊ธฐ์ถ] ์์๋๋ก ๋ฐฉ๋ฌธํ๊ธฐ (C++) (0) | 2024.02.25 |
---|---|
[BOJ] 2875 (C++) (1) | 2024.02.25 |
[Softeer] ์ฑ์ ํ๊ท (C++) (0) | 2024.02.15 |
[๋ฐฑ์ค(BOJ)] 2178๋ฒ: ๋ฏธ๋ก ํ์ (C++) (0) | 2023.09.08 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ (C++) (0) | 2023.09.08 |