华中农业大学第四届程序设计大赛网络同步赛 G.Array C 线段树或者优先队列
Problem G: Array C
Time Limit: 1 Sec Memory Limit: 128 MB
Description
Giving two integers and and two arrays and both with length , you should construct an array also with length which satisfied:
1.0≤Ci≤Ai(1≤i≤n)
2..png)
and make the value S be minimum. The value S is defined as:
.png)
Input
There are multiple test cases. In each test case, the first line contains two integers n(1≤n≤1000) andm(1≤m≤100000). Then two lines followed, each line contains n integers separated by spaces, indicating the array Aand B in order. You can assume that 1≤Ai≤100 and 1≤Bi≤10000 for each i from 1 to n, and there must be at least one solution for array C. The input will end by EOF.
Output
For each test case, output the minimum value S as the answer in one line.
Sample Input
3 4
2 3 4
1 1 1
Sample Output
6
思路:首先我们会想找到最小的一直下去,但是有个A数组为上界不好处理,所以先将C数组全部修改成A数组,一直向下减,当C相加等于M的时候得到答案;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
#define esp 0.00000000001
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
struct is
{
ll l,r;
ll maxx;
ll num;
ll sum;
}tree[];
ll a[];
ll b[];
void buildtree(ll l,ll r,ll pos)
{
tree[pos].l=l;
tree[pos].r=r;
if(l==r)
{
tree[pos].num=a[l];
tree[pos].maxx=(a[l]*a[l]-(a[l]-)*(a[l]-))*b[l];
tree[pos].sum=a[l];
return;
}
ll mid=(l+r)/;
buildtree(l,mid,pos*);
buildtree(mid+,r,pos*+);
tree[pos].maxx=max(tree[pos*].maxx,tree[pos*+].maxx);
tree[pos].sum=tree[pos*+].sum+tree[pos*].sum;
}
void update(ll l,ll r,ll pos,ll change)
{
if(tree[pos].r==change&&tree[pos].l==change)
{
ll kk=tree[pos].num-;
tree[pos].num=kk;
tree[pos].maxx=(kk*kk-(kk-)*(kk-))*b[change];
tree[pos].sum=kk;
return;
}
ll mid=(l+r)/;
if(change<=mid)
update(l,mid,pos*,change);
else
update(mid+,r,pos*+,change);
tree[pos].sum=tree[pos*].sum+tree[pos*+].sum;
tree[pos].maxx=max(tree[pos*].maxx,tree[pos*+].maxx);
}
ll findmax(ll l,ll r,ll pos,ll gg)
{
if(tree[pos].l==tree[pos].r)
return tree[pos].l;
ll mid=(l+r)/;
if(tree[pos*].maxx==gg)
return findmax(l,mid,pos*,gg);
else
return findmax(mid+,r,pos*+,gg);
}
ll getans(ll l,ll r,ll pos)
{
if(l==r)
return tree[pos].num*tree[pos].num*b[l];
ll mid=(l+r)/;
return getans(l,mid,pos*)+getans(mid+,r,pos*+);
}
int main()
{
ll x,y,z,i,t;
while(~scanf("%lld%lld",&x,&y))
{
for(i=;i<=x;i++)
scanf("%lld",&a[i]);
for(i=;i<=x;i++)
scanf("%lld",&b[i]);
buildtree(,x,);
while(tree[].sum!=y)
{
ll pos=findmax(,x,,tree[].maxx);
update(,x,,pos);
}
printf("%lld\n",getans(,x,));
}
return ;
}
华中农业大学第四届程序设计大赛网络同步赛 G.Array C 线段树或者优先队列的更多相关文章
- (hzau)华中农业大学第四届程序设计大赛网络同步赛 G: Array C
题目链接:http://acm.hzau.edu.cn/problem.php?id=18 题意是给你两个长度为n的数组,a数组相当于1到n的物品的数量,b数组相当于物品价值,而真正的价值表示是b[i ...
- [HZAU]华中农业大学第四届程序设计大赛网络同步赛
听说是邀请赛啊,大概做了做…中午出去吃了个饭回来过掉的I.然后去做作业了…… #include <algorithm> #include <iostream> #include ...
- 华中农业大学第四届程序设计大赛网络同步赛 J
Problem J: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1766 Solved: 299[Subm ...
- 华中农业大学第四届程序设计大赛网络同步赛 I
Problem I: Catching Dogs Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1130 Solved: 292[Submit][St ...
- 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;
1020: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: ->打开链接<- Descriptio ...
- 华中农业大学第五届程序设计大赛网络同步赛-G
G. Sequence Number In Linear algebra, we have learned the definition of inversion number: Assuming A ...
- 华中农业大学第五届程序设计大赛网络同步赛-L
L.Happiness Chicken brother is very happy today, because he attained N pieces of biscuits whose tast ...
- 华中农业大学第五届程序设计大赛网络同步赛-K
K.Deadline There are N bugs to be repaired and some engineers whose abilities are roughly equal. And ...
- 华中农业大学第五届程序设计大赛网络同步赛-D
Problem D: GCD Time Limit: 1 Sec Memory Limit: 1280 MBSubmit: 179 Solved: 25[Submit][Status][Web B ...
随机推荐
- Spark中cache和persist的区别
cache和persist都是用于将一个RDD进行缓存的,这样在之后使用的过程中就不需要重新计算了,可以大大节省程序运行时间. cache和persist的区别 基于Spark 1.6.1 的源码,可 ...
- [py]django模板继承
参考 1.展示arr,d等数据类型 2.逻辑for if / url获取 3.获取内置变量 django模板继承 通过搞一个base.html 这个base.html可以包含两类 block片断 其他 ...
- String.getBytes()未设置字符集导致打印的pdf乱码
如果不设置字符集会选择系统字符集,系统也没设置,会选iso-8859-1 导致汉字乱码,成为?
- LNMPA架构剖析
LAMP或LNMP的劣势: Nginx是小巧而高效的Linux下Web服务器,跟Apache相比,它消耗资源更少,支持的并发连接更多,反向代理功能效率高.静态文件处理更快等等,Nginx可以承受3万以 ...
- keras自定义padding大小
1.keras卷积操作中border_mode的实现 def conv_output_length(input_length, filter_size, border_mode, stride): i ...
- DNS解析原理和流程
DNS解析原理和流程 DNS解析其实就是将IP地址(202.96.134.133)变成域名(www.xxxxx.com) 网络通讯大部分是基于TCP/IP的,而TCP/IP是基于IP地址的,所 ...
- Summary: 书架问题
Consider the problem of storing n books on shelves in a library. The order of the books is fixed by ...
- FAFU 1395
动态规划:...翻牌FAFU 1395 动态规划
- quartz开源作业调度框架的配置
quartz开源作业调度框架的job服务实现,Quartz是一个完全由java编写的开源作业调度框架,使用时候需要创建一个实现org.quartz.Job接口的java类,Job接口包含唯一的方法: ...
- 日志处理(一) log4j 入门和详解(转)
log4j 入门. 详解 转自雪飘寒的文章 1. Log4j 简介 在应用程序中添加日志记录总的来说基于三 个目的: 监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作 ...