AtCoder Beginner Contest 374
A - Takahashi san 2
思路
取末三位的子串判断即可;
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef priority_queue<int> PQ;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
string s;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>s;
int l=s.size();
if(s.substr(l-3)=="san")cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
B - Unvarnished Report
思路
判断 \(S\) 和 \(T\) 相同输出“Yes”,否则找出不同位置即可(注意别越界);
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef priority_queue<int> PQ;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
string s,t;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>s>>t;
if(s==t){
cout<<"0"<<endl;
return 0;
}
int ls=s.size();
int lt=t.size();
if(ls>=lt){
s+="X";
for(int i=0;i<ls;i++){
if(s[i]!=t[i]){
cout<<i+1<<endl;
return 0;
}
}
}
else{
t+="X";
for(int i=0;i<lt;i++){
if(s[i]!=t[i]){
cout<<i+1<<endl;
return 0;
}
}
}
return 0;
}
C - Separated Lunch
思路
\(N\) 的范围较小,直接DFS即可;
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef priority_queue<int> PQ;
const int N = 25, MAX = 1e9, INF = -1e9;
int n;
int a[N];
int ans=MAX;
void dfs(int x,int l,int r){
if(x>n){
ans=min(ans,max(l,r));
return ;
}
dfs(x+1,l+a[x],r);
dfs(x+1,l,r+a[x]);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
dfs(1,0,0);
cout<<ans<<endl;
return 0;
}
D - Laser Marking
思路
1.把{0,0}点看作第0个线段,长度为0;
2.生成1~n的全排列,即线段的打印顺序;
3.对于每个排列dfs时间,key表示顺序打印和逆序打印;
4.对于每个线段,加上打印时间,从线段末端点转移到下一个端点(dfs);
时间复杂度大致为:\(O(n! 2^n)\)
AC代码
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define pb push_back
#define bs bitset
using namespace std;
typedef pair<double, double> PDD;
const int N = 8;
const double INF = 1e9;
int n, t, ss;
vector<PDD> s(N);
vector<PDD> e(N);
double len[N];
double ans = INF;
vector<int> ord(N);
void dfs(int x, bool key, double l) {
l += len[ord[x]] / t;
if (x == n) {
ans = min(ans, l);
//cout<<l<<endl;
return;
}
double x0, y0;
if (key) {
x0 = e[ord[x]].first;
y0 = e[ord[x]].second;
} else {
x0 = s[ord[x]].first;
y0 = s[ord[x]].second;
}
double l1 = sqrtl((x0 - s[ord[x + 1]].first) * (x0 - s[ord[x + 1]].first) +
(y0 - s[ord[x + 1]].second) * (y0 - s[ord[x + 1]].second))/ss ;
double l0 = sqrtl((x0 - e[ord[x + 1]].first) * (x0 - e[ord[x + 1]].first) +
(y0 - e[ord[x + 1]].second) * (y0 - e[ord[x + 1]].second))/ss;
dfs(x + 1, 1, l + l1);
dfs(x + 1, 0, l + l0);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
cin >> ss >> t;
s[0]={0,0};e[0]={0,0};len[0]=0.0;
for (int i = 1; i <= n; i++) {
cin >> s[i].first >> s[i].second;
cin >> e[i].first >> e[i].second;
len[i] = sqrtl((s[i].first - e[i].first) * (s[i].first - e[i].first) +
(s[i].second - e[i].second) * (s[i].second - e[i].second));
}
for (int i = 1; i <= n; i++) {
ord[i] = i;
}
do {
dfs(0, true, 0);
} while (next_permutation(ord.begin()+1, ord.begin() + 1 + n));
cout << fixed << setprecision(12) << ans << endl;
return 0;
}
E - Sensor Optimization Dilemma 2
思路
目标是寻找 \(W\) 最小值中的最大值,考虑二分;二分模板中我们只需要编写 \(check()\) 函数即可,我们需要检查当前花费条件下是否可以达到 \(W\) 的生产效率,由于 \(N\) 较小,考虑枚举,每次优先选择性价比较高的机器,再逐步替换,找到最小花费,与 \(x\) 比较返回,时间复杂度大致为\(O(nlogx)\);
AC代码
#include<bits/stdc++.h>
#define endl '\n'
#define int int long long
#define pb push_back
#define bs bitset
using namespace std;
typedef pair<char,int> PCI;
typedef pair<int,int> PII;
typedef priority_queue<int> PQ;
const int N = 2e5+10, MAX = 1e9, INF = -1e9;
int n,x;
int A[N], B[N], P[N], Q[N];
bool check(int w){
int sum=0;
for(int i=1;i<=n;i++){
int a = A[i], b = B[i], p = P[i], q = Q[i];
if(a * q < b * p) {
swap(a,b);swap(p,q);
}
int res = 1e18;
for(int j = 0;j < 100;j ++) {
int v = j * q;
int need = w - j * b;
v += (need + a - 1) / a * p;
res = min(res, v);
}
sum += res;
}
return sum<=x;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> x;
for (int i = 1; i <= n; i++)
{
cin >> A[i] >> P[i] >> B[i] >> Q[i];
}
int l = 0, r = 1e9;
while (l < r)
{
int mid = l + r + 1 >> 1;
if (check(mid))
{
l = mid;
}
else
r = mid - 1;
}
cout << l << endl;
return 0;
}