2024牛客寒假算法基础集训营2
A.
模拟
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
void solve() {
int n;cin>>n;
while(n--){
int a,b,c;cin>>a>>b>>c;
int ans=0;
if(a==150)ans+=1;
else if(a==200)ans+=2;
if(b==34||b==36||b==38||b==40)ans+=1;
else if(b==45)ans+=2;
if(c==34||c==36||c==38||c==40)ans+=1;
else if(c==45)ans+=2;
cout<<ans<<'\n';
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int left=1;
//cin>>left;
while(left--){
solve();
}
}
B.
把防猫网的位置用小数来表示
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
void solve() {
int n,m,k;cin>>n>>m>>k;
vector<vector<int>>g(n+5,vector<int>(m+5,0));
for(int i=1,x,y;i<=k;i++){
cin>>x>>y;
g[x][y]=1;
}
int ans=0;
map<pair<double,double>,int>mp;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(g[i][j]){
double xx=(i-1+i)*1.0/2,yy=j*1.0;
if(!mp[{xx,yy}]) {
mp[{xx, yy}] = 1;
ans++;
}
xx=(i+1+i)*1.0/2,yy=j*1.0;
if(!mp[{xx,yy}]) {
mp[{xx, yy}] = 1;
ans++;
}
xx=i*1.0,yy=(j-1+j)*1.0/2;
if(!mp[{xx,yy}]) {
mp[{xx, yy}] = 1;
ans++;
}
xx=i*1.0,yy=(j+1+j)*1.0/2;
if(!mp[{xx,yy}]) {
mp[{xx, yy}] = 1;
ans++;
}
}
}
}
cout<<ans<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int left=1;
//cin>>left;
while(left--){
solve();
}
}
E.
把1,2的位置分别存下来
从后往前遍历,取较小的位置,后面的全部去掉
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
void solve() {
int n;cin>>n;
vector<int>pos_1,pos_2;
for(int i=1,x;i<=n;i++){
cin>>x;
if(x==1)pos_1.push_back(i);
else pos_2.push_back(i);
}
if(pos_2.size()==0){
cout<<pos_1.size()<<'\n';return ;
}
if(pos_1.size()==0){
cout<<pos_2.size()<<'\n';return ;
}
int ans=0;
while(pos_1.size()&&pos_2.size()){
if(pos_1.back()<pos_2.back()){
ans++;
while(pos_2.size()&&pos_1.back()<pos_2.back())pos_2.pop_back();
pos_1.pop_back();
}
if(pos_2.back()<pos_1.back()){
ans++;
while(pos_1.size()&&pos_2.back()<pos_1.back())pos_1.pop_back();
pos_2.pop_back();
}
}
if(pos_2.size())ans+=pos_2.size();
if(pos_1.size())ans+=pos_1.size();
cout<<ans<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int left=1;
cin>>left;
while(left--){
solve();
}
}
I.
可以发现边权的计算方式就是两倍的较大值
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
void solve() {
int n;cin>>n;
vector<int>a(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a.begin()+1,a.end());
int ans=0;
for(int i=2;i<=n;i++){
ans+=(i-1)*2*a[i]*2;
}
cout<<ans<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int left=1;
cin>>left;
while(left--){
solve();
}
}
J.
可以发现边权计算方式就是两倍的较小值或最小值的4倍
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
void solve() {
int n;cin>>n;
vector<int>v(n+1);
int mi=0x3f3f3f3f;
for(int i=1;i<=n;i++){
cin>>v[i];
}
sort(v.begin()+1,v.end());
mi=v[1];
for(int i=1;i<=n;i++){
if(v[i]==mi)v[i]*=2;
else v[i]=min(2*v[i],4*mi);
}
vector<int>pre(n+1,0);
for(int i=1;i<=n;i++)pre[i]=pre[i-1]+v[i];
int ans=0;
for(int i=2;i<=n;i++){
ans+=pre[i-1];
}
ans*=2;
cout<<ans<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int left=1;
cin>>left;
while(left--){
solve();
}
}