Problem

给一个由问号和数字组成的数字串A(问号表示任一数字)。

再给定n个数字Bi,和0~9的数字的价值。

F(x)表示x各个位数上的价值和。问A为何值时,sum(F(Bi+A))的值最大为多少。

1 ≤ A,Bi < 101000 没有前导零

Solution

dp[i][j]表示第i位时有j个数发生进位时的最大值。

然后我们对有没有进位的情况进行分类讨论

Notice

每次做之前要radixsort一下

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rank t
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 1000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int rank[N + 5], S[N + 5][N + 5], now[N + 5], num[10], F[N + 5][N + 5], len[N + 5], V[10];
char st[N + 5][N + 5];
int m;
void radixsort(int pos)
{
memset(num, 0, sizeof num);
rep(i, 1, m) num[S[i][pos]]++;
per(i, 9, 1) num[i - 1] += num[i];
per(i, m, 1) now[num[S[rank[i]][pos]]--] = rank[i];
rep(i, 1, m) rank[i] = now[i];
}
int sqz()
{
scanf("%s", st[0] + 1);
int n = len[0] = strlen(st[0] + 1);
m = read();
rep(i, 1, m) scanf("%s", st[i] + 1), len[i] = strlen(st[i] + 1), n = max(n, len[i]);
n++;
rep(i, 0, m)
rep(j, 1, len[i])
S[i][j + n - len[i]] = (st[i][j] == '?' ? -1 : st[i][j] - '0');
rep(i, 0, 9) V[i] = read();
rep(i, 1, n)
rep(j, 0, m) F[i][j] = -INF;
int l = S[0][n] == -1 ? 0 : S[0][n], r = S[0][n] == -1 ? 9 : S[0][n];
rep(i, l, r)
{
int cnt = 0, total = 0;
rep(j, 1, m)
{
if (i + S[j][n] >= 10) cnt++;
total += V[(i + S[j][n]) % 10];
}
F[n][cnt] = max(F[n][cnt], total);
}
rep(i, 1, m) rank[i] = i;
rank[m + 1] = m + 1;
per(i, n - 1, 1)
{
radixsort(i + 1);
int l = S[0][i] == -1 ? 0 : S[0][i], r = S[0][i] == -1 ? 9 : S[0][i];
if (n - i + 1 == len[0] && l == 0) l++;
rep(num, l, r)
{
int cnt = 0, total = 0;
rep(j, 1, m)
{
if (max(len[j], len[0]) < n - i + 1) continue;
total += V[(S[j][i] + num) % 10];
cnt += S[j][i] + num >= 10;
}
rep(j, 1, m + 1)
{
F[i][cnt] = max(F[i][cnt], F[i + 1][j - 1] + total);
total += (max(len[rank[j]], len[0]) >= n - i + 1 || (S[rank[j]][i] + num + 1) ? V[(S[rank[j]][i] + num + 1) % 10] : 0) - (max(len[rank[j]], len[0]) >= n - i + 1 || (S[rank[j]][i] + num)? V[(S[rank[j]][i] + num) % 10] : 0);
cnt += S[rank[j]][i] + num + 1 == 10;
}
}
}
int ans = 0;
rep(i, 0, m) ans = max(ans, F[1][i]);
printf("%d\n", ans);
return 0;
}

[Codeforces778E]Selling Numbers的更多相关文章

  1. Codeforces Round #402 (Div. 1)

    A题卡壳了,往离线倒着加那方面想了会儿,后来才发现方向错了,二十多分钟才过掉,过了B后做D,想法好像有点问题,最后只过两题,掉分了,差一点回紫. AC:AB Rank:173 Rating:2227- ...

  2. Codeforces Round#402(Div.1)掉分记+题解

    哎,今天第一次打div1 感觉头脑很不清醒... 看到第一题就蒙了,想了好久,怎么乱dp,倒过来插之类的...突然发现不就是一道sb二分吗.....sb二分看了二十分钟........ 然后第二题看了 ...

  3. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  6. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  7. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  8. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  9. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

随机推荐

  1. Oracle数据库基础入门《二》Oracle内存结构

    Oracle数据库基础入门<二>Oracle内存结构 Oracle 的内存由系统全局区(System Global Area,简称 SGA)和程序全局区(Program Global Ar ...

  2. 解救小哈——bfs广搜

    问题描述: 小哈去玩迷宫,结果迷路了,小哼去救小哈.迷宫由n行m列的单元格组成(n和m都小于等于50),每个单元格要么是空地,要么是障碍物. 问题:帮小哼找到一条从迷宫的起点通往小哈所在位置的最短路径 ...

  3. 【Checkio Exercise】Robot Sort

    Robot Sort All of the refined ingots should be sorted by size in each lot while passing by on a conv ...

  4. 第九节 JS运动应用

    多物体运动框架 多个物体同时运动 例子:多个Div,鼠标移入变宽 单定时器,存在问题 每个Div一个定时器 <!DOCTYPE html> <html lang="en&q ...

  5. Eclipse中XML文件自定义格式化配置

    1,编码格式:UTF-8 2,Line Width:90,Indent using spaces:2 3,默认编辑器 当添加Spket插件后,xml文件默认编辑器将被修改为Spket,要求恢复默认,则 ...

  6. PHP防止网页快速刷新+代理ip访问

    前几天网站收到了一些CC攻击,比较郁闷...这里分享一下,防止网页自动刷新的方法以及阻止代理IP访问网站的方法,代码是分开的,两个功能,需要那个用那个,可以自定义时间间隔,这个代码不止可以防CC攻击, ...

  7. JavaScript 序列化、转义

    JavaScript  序列化.转义 序列化 // 将对象转换为字符串 JSON.stringify() // 将字符串转换为对象类型 JSON.parse() 转义 // URl中未转义的字符 de ...

  8. Python 面向对象介绍

    面向对象,面向过程 面向对象引子 人狗大战,人与狗都有不同的特点,如果要写出这两个不同角色 需要写出两个角色,可以使用嵌套函数,函数内在写入函数,然后通 过字典,将里层函数reture出来,在调用. ...

  9. Linux 组管理、权限

    权限说明 1. 组涉及到两个配置文件,组文件/etc/group,组密码管理员/etc/gshadow/,GID500往后的算普通组. 2.主组与附属组,当创建一个用户,没有制定,用户会默认创建一个与 ...

  10. How to use “cat” command on “find” command's output?

    You can do this with find alone using the -exec action: find /location -size 1033c -exec cat {} + {} ...