AcWing第62场周赛
依照题意,找到满足小中大三个数并输出其对应下标,由于没有对数的特殊要求,所以任意满足小中大的子数组都可以输出
于是构造一个哈希表 map < long long , int>k ,key 值存放数组的数,value 存放下标
再对原数组进行操作,排序+去重得到新数组,如果这个数组长度小于3,则不满足题意,反之就输出前三个数在哈希表 k 里所存的原下标即可
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
int main()
{
map<long long,int>k;
int n;
cin >> n;
vector<long long> vec;
for (int i = 0; i < n; i ++ )
{
long long s;
cin >> s;
vec.push_back(s);
k[vec[i]]=i;
}
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
int t=vec.size();
if(t<3) cout << "-1 -1 -1";
else
{
cout << k[vec[0]]+1<<" "<<k[vec[1]]+1<<" "<<k[vec[2]]+1;
}
return 0;
}
题目很长,考查语文水平,以至于我完全看不懂
所以此处思路完全取自该题底下第一题解(真神仙大佬👍)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int delta = 1;
int n, m, t, a[N], f[N];
int main(){
scanf("%d%d", &n, &m);
while (m -- ){
scanf("%d", &t);
f[++ a[t]] ++;
if (f[delta] == n){
putchar('1');
delta ++;
}
else putchar('0');
}
return 0;
}
力扣第304场周赛
使数组中所有元素都等于0
这题没什么好说的,对数组排序,去重,然后再删除0,最后输出数组长度即可
class Solution {
public:
int minimumOperations(vector<int>& nums) {
sort(nums.begin(),nums.end());
nums.erase(unique(nums.begin(),nums.end()),nums.end());
for(auto it=nums.begin();it!=nums.end();it++)
{
if(*it==0)
{
nums.erase(it);
it--;
}
}
return nums.size();
}
};
分组的最大数量
题目等价于 求使 1+2+...+x <= n 成立的最大 x
也就是 (x+1)*x/2 <= n < (x+2)*(x+1)/2
class Solution {
public:
int maximumGroups(vector<int>& grades) {
int t = 2 * grades.size(), g = sqrt(t);
return g * (g + 1) > t ? g - 1 : g;
}
};
也是语文题,把题目看懂就行