A. Fair Game
題意:給你n個卡牌(n是偶數),每一個卡片上都有一個數字,有兩個人,這兩個人各選擇一個數字把所有帶有這個數字的卡片拿走,問兩個人能不能把所有的卡片拿走,且兩個人拿到的卡片數相同。
思路:這個題emmm,讀了好久,每一個人只能選擇一個數字然後就直接寫就行了。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MAXN 110000
#define MAXE 5
#define INF 100000000
#define MOD 10001
#define LL long long
#define pi 3.14159
using namespace std;
int arr[MAXN];
int vis[MAXN];
int main() {
std::ios::sync_with_stdio(false);
int n;
cin >> n;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; ++i) {
cin >> arr[i];
vis[arr[i]]++;
}
int cnt = 0;
for (int i = 1; i <= 100; ++i) {
if (vis[i])
cnt++;
}
if (cnt == 2) {
bool flag = false;
for (int i = 1; i <= 100; ++i) {
for (int j = i + 1; j <= 100; ++j) {
if (vis[i] && vis[j] && vis[i] == vis[j]) {
cout << "YES\n";
cout << i << ' ' << j << endl;
flag = true;
break;
}
}
if (flag) {
break;
}
}
if (!flag) {
cout << "NO\n";
}
} else {
cout << "NO\n";
}
return 0;
}
B. Polycarp and Letters
題意:給一個只包含大寫字母和小寫字母的字符串,現在有一個集合,它可以包含字符串中的不同小寫字母的位置,但是要求其任意兩個位置之間在字符串中不能有大寫字母。求集合最大的大小。
思路:直接用一個set來保存小寫字母就可以,注意一下如果字符串當中沒有大寫字母的情況。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MAXN 110000
#define MAXE 5
#define INF 100000000
#define MOD 10001
#define LL long long
#define pi 3.14159
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int n;
string str;
cin >> n >> str;
set<char> Set;
int max_sum = 0;
for (int i = 0; i < n; ++i) {
if (str[i] >= 'a' && str[i] <= 'z') {
Set.insert(str[i]);
} else if (str[i] >= 'A' && str[i] <= 'Z') {
max_sum = max(max_sum, (int) Set.size());
Set.clear();
}
}
max_sum = max(max_sum, (int) Set.size());
cout << max_sum << endl;
return 0;
}
C. Bus
題意:有個x軸,汽車從0出發到x=a,再返回到0,一直做這樣的往返運動,現在規定0->a或a->0都算一次旅行,並且一開始汽車油箱有b升油,每走一個單位要消耗一升油,現在知道x=f位置處有個加油站,每次經過可以選擇加油或不加,加的話可以加滿到b升,要求完成k次旅行,求最少的加油次數,不能完成輸出-1。
思路:這個直接模擬就可以,注意處理一下細節。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MAXN 110000
#define MAXE 5
#define INF 100000000
#define MOD 10001
#define LL long long
#define pi 3.14159
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
LL a, b, f, k;
cin >> a >> b >> f >> k;
LL num = b;
bool flag = true;
LL ans = 0;
for (int i = 1; i <= k; ++i) {
if (i % 2) {
if (num < f) {
flag = false;
break;
} else if (num >= f && num < a) {
num = b - (a - f);
ans++;
} else {
num -= a;
if (i != k && num < (a - f)) {
ans++;
num = b - (a - f);
}
}
} else {
if (num < (a - f)) {
flag = false;
break;
} else if (num >= (a - f) && num < a) {
num = b - f;
ans++;
} else {
num -= a;
if (i != k && num < f) {
ans++;
num = b - f;
}
}
}
if (num < 0) {
flag = false;
break;
}
}
if (flag) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
return 0;
}
D. Make a Permutation!
題意:每次可以任意改變數組中的一個數,要求把數組變成包含
1~n所有數字,求改變數的最小數量和最後的字典序最小的數組。
思路:這個題直接用優先隊列,然後標記一下位置就可以了。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MAXN 210000
#define MAXE 5
#define INF 100000000
#define MOD 10001
#define LL long long
#define pi 3.14159
using namespace std;
int arr[MAXN];
int vis[MAXN];
int main() {
std::ios::sync_with_stdio(false);
int n;
cin >> n;
memset(vis, 0, sizeof(vis));
priority_queue<int, vector<int>, greater<int> > que;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
vis[arr[i]]++;
}
for (int i = 1; i <= n; ++i) {
if (!vis[i])
que.push(i);
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (vis[arr[i]] > 1) {
if (arr[i] < que.top()) {
vis[arr[i]] = -1;
} else {
ans++;
vis[arr[i]]--;
arr[i] = que.top();
que.pop();
vis[arr[i]]++;
}
} else if (vis[arr[i]] == -1) {
ans++;
arr[i] = que.top();
que.pop();
vis[arr[i]]++;
}
}
cout << ans << endl;
for (int i = 1; i <= n; ++i) {
if (i == n) {
cout << arr[i] << endl;
} else {
cout << arr[i] << " ";
}
}
return 0;
}
E. Fire
題意:已知第i個文件:保存所需的時間爲ti,到了時間di則會毀壞,和可得的價值pi。求能保存的文件的最大價值和,以及能保存的文件的編號。
思路:一個01揹包,記錄一下路徑就可以了。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define MAXN 2100
#define MAXE 5
#define INF 100000000
#define MOD 10001
#define LL long long
#define pi 3.14159
using namespace std;
struct Node {
int t;
int d;
int p;
int id;
}box[MAXN];
bool cmp(const Node &x1, const Node &x2) {
if (x1.d == x2.d) {
return x1.t < x2.t;
}
return x1.d < x2.d;
}
int dp[MAXN];
bool path[MAXN][MAXN];
vector<int> vec;
int main() {
std::ios::sync_with_stdio(false);
int n;
cin >> n;
int time = 0;
for (int i = 1; i <= n; ++i) {
cin >> box[i].t >> box[i].d >> box[i].p;
box[i].id = i;
}
memset(dp, 0, sizeof(dp));
memset(path, false, sizeof(path));
sort(box + 1, box + 1 + n, cmp);
int max_sum = 0;
for (int i = 1; i <= n; ++i) {
for (int j = box[i].d - 1; j >= box[i].t; --j) {
if (dp[j] < dp[j - box[i].t] + box[i].p) {
dp[j] = dp[j - box[i].t] + box[i].p;
path[i][j] = true;
}
max_sum = max(max_sum, dp[j]);
}
}
cout << max_sum << endl;
for (int i = 0; i < box[n].d; ++i) {
if (dp[i] == max_sum) {
time = i;
}
}
for (int i = n, j = time; i >= 1; --i) {
if (path[i][j]) {
vec.push_back(box[i].id);
j -= box[i].t;
}
}
cout << vec.size() << endl;
for (int i = (int) vec.size() - 1; i >= 0; --i) {
cout << vec[i] << " ";
}
cout << endl;
return 0;
}
F. Cities Excursions
待補
總結:好久沒打過比賽,一打比賽直接就崩了啊,A題沒讀出來什麼意思然後C題細節沒考慮清楚。
還是太菜了,思路感覺不夠清晰,還是多練習吧。