[BZOJ3900]交换茸角 题解
题目地址:BZOJ:Problem 3900. — 交换茸角 题目描述 动物园 …
May all the beauty be blessed.
题目地址:洛谷:【P3646】[APIO2015]巴厘岛的雕塑 – 洛谷、BZOJ:Problem 4069. — [Apio2015]巴厘岛的雕塑
印尼巴厘岛的公路上有许多的雕塑,我们来关注它的一条主干道。
在这条主干道上一共有 N 座雕塑,为方便起见,我们把这些雕塑从 1 到 N 连续地进行标号,其中第 i 座雕塑的年龄是 Yi 年。为了使这条路的环境更加优美,政府想把这些雕塑分成若干组,并通过在组与组之间种上一些树,来吸引更多的游客来巴厘岛。
下面是将雕塑分组的规则:
这些雕塑必须被分为恰好 X 组,其中 A< = X< = B,每组必须含有至少一个雕塑,每个雕塑也必须属于且只属于一个组。同一组中的所有雕塑必须位于这条路的连续一段上。
当雕塑被分好组后,对于每个组,我们首先计算出该组所有雕塑的年龄和。
计算所有年龄和按位取或的结果。我们这个值把称为这一分组的最终优美度。
请问政府能得到的最小的最终优美度是多少?
输入格式:
输入的第一行包含三个用空格分开的整数 N,A,B。
第二行包含 N 个用空格分开的整数 Y1,Y2,…,YN。
输出格式:
输出一行一个数,表示最小的最终优美度。
输入样例#1:
6 1 3 8 1 2 1 5 4
输出样例#1:
11
子任务 1 (9 分),1< = N< = 20,1< = A< = B< = N,0< = Yi< = 1000000000
子任务 2 (16 分),1< = N< = 50,1< = A< = B< = min{20,N},0< = Yi< = 10
子任务 3 (21 分),1< = N< = 100,A=1,1< = B< = N,0< = Yi< = 20
子任务 4 (25 分),1< = N< = 100,1< = A< = B< = N,0< = Yi< = 1000000000
子任务 5 (29 分),1< = N< = 2000,A=1,1< = B< = N,0< = Yi< = 1000000000
首先既然是最小,又涉及位运算,应该是对二进制位进行DP没跑了。我们从高位往低位来做DP。
其实要解决的问题是“这一位上能不能填0”,那么我们设计DP状态dp[i][j]表示前i个划分成j段,这一位之前的高位与目前最优解相同,这一位能不能填0。枚举k,从dp[k][j – 1]这个状态来进行转移,如果j~i这一段的和能确保目前最优解(即((sum >> pos) | ans) == ans
)且这种情况下当前位能填0(即!(sum & (1ll << (pos - 1)))
),那么可以转移。
每一位的DP结束后,我们从dp[n][A]到dp[n][B]找是否有DP状态表示解可行,有的话这一位就可以为0,否则必须为1。
那么我们可以知道这个的复杂度是O(n^3 \log (\sum y_i))的,对于子任务5的n范围有些吃紧。
接下来,为了能过子任务5,我们得有一点面向子任务编程的想法。子任务5的特点是A=1,也就是说上面所说的下界限制是没有了的。那么我们考虑直接降低DP状态维度来优化复杂度。我们让dp[i]表示前i个符合目前最优解且最后一位能填0最少的划分段数。我们可以枚举k,从dp[k]像上面类似地往后转移,最后判断的依据是dp[n]是否超过B,超过这一位就必须得为1了。因为不需要枚举j了,复杂度降为O(n^2 \log (\sum y_i))。
// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
}
const int MAXN = 2005;
int n, a, b;
LL y[MAXN];
int f[MAXN][MAXN], g[MAXN];
int main() {
n = readint();
a = readint();
b = readint();
for(int i = 1; i <= n; i++) {
y[i] = y[i - 1] + readint();
}
int len = 0;
LL t = y[n];
while(t) {
len++;
t >>= 1;
}
LL ans = 0;
if(a == 1) {
for(int pos = len; pos; pos--) {
memset(g, 0x3f, sizeof(g));
g[0] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 0; j < i; j++) {
if(g[j] < b) {
LL sum = y[i] - y[j];
if(((sum >> pos) | ans) == ans && !(sum & (1ll << (pos - 1)))) {
g[i] = std::min(g[i], g[j] + 1);
}
}
}
}
ans <<= 1;
if(g[n] > b) ans++;
}
} else {
for(int pos = len; pos; pos--) {
memset(f, 0, sizeof(f));
f[0][0] = 1;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
for(int k = 0; k < i; k++) {
if(f[k][j - 1]) {
LL sum = y[i] - y[k];
if(((sum >> pos) | ans) == ans && !(sum & (1ll << (pos - 1)))) {
f[i][j] = 1;
}
}
}
}
}
ans <<= 1;
bool success = false;
for(int i = a; i <= b; i++) {
if(f[n][i]) {
success = true;
break;
}
}
if(!success) ans++;
}
}
printf("%lld", ans);
return 0;
}
题目地址:HDUOJ:Problem – 4352
#define xhxj (Xin Hang senior sister(学姐))
If you do not know xhxj, then carefully reading the entire description is very important.
As the strongest fighting force in UESTC, xhxj grew up in Jintang, a border town of Chengdu.
Like many god cattles, xhxj has a legendary life:
2010.04, had not yet begun to learn the algorithm, xhxj won the second prize in the university contest. And in this fall, xhxj got one gold medal and one silver medal of regional contest. In the next year’s summer, xhxj was invited to Beijing to attend the astar onsite. A few months later, xhxj got two gold medals and was also qualified for world’s final. However, xhxj was defeated by zhymaoiing in the competition that determined who would go to the world’s final(there is only one team for every university to send to the world’s final) .Now, xhxj is much more stronger than ever,and she will go to the dreaming country to compete in TCO final.
As you see, xhxj always keeps a short hair(reasons unknown), so she looks like a boy( I will not tell you she is actually a lovely girl), wearing yellow T-shirt. When she is not talking, her round face feels very lovely, attracting others to touch her face gently。Unlike God Luo’s, another UESTC god cattle who has cool and noble charm, xhxj is quite approachable, lively, clever. On the other hand,xhxj is very sensitive to the beautiful properties, “this problem has a very good properties”,she always said that after ACing a very hard problem. She often helps in finding solutions, even though she is not good at the problems of that type.
Xhxj loves many games such as,Dota, ocg, mahjong, Starcraft 2, Diablo 3.etc,if you can beat her in any game above, you will get her admire and become a god cattle. She is very concerned with her younger schoolfellows, if she saw someone on a DOTA platform, she would say: “Why do not you go to improve your programming skill”. When she receives sincere compliments from others, she would say modestly: “Please don’t flatter at me.(Please don’t black).”As she will graduate after no more than one year, xhxj also wants to fall in love. However, the man in her dreams has not yet appeared, so she now prefers girls.
Another hobby of xhxj is yy(speculation) some magical problems to discover the special properties. For example, when she see a number, she would think whether the digits of a number are strictly increasing. If you consider the number as a string and can get a longest strictly increasing subsequence the length of which is equal to k, the power of this number is k.. It is very simple to determine a single number’s power, but is it also easy to solve this problem with the numbers within an interval? xhxj has a little tired,she want a god cattle to help her solve this problem,the problem is: Determine how many numbers have the power value k in [L,R] in O(1)time.
For the first one to solve this problem,xhxj will upgrade 20 favorability rate。
定义一个数的内部LIS长度为这个数拆成一个个数字这个序列的LIS长度,给出区间[l, r],求区间内内部LIS长度为k的数的个数。
输入格式:
First a integer T(T<=10000),then T lines follow, every line has three positive integer L,R,K.(0<L<=R<2^63-1 and 1<=K<=10).
输出格式:
For each query, print “Case #t: ans” in a line, in which t is the number of the test case starting from 1 and ans is the answer.
输入样例#1:
1 123 321 2
输出样例#1:
Case #1: 139
思路参考HDU 4352 XHXJ’s LIS(数位dp&状态压缩) – CSDN博客,感谢原作者。
首先这个题我们可以想到数位DP(人生第一个数位DP题),但是数位DP怎么来处理LIS是个很棘手的问题。
我们考虑O(n \log n)求LIS的方法,是在外部维护了一个当前LIS的数组,我们如果在这里用这个数组,会发现LIS的长度不会大于10,LIS的数组显然是可以状态压缩做的。这样,如果进行一些预处理,求LIS的复杂度可以是O(1)的。
设计状态dp[len][S][k]为枚举到长度为len,S为目前LIS数组里的元素情况,要求的LIS长度为k的数字数量。考虑采取记忆化搜索,我们可以向后枚举当前位置可以填充哪个数字。当填充完毕后,检查LIS是否等于k,并返回,这是整个搜索的终点。由于我们询问区间,势必会对答案产生一定的限制,如果当前搜索受到限制,那么搜索的结果不应该被保存。还要特别处理一下前导0之类的。
实现细节参考代码注释吧。
// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF
: *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
}
LL dp[20][1 << 10][11];
int dis[20], cnt[1 << 10], nxt[10][1 << 10];
// 这里是计算每个状态后插入一个数的状态
inline int calnxt(int s, int num) {
for(int i = num; i <= 9; i++) {
if(s & (1 << i)) {
return (s ^ (1 << i)) | (1 << num);
}
}
return s | (1 << num);
}
inline void init() {
memset(dp, -1, sizeof(dp));
// 把每个状态的LIS长度处理出来
for(int i = 0; i < (1 << 10); i++) {
cnt[i] = 0;
for(int j = 0; j < 10; j++) {
if(i & (1 << j)) cnt[i]++;
nxt[j][i] = calnxt(i, j);
}
}
}
// s表示状态,lim表示当前是否受限,zero表示当前是否在前导0里
inline LL dfs(int k, int len, int s, bool lim, bool zero) {
if(len < 0) return cnt[s] == k;
if(!lim && dp[len][s][k] != -1) return dp[len][s][k];
LL res = 0;
int limm = lim ? dis[len] : 9;
for(int i = 0; i <= limm; i++) {
// zero产生的影响是状态不会发生改变
res += dfs(k, len - 1, (zero && i == 0) ? s : nxt[i][s], lim && i == limm,
zero && i == 0);
}
if(!lim) dp[len][s][k] = res;
return res;
}
inline LL work(LL n, LL k) {
// 先处理出来限制条件
int pos = 0;
while(n) {
dis[pos++] = n % 10;
n /= 10;
}
return dfs(k, pos - 1, 0, true, true);
}
int T;
LL l, r, k;
int main() {
T = readint();
init();
for(int ii = 1; ii <= T; ii++) {
l = readint();
r = readint();
k = readint();
// 这里我们选择把0~l-1和0~r的都求出来然后减掉
printf("Case #%d: %lld\n", ii, work(r, k) - work(l - 1, k));
}
return 0;
}
题目地址:ZCMU:Power Eggs 题目描述 Benedict bought K i …
题目地址:洛谷:【P3648】[APIO2014]序列分割 – 洛谷、BZOJ:Problem 3675. — [Apio2014]序列分割
你正在玩一个关于长度为n的非负整数序列的游戏。这个游戏中你需要把序列分成k+1个非空的块。为了得到k+1块,你需要重复下面的操作k次:
选择一个有超过一个元素的块(初始时你只有一块,即整个序列)
选择两个相邻元素把这个块从中间分开,得到两个非空的块。
每次操作后你将获得那两个新产生的块的元素和的乘积的分数。你想要最大化最后的总得分。
输入格式:
第一行包含两个整数n和k。保证k+1≤n。
第二行包含n个非负整数a1,a2,⋯,an (0≤ai≤10^4),表示前文所述的序列。
输出格式:
第一行输出你能获得的最大总得分。
第二行输出k个介于1到n−1之间的整数,表示为了使得总得分最大,你每次操作中分开两个块的位置。第i个整数si表示第i次操作将在si和si+1之间把块分开。
如果有多种方案使得总得分最大,输出任意一种方案即可。
输入样例#1:
7 3 4 1 3 4 0 2 3
输出样例#1:
108 1 3 5
限制与约定
第一个子任务共 11 分,满足 1≤k<n≤10。
第二个子任务共 11 分,满足 1≤k<n≤50。
第三个子任务共 11 分,满足 1≤k<n≤200。
第四个子任务共 17 分,满足 2≤n≤1000,1≤k≤min{n−1,200}。
第五个子任务共 21 分,满足 2≤n≤10000,1≤k≤min{n−1,200}。
第六个子任务共 29 分,满足 2≤n≤100000,1≤k≤min{n−1,200}。
首先我们来试一下把一段序列割成3段的两种割法:
假如有序列 a_1 \sim a_n ,我们割成 a_1 \sim a_i, a_{i+1} \sim a_j, a_{j+1} \sim a_n 三段,前缀和数组是s,先割i后割j的得分是 (s[n] - s[i]) * s[i] + (s[n] - s[j]) * (s[j] - s[i]) ,先割j后割i的得分是 (s[n] - s[j]) * s[j] + (s[j] - s[i]) * s[i] 。我们把它都展开,然后惊奇地发现,他们居然是相等的。
我们设计状态dp[i][j]表示前j个元素切了i刀的最大得分,有了上面的性质,转移就好办了:
dp[i][j] = \max_{k < j}\{ dp[i - 1][k] + s[k] * (s[j] - s[k]) \}
后面的得分怎么计算?我们可以认为是从后往前切,那么这一刀就是第一刀,计算方式就是上面写的那样。毕竟怎么切法得分都是一样的。
但是,现在的转移是O(n)的,总复杂度是O(n^2k)的,需要优化。
首先考虑把i维度用滚动数组优化掉空间,然后来推斜率优化。
现在有j、k两个位置,k>j,k优于j,令f[j] = dp[i - 1][j],则要满足
f[k] + s[k] * (s[i] - s[k]) \geq f[j] + s[j] * (s[i] - s[j])
移项得
f[k] - f[j] - s[k]^2 + s[j]^2 \geq s[i] * (s[j] - s[k])
斜率式是
\frac{f[k] - f[j] - s[k]^2 + s[j]^2}{s[j] - s[k]} \geq s[i]
现在的复杂度是O(nk)的了,已经可以接受了。
但是!题目有个坑,ai可以是0,那这个位置切不切无所谓了,需要特判一下让它斜率极大/极小。
至于方案,记录下来如何转移的就可以了。
注:代码中dp维度和上面题解的不一样。
// Code by KSkun, 2018/3
#include <cstdio>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline int readint() {
register int res = 0, neg = 1;
char c = fgc();
while(c < '0' || c > '9') {
if(c == '-') neg = -1;
c = fgc();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = fgc();
}
return res * neg;
}
const int MAXN = 100005, MAXK = 205;
int n, k, pre[MAXK][MAXN], l, r, q[MAXN];
bool type = false;
LL s[MAXN], dp[MAXN][2];
inline LL sqr(LL x) {
return x * x;
}
inline double slope(int j, int k) {
if(s[j] == s[k]) return -1e18;
return double(dp[k][type ^ 1] - dp[j][type ^ 1] - sqr(s[k]) + sqr(s[j])) / (s[j] - s[k]);
}
int main() {
n = readint();
k = readint();
for(int i = 1; i <= n; i++) {
s[i] = s[i - 1] + readint();
}
for(int p = 1; p <= k; p++) {
l = r = 0;
type ^= 1;
for(int i = 1; i <= n; i++) {
while(l < r && slope(q[l], q[l + 1]) <= s[i]) l++;
dp[i][type] = dp[q[l]][type ^ 1] + s[q[l]] * (s[i] - s[q[l]]);
pre[p][i] = q[l];
while(l < r && slope(q[r], i) <= slope(q[r - 1], q[r])) r--;
q[++r] = i;
}
}
printf("%lld\n", dp[n][type]);
for(int p = k, u = n; p >= 1; p--) {
u = pre[p][u];
printf("%d ", u);
}
return 0;
}