A
# include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long LL;
int n;
map<int,int>ma;
void solve()
{
ma.clear();
cin>>n;
int sum=0;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
ma[x]++;
}
for(auto [x,y]:ma)
sum+=y/2;
cout<<sum<<endl;
}
signed main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
}
B
题意为在序列中寻炸两数乘积为k-2 枚举两个数的位置,第二个数用二分查找
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+10;
int a[N];
signed main()
{
int t;
cin>>t;
while(t--)
{
int k;
cin>>k;
for(int i=1;i<=k;i++)cin>>a[i];
sort(a+1,a+k+1);
int y=k-2;
for(int i=1;i<=k;i++)
{
int l=1,r=k;
while(l<r)
{
int mad=(l+r)/2;
if(a[mad]*a[i]>=y)r=mad;
else l=mad+1;
}
if(a[l]*a[i]==y)
{
cout<<a[i]<<" "<<a[l]<<'\n';
break;
}
}
}
}
C
考虑 奇奇奇偶偶偶 会存在一个交替和为奇数,9即为最小的复合整数 n<=8时特判即可
# include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long LL;
int n;
void solve()
{
cin>>n;
if (n < 5)
{
cout << -1 << "\n";
return;
}
else if (n == 7)
{
cout << "1 3 5 4 6 2 7\n";
return;
}
else if (n == 6)
{
cout << "1 3 5 4 2 6\n";
return;
}
else if (n == 5)
{
cout << "1 3 5 4 2\n";
return;
}
else
{
for(int i=1;i<=n;i+=2)
{
if(i!=7)
cout<<i<<" ";
}
cout<<7<<" "<<2<<" ";
for(int i=4;i<=n;i+=2)
cout<<i<<" ";
cout<<endl;
}
}
signed main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
}
D
题目并未限定跳跃次数 我们可以进行任意次跳跃,只要可以跳过所有障碍物即可,贪心的考虑我们肯定要先选取较大的能量值,用优先队列维护能量值并判断每个障碍物是否能通过
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
struct Node
{
int l, r;
}a[N], b[N];
int n,m,l;
int ans,cnt = 1;
priority_queue <int> q;
void solve()
{
while (!q.empty())
q.pop();
ans = 0;
cin >> n >> m >> l;
for (int i = 1; i <= n; i ++)
cin >> a[i].l >> a[i].r;
for (int i = 1; i <= m; i ++)
cin >> b[i].l >> b[i].r;
cnt=1;
int sum=1;
for (int i = 1; i <= n; i ++)
{
while(cnt<=m&&b[cnt].l<a[i].l)
{
q.push(b[cnt].r);
cnt++;
}
while(!q.empty()&&sum<=a[i].r-a[i].l+1)
{
sum+=q.top();
q.pop();
ans++;
}
if(sum<=a[i].r-a[i].l+1)
{
cout<<-1<<endl;
return ;
}
}
cout << ans << endl;
}
signed main ()
{
int T;
cin >> T;
while (T --) solve();
return 0;
}