CF253A Boys and Girls 題解
阿新 • • 發佈:2021-12-21
CF253A Boys and Girls 題解
Content
有 \(n\) 個男生、\(m\) 個女生坐在一排,請求出這樣一種方案,使得相鄰兩個座位之間的人的性別不同的次數最多。
資料範圍:\(1\leqslant n,m\leqslant 100\)。
Solution
這題不難,就是先把能男女組合的儘量組合了,然後剩餘有多的直接丟在後面不管(或者也可以先把多的求出來然後丟在前面,但是不如本方法方便,讀者可以親自嘗試),但是有坑點:
- 這題目要開檔案輸入輸出!所以千萬不要忘了 \(\texttt{freopen}\)。
- 如果是女生多,則兩兩組合時先讓女生在前,男生在後,這樣才能儘可能地滿足題目要求。如果是男生多,則兩兩組合時先讓男生在前,女生在後。
跳過這些坑點,勝利其實就在眼前了。
Code
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; int n, m, minx; int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); scanf("%d%d", &n, &m); minx = min(n, m); for(int i = 1; i <= minx; ++i) printf(minx == n ? "GB" : "BG"); n -= minx, m -= minx; if(n) for(int i = 1; i <= n; ++i) printf("B"); else for(int i = 1; i <= m; ++i) printf("G"); return 0; }