夜未央Test1题解
T1 积木游戏
树状数组的一个简单应用,建立一个维护左节点的树状数组和一个维护右节点的树状数组,对于add操作,只要在维护左节点的树状数组l处加1,维护右节点的树状数组r处加1,那么询问[l,r]的答案就是左节点数组的r前缀和减去右节点数组的l-1前缀和。
var
q,suml,sumr,i,j,k,l,r,m,n:longint;
cl,cr:array[..] of longint; {file}
procedure openf;
begin
assign(input,'block.in'); reset(input);
assign(output,'block.out'); rewrite(output);
end;
procedure closef;
begin
close(input); close(output);
halt;
end; {lowbit}
function lowbit(p:longint):longint;
begin
exit(p and -p);
end; {add}
procedure addl(x,num:longint);
begin
while x<=n+ do
begin
inc(cl[x],num);
x:=x+lowbit(x);
end;
end;
procedure addr(x,num:longint);
begin
while x<=n+ do
begin
inc(cr[x],num);
x:=x+lowbit(x);
end;
end; {get}
procedure getsuml(x:longint);
begin
suml:=;
while x> do
begin
inc(suml,cl[x]);
x:=x-lowbit(x);
end;
end;
procedure getsumr(x:longint);
begin
sumr:=;
while x> do
begin
inc(sumr,cr[x]);
x:=x-lowbit(x);
end;
end; begin
{input}
openf;
readln(n,m); {doit}
for i:= to m do
begin
readln(q,l,r);
if q= then begin
addl(l,);
addr(r,);
end;
if q= then begin
getsuml(r);
getsumr(l-);
writeln(suml-sumr);
end;
end;
closef;
end.
1
#include<iostream>
#include<cstdio>
#define lowbit(x) x&(-x)
using namespace std; int q,i,j,k,l,r,m,n;
int cl[],cr[]; //file
void openf()
{
freopen("block.in","r",stdin);
freopen("block.out","w",stdout);
}
void closef()
{
fclose(stdin); fclose(stdout);
} //INT
int INT()
{
int res;
char ch;
while (ch = getchar(), !isdigit(ch));
for (res = ch - ''; ch = getchar(), isdigit(ch);)
res = res * + ch - '';
return res;
} //add
void addl(int x,int num)
{
for (; x<=n+; x+=lowbit(x)) cl[x]+=num;
}
void addr(int x,int num)
{
for (; x<=n+; x+=lowbit(x)) cr[x]+=num;
} //get
int suml(int x)
{
int sum = ;
for (; x; x-=lowbit(x)) sum+=cl[x];
return sum;
}
int sumr(int x)
{
int sum = ;
for (; x; x-=lowbit(x)) sum+=cr[x];
return sum;
} int main()
{
//input
openf();
n = INT();
m = INT(); //doit
for (i = ; i<=m; i++)
{
q = INT();
l = INT();
r = INT();
if (q==)
{
addl(l,);
addr(r,);
}else printf("%d\n",suml(r)-sumr(l-));
}
closef();
return ;
}
2
T2 数字游戏
这道题首先要做到的就是如何确定方程,由于按照顺序输出,所以我们假定x1<=x2<=x3<=……<=xn,那么我们先将a排序,由于x1+x2是最小的,所以就是a1,同理x1+x3第二小,为a2,但是需要注意的是x2+x3不一定是第三小的,有可能x1+xk比其小,所以我们从a3开始,一个一个假定为x2+x3,联立之前的几个方程,解出x1,x2,x3,然后如果是负数或是无解则跳过,若解出,将x1+x2,x1+x3,x2+x3的结果从a数组中去掉,然后剩下最小的就一定是x1+x4,于是解出x4,之后如上去掉x4与之前几个解的和,剩下最小的是x1+x5,依次做下去,如果其中一个解为负,则该情况不成立,直到所有解解出,输出即可。
用样例小小解释一下,a数组排序后为{1,2,3,4,5,6},则x1+x2=1,x1+x3=2,假设x2+x3=3解得x1=0,所以x2=1,x3=2,去掉1,2,3,集合为{4,5,6},则x4+x1为4,x4=4,去除4,5,6,集合为空,所以解成立,输出0,1,2,4。
var
bo:boolean;
q,i,j,k,l,m,n,tot:longint;
b,a:array[..] of longint;
x:array[..] of longint; {file}
procedure openf;
begin
assign(input,'math.in'); reset(input);
assign(output,'math.out'); rewrite(output);
end;
procedure closef;
begin
close(input); close(output);
halt;
end; {sort}
procedure qsort(l,r:longint);
var
i,j,mid,t:longint;
begin
i:=l; j:=r;
mid:=b[l+random(r-l+)];
repeat
while b[i]<mid do inc(i);
while b[j]>mid do dec(j);
if i<=j then begin
t:=b[i];
b[i]:=b[j];
b[j]:=t;
inc(i); dec(j);
end;
until i>j;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end; begin
{input}
openf;
readln(n);
n:=n*(n-) div ;
for i:= to n do
read(b[i]);
randomize;
qsort(,n); {doit}
for i:= to n do
begin
bo:=true;
move(b[],a[],n*sizeof(b[]));
x[]:=(a[]+a[]-a[i]);
if x[]< then continue;
if x[] mod = then
begin
x[]:=x[] div ;
x[]:=a[]-x[];
if x[]< then continue;
x[]:=a[i]-x[];
if x[]< then continue;
end
else continue;
a[]:=; a[]:=;
a[i]:=;
tot:=;
for j:= to n do
if a[j]<> then
begin
inc(tot);
x[tot]:=a[j]-x[];
if x[tot]< then begin
bo:=false;
break;
end;
k:=;
for l:=j to n do
if a[l]=x[tot]+x[k] then
begin
a[l]:=;
inc(k);
if k=tot then break;
end;
if k<>tot then bo:=false;
end;
if bo then begin
for l:= to tot do
write(x[l],' ');
closef;
end;
end; {closef}
writeln('No solution');
closef;
end.
1
#include <iostream> using namespace std; bool bo;
int q,i,j,k,l,m,n,tot;
int b[],a[];
int x[]; //file
void openf()
{
freopen("math.in","r",stdin);
freopen("math.out","w",stdout);
}
void closef()
{
fclose(stdin); fclose(stdout);
} //INT
int INT()
{
int res;
char ch;
while (ch = getchar(), !isdigit(ch));
for (res = ch - ''; ch = getchar(), isdigit(ch);)
res = res * + ch - '';
return res;
} //sort
void qsort(int l,int r)
{
int i,j,t,mid;
mid = b[(l+r)>>];
i = l; j = r;
do {
while (b[i]<mid) i++;
while (b[j]>mid) j--;
if (i<=j)
{
t = b[i];
b[i] = b[j];
b[j] = t;
i++; j--;
}
}
while (i<=j);
if (i<r) qsort(i,r);
if (l<j) qsort(l,j);
} int main()
{
int step = ;
//input
openf();
n = INT();
n = n * (n - ) / ;
if (n==)
{
cout<<"No solution";
closef();
return();
}
for (i=; i<=n; i++)
b[i] = INT();
qsort(,n);
//doit
for (i=; i<=n; i++)
{
bo = true;
for (j=; j<=n; j++)
a[j] = b[j];
x[] = (a[]+a[]-a[i]);
if (x[]<) continue;
if (x[]%==)
{
x[] /= ;
x[] = a[]-x[];
if (x[]<) continue;
x[] = a[i]-x[];
if (x[]<) continue;
}
else continue;
a[] = ; a[] = ;
a[i] = ;
tot = ;
for (j=; j<=n; j++)
if (a[j]!=)
{
x[++tot] = a[j]-x[];
if (x[tot]<)
{
bo = false;
break;
}
k = ;
for (l=j; l<=n; l++)
if (a[l] == x[tot]+x[k])
{
a[l] = ;
k++;
if (k == tot) break;
}
if (k!=tot) bo = false;
}
if (bo)
{
for (l=; l<=tot; l++)
printf("%d ",x[l]);
//system("pause");
closef;
return ;
}
} cout<<"No solution";
closef();
return ;
}
2
T3 造梦
分析题目,可得一个很显然的贪心,所有石柱相差不超过5那么全部一样是最优的,因为最终高度取决于最短的石柱,然后如何去求这个统一高度呢?我们可以这样设想,如果知道了这个需要的高度,那么验证是否可以达到是不是就非常简单了,只要将每一块石料除以已知高度加到ans上,看是否达到需要的块数就可以了。那么,二分检索的模型就浮出水面了,我们取最长石料为右端点,1为左端点,每一次取中点检验是否可行,然后根据单调性更新左右端点,就可以简单地完成这道题了。
var
l,r,ans,mid,cnt,i,j,k,m,n:longint;
a:array[..] of longint; {file}
procedure openf;
begin
assign(input,'build.in'); reset(input);
assign(output,'build.out'); rewrite(output);
end;
procedure closef;
begin
close(input); close(output);
halt;
end; {check}
function check(x:longint):boolean;
begin
cnt:=;
for i:= to m do
begin
inc(cnt,a[i] div x);
if cnt>=n then exit(true);
end;
exit(false);
end; begin
{input}
openf;
readln(m,n); {doit}
for i:= to m do
begin
read(a[i]);
if a[i]>r then r:=a[i];
end;
l:=;
repeat
mid:=(l+r)>>;
if check(mid) then begin
ans:=mid;
l:=mid+;
end
else r:=mid-;
until l>r; {output}
writeln(ans);
closef;
end.
1
#include <iostream>
#include <cstdio> using namespace std; const int size=; int l,r,ans,mid,cnt,i,j,k,m,n;
int a[size]; //file
void openf()
{
freopen("build.in", "r", stdin);
freopen("build.out", "w", stdout);
}
void closef()
{
fclose(stdin); fclose(stdout);
} //check
bool check(int x)
{
cnt = ;
for (i = ; i < m; i++)
{
cnt+=(a[i] / x);
if (cnt >= n) return(true);
}
return(false);
} //INT
int INT()
{
int res;
char ch;
while (ch = getchar(), !isdigit(ch));
for (res = ch - ''; ch = getchar(), isdigit(ch);)
res = res * + ch - '';
return res;
} //main
int main()
{
openf();
m = INT();
n = INT(); for (i = ; i < m; i++)
{
a[i] = INT();
if (a[i] > r) r = a[i];
}
l = ;
do
{
mid = (l + r) >> ;
if (check(mid))
{
ans = mid;
l = mid + ;
}
else r = mid-;
}
while (l <= r); printf("%d",ans);
//system("pause");
closef;
return();
}
2
夜未央Test1题解的更多相关文章
- 夜未央Test1
积木游戏(block.pas) [题目描述] 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,最高的积木的最终需要达到h. 在搭建开始之前,没有任何积木(可以看成n ...
- Git异常:Cannot delete the branch 'test1' which you are currently on
GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
随机推荐
- Linux网络管理——子网掩码
1. 网络基础 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",&q ...
- 读jQuery源码 jQuery.data
var rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/, rmultiDash = /([A-Z])/g; function internalData( elem, n ...
- Nginx 拒绝指定IP访问
来源 : http://www.ttlsa.com/nginx/nginx-deny-ip-access/ 闲来无事,登陆服务器,发现有个IP不断的猜测路径.试图往服务器上传文件(木马).于是查看 ...
- mini-httpd源码分析-tdate_parse.h
///关联字符串和整数 struct strlong { char* s; long l; }; ///将字符串中的大写字母转换成小写字母 static void pound_case(char* s ...
- OpenCV学习 6:平滑滤波器 cvSmooth()——2
原创文章,欢迎转载,转载请注明出处 前面进行了彩色的模糊处理,我们对黑白图片进行同样的平滑处理,看看效果.首先是需要创建黑白图片,我对opencv的各种函数还不是很熟悉,我们可以先用熟悉的创建单通道的 ...
- 自定义Edit控件控制输入范围
//自定义Edit,实现十六进制输入控制,使用OnChar()函数实现 //MyEdit.h #pragma once class CMyEdit : public CEdit { DECLARE_D ...
- rsyslog 配置
在Debian环境下: 1,配置文件在/etc/rsyslogd.conf下: 2,如果要增加配置,并且不想直接修改rsyslogd.conf文件,可以在/etc/rsyslog.d/目录下增加文件, ...
- applet部署,无需修改客户端设置。
1 开发applet程序,编译成jar包 2 给jar包做数字签名: (1).用keytool生成密钥: keytool -genkey -keystore myapplet.keystore - ...
- Ignatius and the Princess III(母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 统计分析SQL Server Profiler 跟踪的SQL
--跟踪文件读入到表中分析 SELECT * INTO ZGSJY FROM fn_trace_gettable('E:\wxxcdbprofiler.trc', default); --某时间内,最 ...