CodeForce---Educational Codeforces Round 3 D. Gadgets for dollars and pounds 正题
对于这题笔者无解,只有手抄一份正解过来了:
基本思想就是 :
- 二分答案,对于第x天,计算它最少的花费f(x),<=s就是可行的,这是一个单调的函数,所以可以二分。
- 对于f(x)的计算,我用了nlog(n)的算法,遍历m个商品,用c[i]乘以前x天里,第t[i]种货币的最便宜的价格,存放到一个数组里,之后排序,计算前k个的和就是f(x)
- 前x天里,第t[i]种货币的最便宜的价格是通过预处理得到的,很容易的计算一下前缀最小值就行了。
贴代码吧:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,int> pii;
#define fi first
#define se second
#define mp make_pair
const int maxn = ;
int a[maxn],b[maxn],t[maxn],c[maxn];
int n,m,k,s;
int am[maxn],bm[maxn];
int ida[maxn],idb[maxn];
const int inf = 0x3f3f3f3f; pii pli[maxn];
int id[maxn]; ll f(int x){
ll ret = ;
for (int i=;i<=m;i++){
if (t[i] == ){
pli[i].fi = (ll)c[i] * (ll)am[x]; }
else{
pli[i].fi = (ll)c[i] * (ll)bm[x];
}
pli[i].se = i;
}
sort(pli+,pli+m+);
for (int i=;i<=k;i++){
ret += pli[i].fi;
} return ret;
} int main(){
cin>>n>>m>>k>>s;
am[] = bm[] = inf;
for (int i=;i<=n;i++){
scanf("%d",&a[i]);
if (a[i] < am[i-]){
am[i] = a[i];
ida[i] = i;
}
else{
am[i] = am[i-];
ida[i] = ida[i-];
}
}
for (int i=;i<=n;i++){
scanf("%d",&b[i]);
if (b[i] < bm[i-]){
bm[i] = b[i];
idb[i] = i;
}
else{
bm[i] = bm[i-];
idb[i] = idb[i-];
}
}
for (int i=;i<=m;i++){
scanf("%d%d",&t[i],&c[i]);
} ll low,high,mid;
low = ,high = n;
ll d = -;
while(low <= high){
mid = (low + high) / (ll);
if (f(mid) <= s){
high = mid - ;
d = mid;
for (int i=;i<=k;i++){
id[i] = pli[i].se;
}
}
else{
low = mid + ;
}
}
cout << d <<"\n";
if (d == (ll)-)
return ;
int x = (int)d;
for (int i=;i<=k;i++){
printf("%d %d\n",id[i],t[id[i]]==?ida[x]:idb[x]);
} return ;
}
CodeForce---Educational Codeforces Round 3 D. Gadgets for dollars and pounds 正题的更多相关文章
- Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心
D. Gadgets for dollars and pounds 题目连接: http://www.codeforces.com/contest/609/problem/C Description ...
- CF# Educational Codeforces Round 3 D. Gadgets for dollars and pounds
D. Gadgets for dollars and pounds time limit per test 2 seconds memory limit per test 256 megabytes ...
- Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分+前缀
D. Gadgets for dollars and pounds time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforce |Educational Codeforces Round 77 (Rated for Div. 2) B. Obtain Two Zeroes
B. Obtain Two Zeroes time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
随机推荐
- fhq_treap 总结
今天跟着zcg大神学了一发fhq_treap 发现在维护区间问题上fhq_treap不仅思维量小,而且代码量更小 是Splay的不错的替代品,不过至今还是有一些问题不能很好的解决 譬如查询某个数在序列 ...
- lintcode :Ugly Numbers 丑数
题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n ...
- MIT算法导论——第一讲.Analysis of algorithm
本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...
- 关于java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream解决办法
吉林的一个项目有个错误找了一天,有段报错是: java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream 1.遇到过两次,第 ...
- cpan 配置
$ cpan Cpan>o conf init 最主要的是配置镜像地址,试了下,还是香港的靠谱…… cpan中镜像地址列表:http://www.cpan.org/SITES.html 香港的镜 ...
- Python转义字符
在需要在字符中使用特殊字符时,python用反斜杠(\)转义字符.有时我们并不想让转义字符生效,我们只想显示字符串原来的意思,这就要用r和R来定义原始字符串.如:print r'\t\r'实际输出为“ ...
- [HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 RT,该题要求每次更新是更新所有节点,分别求平方根,查询是求和.昨晚思前想后找有没有一个数学上的 ...
- C++ string类的学习
string类对于处理字符串的一些应用非常的方便,我个人感觉,string和字符数组const char *很像,而且又比字符数组用起来方便的多. 注意其删除,取子串,插入等函数里面都有一个重载版本是 ...
- sdut 2351 In Danger (找规律)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2351 题意:xyez, xy表示一个十进 ...
- 函数fsp_get_space_header
/**********************************************************************//** Gets a pointer to the sp ...