(1条未读私信) 2023四川大学“腾讯杯”新生赛(同步赛)_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com)
A:报数
补充知识点
1.lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。
在从小到大的排序数组中,
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
在从大到小的排序数组中,重载lower_bound()和upper_bound()
lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
原文链接:https://blog.csdn.net/qq_40160605/article/details/80150252
2.1ll什么时候需要用到
超过了int类型的范围时,这时候我们就需要强制类型转换来消除错误。
int a = 1LL * 100000 * 100000 / 1000;
1LL会在运算时把后面的临时数据扩容成long long类型,再在赋值给左边时转回int类型。
题意:
有 nnn 个数 a1,a2,…,ana_1, a_2, \dots, a_na1,a2,…,an,你需要找到一个集合 SSS,使得 SSS 中严格大于 SSS 的平均数的数字个数尽量多。输出最多的个数。
注意:这里的集合是可重集,数字可以重复。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
ll a[N];
ll n, sum, ans;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (ll i = 0; i < n; i++){
cin >> a[i];
sum += a[i];//计算总和
}
sort(a, a + n);
double aver = sum * 1.0 / n;
for (ll i = n - 1; i >= 0; i--){
ll x = upper_bound(a, a + i, aver) - a;//找到第一个大于后的数组下标,下标从0开始的
ans = max(ans, i + 1 - x); //i+1表示当前的数组内的个数
sum -= a[i];
aver = sum * 1.0 / (i)*1.0;
}
cout << ans << endl;
return 0;
}
B题:糖果
//只要找出两个数相加的和相同就行
#include<bits/stdc++.h>
using namespace std;
const int N=2e7+10;
typedef long long ll;
ll a[N],b[N];//用b数组来记录总和的值之前是否出现
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n;
cin>>n;
for(ll i=0;i<n;i++)
cin>>a[i];
for(ll i=0,j=n-1;i<n-1;i++){
while(i<j){
ll k=a[i]+a[j];
if(b[k]!=0){
cout<<"YES";
return 0;
}
b[k]=1;//记1表示出现过
j--;
}
j=n-1;//一直要比较到末尾
}
cout<<"NO";
return 0;
}
L:小饭的数学作业
转化:就是求正多边形的内切圆边长
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
double PI = 3.141592653589793238;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t,n;
cin>>t;
while(t--){
cin>>n;
printf("%.9lf\n", 1 / tan(PI / 2 / n));
}
return 0;
}