牛客练习赛43D Tachibana Kanade Loves Sequence

时间:2024-04-18 07:28:52

题目链接:https://ac.nowcoder.com/acm/contest/548/D

题目大意

  略

分析

  贪心,首先小于等于 1 的数肯定不会被选到,因为选择一个数的代价是 1,必须选择大于1的数答案才有可能增加。

  其次,答案一定是在某一个数组所有大于 1 的元素全部选完的情况下得出来的,可以用反证法证明。

  对于 a, b 两个数组,我们可以考虑先全部选中所有大于 1 的元素,然后从那个和比较大的数组中不断扣掉数,在两数组和的大小关系不变的情况下极限扣数,扣完答案就出来了,扣数也是贪心,从小的数开始扣才能扣最多。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int n, a[maxN], b[maxN], c[maxN], d[maxN];
LL sumA, sumB; // 保存对应a[i]*c[i] , b[i]*d[i]的累加和
int cntC, cntD; // 保存 c, d中1的个数 int main(){
INIT();
cin >> n;
Rep(i, n) {
cin >> a[i];
// 先把大于1的全部选中,且只有大于1的才有必要选
if(a[i] > ) {
c[i] = ;
++cntC;
sumA += a[i];
}
}
Rep(i, n) {
cin >> b[i];
if(b[i] > ) {
d[i] = ;
++cntD;
sumB += b[i];
}
} // A指向和比较大的序列数组
// B指向对应构造的序列数组
// cntB指向记录B中有多少个1的变量
int *A, *B;
int* cntB;
// 最小堆
priority_queue< PII, vector< PII >, greater< PII > > pQ;
LL target = abs(sumA - sumB);
if(sumA > sumB) {
A = a;
B = c;
cntB = &cntC;
}
else {
A = b;
B = d;
cntB = &cntD;
}
// 排序太烦了,直接放堆里
Rep(i, n) if(B[i] == ) pQ.push(MP(A[i], i)); // 从小到大取,看看最多能从和比较大的数组中取走多少个
while(!pQ.empty()) {
PII tmp = pQ.top();
pQ.pop(); target -= tmp.ft;
if(target >= ) {
B[tmp.sd] = ;
--*cntB;
}
else break;
} cout << min(sumA, sumB) - cntC - cntD << endl;
Rep(i, n) cout << c[i] << " ";
cout << endl;
Rep(i, n) cout << d[i] << " ";
cout << endl;
return ;
}