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. Php5.3的lambda函数以及closure(闭包)

    从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...

  2. artdialog关闭弹出窗口

    打开 function opentree(){  var dialog = art.dialog({    title: '选择提交部门',      content:jQuery("#my ...

  3. 转:Server.MapPath相关

    如果你从Page类继承的类中执行这条语句,才可以简单地使用 DataBase = Server.MapPath("data.mdb");否则写全命名空间:System.Web.Ht ...

  4. Octopress创建GitHub Pages——基于代码托管的静态博客

    Github Pages是静态网页来的,官方也半认可了它的博客用途,代码挂在github上,随时都可以更改,算是不错的一种尝试,因为它是静态的,所以在表现上会自由得多,但是,同样因为它是静态的,管理上 ...

  5. python之字符串格式化(format)

    用法: 它通过{}和:来代替传统%方式 1.使用位置参数 要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 ...

  6. javascript学习笔记(2)

    <html> <head><title>Throwing die</title><script>    var canv_width  = ...

  7. 关于C语言中结构体中的结构体成员导致的字节对齐问题

    关于结构体的字节对齐是什么,就不赘述,再此附上一篇文章,介绍字节对齐:http://www.linuxsong.org/2010/09/c-byte-alignment/ 这里的结构体字节对齐的数据类 ...

  8. 致命错误C0000034正在应用更新操作37解决方法

    当碰到这个问题的时候关闭电脑,启动,不断按F8键,进入命令行,输入Notepad.exe弹出记事本,文件,打开,所有文件,然后将C:\Windows\WinSxS目录下的pending.xml文件中的 ...

  9. java积累

    数组的使用 package javaDemo; import java.util.*; /** * * @author Administrator * @version 1.0 * * */ publ ...

  10. centos6.5 openvpn安装配置

    http://m.jb51.net/?host=www.jb51.net&src=http%3A%2F%2Fwww.jb51.net%2Fsoftjc%2F150885.html