[Codeforces778E]Selling Numbers
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的更多相关文章
- Codeforces Round #402 (Div. 1)
A题卡壳了,往离线倒着加那方面想了会儿,后来才发现方向错了,二十多分钟才过掉,过了B后做D,想法好像有点问题,最后只过两题,掉分了,差一点回紫. AC:AB Rank:173 Rating:2227- ...
- Codeforces Round#402(Div.1)掉分记+题解
哎,今天第一次打div1 感觉头脑很不清醒... 看到第一题就蒙了,想了好久,怎么乱dp,倒过来插之类的...突然发现不就是一道sb二分吗.....sb二分看了二十分钟........ 然后第二题看了 ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [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 ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
随机推荐
- JavaScript 数组插入元素并排序
1.插入类排序 插入类排序的思想是:在一个已排好序的序列区内,对待排序的无序序列中的记录逐个进行处理,每一步都讲待排序的记录和已排好的序列中的记录进行比较,然后有序的插入到该序列中,直到所有待排序的记 ...
- python将字符串转换成整型
将字符串转换成,整型,从字面理解很容易让人误会. 比如,要把这个"abcabc"转换成整型,臣妾做不到啊.除成转成ascii. 我们所说字符串转成整型是这样的. s = " ...
- 使用intellJ导入非maven,gradle等非构建工程的依赖,发布工程时候的打包详解
一.导入 1.java项目在没有导入该jar包之前,如图: 2.点击 File -> Project Structure(快捷键 Ctrl + Alt + Shift + s),点击Proje ...
- 解救小哈——bfs广搜
问题描述: 小哈去玩迷宫,结果迷路了,小哼去救小哈.迷宫由n行m列的单元格组成(n和m都小于等于50),每个单元格要么是空地,要么是障碍物. 问题:帮小哼找到一条从迷宫的起点通往小哈所在位置的最短路径 ...
- zw量化交易·实盘操作·系列培训班
参见: <zw量化交易·实盘操作·系列培训班> http://blog.sina.com.cn/s/blog_7100d4220102w0q5.html
- 复旦大学2017--2018学年第一学期高等代数I期末考试情况分析
一.期末考试成绩班级前十名 郭宇城(100).魏一鸣(93).乔嘉玮(92).刘宇其(90).朱柏青(90).王成文健(90).方博越(88).熊子恺(88).张君格(88).崔镇涛(87).史书珣( ...
- C#方法过滤器
方法过滤器 使用Emit和注解属性Attribute实现 使用方式 1. 自定义方法过滤器 可分别定义方法执行前过滤器, 方法执行结束过滤器, 方法异常过滤器 执行前过滤器继承 ExecutingFi ...
- windows10下安装Redis
已有64位的Redis-x64-3.2.100.msi,点击以安装
- abstract class和interface的异同
含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...
- 转 flowcanvas
http://blog.sina.com.cn/s/blog_5fb40ceb0102wveq.html Unity 强大的可视化编程插件,Flowcanvas + Nodecanvas 组合(深度修 ...