The 2022 ICPC Asia Xian Regional Contest
J.Strange Sum
选0~2个数,使和最大,不选就是0.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n; cin >> n;
vector<ll> ve(n);
for (int i = 0; i < n; ++i) {
cin >> ve[i];
}
std::sort(ve.begin(), ve.end());
ll ans = 0;
ans = max({ans, ve[n-1], ve[n-1] + ve[n-2]});
cout << ans ;
return 0;
}
F.Hotel
题意:n个队住宿,单人间a元双人间b元,一个队三个人,要求花钱最少。双人间只能一个队并且同性居住。暴力求解。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define int long long
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, a, b; cin >> n >> a >> b;
int sum = 0;
vector<char> ve(3);
while(n --){
for (int i = 0; i < 3; ++i) {
cin >> ve[i];
}
int res = 0;
std::sort(ve.begin(), ve.end());
if(ve[0] == ve[1] || ve[1] == ve[2]){
res = min({b, a+a}) + min(a, b);
}else{
res = min(a, b)*3;
}
sum += res;
}
cout << sum;
return 0;
}
C. Clone Ranran
题意:完成c个题目,一开始只有一个人,b分钟只能做一道题。还可以花a分钟克隆一个人,克隆人可以和母体做同样的操作,求完成任务最少时间。
思路:一次性把人克隆完,所有人再一起做题。通过枚举克隆的次数然后计算总时间求最小值。
克隆抽象成:1—>2, 2—>4, 4—>8…
每次克隆人数 * 2,可以把总共克隆次数记作 i,克隆出的总人数记作m = pow(2, i)。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t; cin >> t;
while(t --){
ll a, b, c; cin >> a >> b >> c;
ll ans = INF;
for(int i = 0; i <= __lg(c)+1; i++){
ll m = pow(2, i);
ll dd = c/m + ((c%m)?1:0);
ll res = i*a + dd*b;
ans = min(ans, res);
}
cout << ans << '\n';
}
return 0;
}
题意:有n个字符串, 如果一个字符串的所有前缀子字符串和后缀子字符串都在给定的字符串中出现过则合法,求合法字符串的最大长度。
思路:将字符串按长度排序,已经合法的字符串可以成为后续字符串的子串,存入集合,不合法直接扔掉。这样每次只用对比缺少第一个字符的后缀字符串和缺少最后一个字符的前缀字符串是否在集合中, 存在即合法。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
int cmp(string a, string b){
if(a.size()!=b.size()) return a.size() < b.size();
return a<b;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n; cin >> n;
set<string> se;
vector<string> ve;
while(n --){
string x; cin >> x;
ve.push_back(x);
}
int ans = 0;
std::sort(ve.begin(), ve.end(), cmp);
for(auto x: ve){
if(x.size() == 1) se.insert(x), ans = 1;
else{
for (int j = 1; j < x.size(); ++j) {
string ss = x.substr(0, x.size() - 1);
string mm = x.substr(1);
if( se.count(ss) && se.count(mm)){
ans = max(ans, (int) x.size());
se.insert(x);
}
}
}
}
cout << ans;
return 0;
}