Mike and distribution CodeForces - 798D (贪心+思维)

时间:2023-03-09 06:00:46
Mike and distribution CodeForces - 798D (贪心+思维)

题目链接

TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料。

题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1~n的不重复数字,

要求这k个数字为下标的对应a和b中的数的和乘以2的值  分别大于a和b 的数组总和。

思路:首先对a进行降序排序,然后输出最大值的下标,随后进行幅度为2的枚举,对排序后的a2~an进行选择性输出下标,(注意,排序的时候用一个新数组开两个变量,一个index,一个v进行排序,可以用结构体,也可以用pair)

那么怎么进行选择性排序呢,由于要求的是这k的数的和*2大于总和,那么如果每两个数选取了一个大的,那么选出来的n/2+1个数的和*2一定大于原数组总和,至于为什么可以自行思考。

时间复杂度为O ( n*log n  )  ,,  非常好的一个贪心题目,思想可以抽象为把n个数分为 n/2的小集合,从每一个小子集中选大的那个数,那么总和*2一定大于全集的和。

附上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
ll a[maxn];
ll b[maxn];
struct node
{
int id;
ll v;
}c[maxn];
bool cmp(node a,node b)
{
return a.v>b.v;
}
int main()
{
scanf("%d",&n);
repd(i,,n)
{
scanf("%lld",&a[i]);
c[i].v=a[i];
c[i].id=i;
}
repd(i,,n)
{
scanf("%lld",&b[i]);
}
sort(c+,c++n,cmp);
printf("%d\n",n/+);
printf("%d ",c[].id );
for(int i=;i<=n;i+=)
{
if(i<n)
{
if(b[(c[i].id)]>b[c[i+].id])
{
printf("%d ", c[i].id);
}else
{
printf("%d ",c[i+].id);
}
}else
{
printf("%d",c[i].id );
}
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}