跳到正文

个人记录

2026/3/28总结

cyz20121112约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年3月28日
更新
2026年3月28日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

第一题:

错因:读题时条件读反了

C++
#include<bits/stdc++.h>
using namespace std;
int a=1,b=2,c=3,d=4;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	string x;
	cin>>x;
	for(int i=0;i<x.size();i++){
		if(x[i]=='V'){
			swap(a,b);
			swap(c,d);
		}else if(x[i]=='H'){
			swap(a,c);
			swap(b,d);
		}
	}
	cout<<a<<" "<<b<<endl<<c<<" "<<d;
	return 0;
}

第二题:

错因:没考虑相等的情况

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,a[60005];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>a[i];
	}
	sort(a+1,a+1+m);
	for(int i=1;i<=m;i++){
		n-=a[i];
		if(n<0){//不够了就输出
			cout<<i-1;
			return 0;
		}
	}
	cout<<m;
	return 0;
}

第四题:

错因:没有想到二分的思路

C++
#include<bits/stdc++.h>
using namespace std;
long long n,ll,q,u,a[200005];
bool cmp(int x,int y){
	return x>y;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>ll>>u;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+1+n,cmp);//从大到小排列
    a[0]=ll;
	for(int i=1;i<=n;i++){//路程前缀和
		a[i]+=a[i-1];
	}
	int qq;
	cin>>qq;
	while(qq--){
		cin>>q;
		int l=-1,r=n+1;
		while(l+1<r){//二分求最优解
			int mid=(l+r)/2;
			if(a[mid]>q*u){
				r=mid;
			}else{
				l=mid;
			}
		}
        if(r==n+1){//答案不动就是无解
            cout<<-1<<endl;
        }else{
            cout<<r<<endl;
        }
	}
	return 0;
}
原文