https://codeforces.com/contest/1985
较有价值的为DEF题
A:
# include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
string s1,s2;
cin>>s1>>s2;
swap(s1[0],s2[0]);
cout<<s1<<" "<<s2<<endl;
}
return 0;
}
B:
#include <bits/stdc++.h>
using namespace std;
int main(){
int t; cin >> t;
while(t--){
int n; cin >> n;
cout << (n == 3 ? 3 : 2) << endl;
}
}
C 按照定义判断即可
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int T;
cin>>T;
while(T--)
{
int a=0;
int n;
cin>>n;
ll sum=0,mx=0;
for(int i=1;i<=n;i++)
{
ll x;
cin>>x;
sum+=x;
mx=max(mx,x);
if(mx==sum-mx)
a++;
}
cout<<a<<endl;
}
}
D 寻找横向最长与纵向最长的,答案位于其交点
int T;
cin>>T;
while(T--)
{
int n,m;
cin>>n>>m;
vector<vector<char>> arr(n, vector<char>(m));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>arr[i][j];
}
int ans=0,mx_h=0,mx_s=0,k1,k2;
for(int i=0;i<n;i++)
{
ans=0;
for(int j=0;j<m;j++)
{
if(arr[i][j]=='#')
ans++;
}
if(ans>mx_h)
{
k1=i;
mx_h=ans;
}
}
for(int i=0;i<m;i++)
{
ans=0;
for(int j=0;j<n;j++)
{
if(arr[j][i]=='#')
ans++;
}
if(ans>mx_s)
{
k2=i;
mx_s=ans;
}
}
// cout<<k1<<mx_h<<" "<<k2<<mx_s;
int x,y;
for(int j=0;j<m;j++)
{
if(arr[k1][j]=='#')
{
x=j;
break;
}
}
for(int j=0;j<n;j++)
{
if(arr[j][k2]=='#')
{
y=j;
break;
}
}
cout<<y+(mx_s-1)/2+1<<" "<<x+(mx_h-1)/2+1;
cout<<endl;
答案思路较为精巧 交点位于最上与最下中点处
pair<int, int> top = {INF, INF}, bottom = {-INF, -INF};
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(arr[i][j] == '#'){
top = min(top, {i, j});
bottom = max(bottom, {i, j});
}
}
}
E :答案较大超出int,数据较小考虑枚举两层边长,剩下一个需判断是否满足条件 最后用组合数枚举答案更新最大值
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int T;
cin>>T;
while(T--)
{
ll x,y,z,k;
cin>>x>>y>>z>>k;
ll ans=0;
for(ll i=1;i<=x;i++)
{
for(ll j=1;j<=y;j++)
{
ll c=k/(i*j);
if(i*j*c!=k)
continue;
if(c<=z) ans=max((x-i+1)*(y-j+1)*(z-c+1),ans);
}
}
cout<<ans<<endl;
}
return 0;
}
F:题目要求最小回合数,考虑二分,判断边界条件回合越多射死怪兽几率越大,所以满足条件时r=mid;注意二分函数中攻击和会超出longlong 一旦大于h时就返回true 否则会被卡
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+10;
ll a[N],c[N];
bool check(ll mid,ll n,ll h)
{
ll sum=0;
for(int i=1;i<=n;i++)
{
sum+=a[i]+((mid-1)/c[i])*a[i];
if(sum>=h)(注意此处)
return true;
}
return false;
}
int main()
{
int T;
cin>>T;
while(T--)
{
ll h,n;
cin>>h>>n;
ll ans=0;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>c[i];
ll l=1,r=1e12;
while(l<r)
{
ll mid=l+r>>1;
if(check(mid,n,h))r=mid;
else l=mid+1;
}
cout<<l<<endl;
}
}