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题解的更多相关文章

  1. 夜未央Test1

    积木游戏(block.pas)   [题目描述] 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,最高的积木的最终需要达到h. 在搭建开始之前,没有任何积木(可以看成n ...

  2. Git异常:Cannot delete the branch 'test1' which you are currently on

    GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...

  3. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  4. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  5. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  6. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  7. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  8. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  9. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

随机推荐

  1. Atom power-mode

    最近看到很多大牛都在用这个酷炫狂拽掉渣天的插件,于是就默默地git了一波.实际结果就是机械键盘加上砰砰砰砰,根本停不下来有没有. 建议(ˉ(∞)ˉ) :视力2.0的同学不建议使用,眼瞎的同学用就用吧, ...

  2. cookie程序设计举例

    编写Cookie应用程序,一般流程是:首先尝试获取某个Cookie变量,如果有,则表明是老客户,读取其cookie信息,为其提供服务. 如果没有,则表明是第一次来访的客户,通过表单提交获取其身份信息, ...

  3. SQL Server 判断数据库是否存在,表是否存在

    if DB_ID('testdb') is not null -- 如果这个数据库已经存在了 drop database testdb; create database testdb; if OBJE ...

  4. 详述USB OTG发展及协议标准

    USB On-The-Go 扩大了USB 的应用领域,将USB 从传统的PC 与外设通讯的模式,扩展到移动电子和嵌入式领域中,抛开主机PC,实现通常的外设与外设之间点对点(Point to Point ...

  5. iOS6和iOS7代码的适配(1)

    苹果的iOS7推出后,对于所有的应用来说都提出了一个天然的需求--适配不同版本的SDK.目前来说,用iOS6的SDK生成的应用,可以在iOS7的系统上跑,UI上也保持了原来的风格样式,这是苹果做的向下 ...

  6. 用sqlserver处理excel表格

    本来最近在研究微信公众平台的,老大临时交我个任务,把excel表格里的数据导入sql数据库,我想这so easy嘛. 没想都在上面消磨了两天... 把情况介绍下:在数据库中有如下这样结构的表(A表) ...

  7. linux内核源码阅读之facebook硬盘加速flashcache之八

    前面我们的分析中重点关注正常的数据流程,这一小节关注如果有异常,那么流程是怎么走完的呢? 1)创建新任务时kcached_job申请不到 2)读写命中时cache块为忙 3)系统关机时处理,系统开机时 ...

  8. 【转】Python3.x移除了callable内建函数

    原文地址:http://www.cnblogs.com/elvisxu/archive/2010/10/26/1861958.html 最近学习Python的时候,在Python3下跑<Dive ...

  9. uva10617 - Again Palindrome(dp)

    再次回文 输入:标准输入 输出:标准输出 时间限制: 2秒 是àpalindorme的读取相同的从左边,因为它从右侧的一个或多个字符的序列.例如,Ž,TOT和女士的 回文,但是,ADAM是不是. 给定 ...

  10. Android 匿名共享内存C++接口分析

    在上一篇Android 匿名共享内存C接口分析中介绍了Android系统的匿名共享内存C语言访问接口,本文在前文的基础上继续介绍Android系统的匿名共享内存提供的C++访问接口.在C++层通过引入 ...