思路:
F1的核心思想:由例子可以推出,每一项减去第一项的结果 等于 原式子减去第一项乘以2的n - 1次方,所以先把2的n次方的值进行预处理,至于要满足题目要求的贪心策略可以直接用优先队列去处理,因为F1题目数据范围不大,考虑直接暴力做,结果则是每次加上多出来的(x)第一项* 2^(k - 1),最后一次操作后加上最后变成的数组a[1]即可
F2的核心思想:最后面的数字是很难被前面的数字影响到的,最后面的数字越打,被前面数字影响到的概率越低,呈2的k次方关系,处理出a[i](i<64)的情况即可,用01关系表示当前位置的行为关系,其他的与F1变化不大
代码:
F1
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define int long long
constexpr int N = 3e3 + 5, mod = 1e9 + 7;
int a[N], b[N], p[N];
int n, m, cnt;
struct Node
{
int x, y;
bool operator < (const Node &b) const
{
return a[x] + a[y] > a[b.x] + a[b.y];
}
};
void solve()
{
cin >> n;
p[0] = 1;
for(int i = 1; i <= n; ++ i)p[i] = p[i - 1] * 2 % mod;
for(int i = 1; i <= n; ++ i)cin >> a[i];
sort(a + 1, a + 1 + n);
cnt = 0;
for(int i = n; i >= 2; -- i)
{
priority_queue<Node>q;
for(int j = 1; j < i; ++ j)q.push({j, j + 1});
for(int j = 1; j < i; ++ j)
{
auto [x, y] = q.top();
q.pop();
b[j] = a[x] + a[y];
q.push({x, y + 1});
}
cnt = (cnt + b[1] * p[i - 2]) % mod;
for(int j = i - 1; j; -- j)b[j] -= b[1];
for(int j = 1; j < i; ++ j)a[j] = b[j] % mod;
}
cout << (a[1] + cnt) % mod << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
// int T;
// for (cin >> T; T -- ; )
solve();
}F2
// LUOGU_RID: 123266099
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define int long long
constexpr int N = 1e6 + 5, mod = 1e9 + 7;
int a[2][N], b[N], p[N];
int n, m, cnt, bas;
int siz, siz2, pt2, ans;
bool sw;
struct Node
{
int x, y;
bool operator < (const Node &b) const
{
return a[sw][x] + a[sw][y] > a[sw][b.x] + a[sw][b.y];
}
};
void solve()
{
cin >> n;
for(int i = 1; i <= n; ++ i)cin >> a[0][i];
sort(a[0] + 1, a[0] + 1 + n);
b[n] = a[sw][1], bas = 1;
for(int i = n; i > 1; -- i)
{
siz = min((LL)62, i);
siz2 = min((LL)62, i - 1);
pt2 = 0;
priority_queue<Node>q;
for(int j = 1; j <= siz; ++ j)
q.push({j, j + 1});
while(pt2 < siz2)
{
auto [x, y] = q.top();
q.pop();
if(y == siz + 1)continue;
a[sw ^ 1][ ++ pt2] = a[sw][x] + a[sw][y] - 2 * b[i];
q.push({x, y + 1});
}
sw ^= 1;
b[i - 1] = a[sw][1];
}
for(int i = 1; i <= n; ++ i)ans = (ans + b[i] * bas) % mod, bas = bas * 2 % mod;
cout << ans << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
// int T;
// for (cin >> T; T -- ; )
solve();
}