CH Round #52 - Thinking Bear #1 (NOIP模拟赛)
A.拆地毯
题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOIP模拟赛)/拆地毯
题解:按最大生成树顺序加k条边即可
代码:
const maxn=+;
var c:array[..maxn] of int64;
fa,a,b:array[..maxn] of longint;
i,j,n,m,k:longint;
ans:int64;
function find(x:longint):longint;
begin
if fa[x]<>x then fa[x]:=find(fa[x]);
exit(fa[x]);
end; procedure sort(l,r:longint);
var i,j:longint;x,y:int64;
begin
i:=l;j:=r;x:=c[(i+j)>>];
repeat
while c[i]>x do inc(i);
while c[j]<x do dec(j);
if i<=j then
begin
y:=a[i];a[i]:=a[j];a[j]:=y;
y:=b[i];b[i]:=b[j];b[j]:=y;
y:=c[i];c[i]:=c[j];c[j]:=y;
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if j>l then sort(l,j);
end;
procedure init;
begin
readln(n,m,k);
for i:= to m do readln(a[i],b[i],c[i]);
end;
procedure main;
begin
sort(,m);
for i:= to n do fa[i]:=i;
j:=;
ans:=;
for i:= to k do
begin
while (find(a[j])=find(b[j])) do inc(j);
fa[find(a[j])]:=find(b[j]);
inc(ans,c[j]);
end;
writeln(ans);
end; begin
init;
main;
end.
B.还教室
题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOIP模拟赛)/还教室
题解:前两个操作都是线段树基础操作,第三个操作需要一点变形
首先我们要知道:方差s=sigma(xi-x0)^2 (i=1..n) /n =sigma(xi^2)-x0^2 x0表示平均数
这手推一下就知道了,我们只需要指导一段区间内的平方和即可,线段树也是可以维护的
(ms贴吧里有人说 只要是具有加和性?就能用线段树来维护?
我理解的是 加上1个值x1 又加了1个值x2,不管第一次加的值,直接把需要加的值更改为x1+x2在维护的时候可以正确就是对的。。。)
代码:
const maxn=+;
type node=record
l,r,mid,lch,rch:longint;
sum,pf,tag:int64;
end;
var t:array[..*maxn] of node;
a:array[..maxn] of longint;
i,j,n,m,ch,x,y:longint;
xx,yy,z:int64;
procedure pushup(k:longint);
begin
with t[k] do
begin
sum:=t[lch].sum+t[rch].sum;
pf:=t[lch].pf+t[rch].pf;
end;
end;
procedure update(k:longint;val:int64);
begin
with t[k] do
begin
inc(tag,val);
inc(pf,*val*sum+(r-l+)*val*val);
inc(sum,(r-l+)*val);
end;
end;
procedure pushdown(k:longint);
begin
with t[k] do
begin
if tag= then exit;
update(lch,tag);update(rch,tag);
tag:=;
end;
end; procedure build(k,x,y:longint);
begin
with t[k] do
begin
l:=x;r:=y;mid:=(l+r)>>;lch:=k<<;rch:=k<<+;tag:=;
if l=r then begin sum:=a[l];pf:=a[l]*a[l];exit;end;
build(lch,l,mid);build(rch,mid+,r);
pushup(k);
end;
end;
procedure change(k,x,y,z:longint);
begin
with t[k] do
begin
if (l=x) and (r=y) then begin update(k,z);exit;end;
pushdown(k);
if y<=mid then change(lch,x,y,z)
else if x>mid then change(rch,x,y,z)
else
begin
change(lch,x,mid,z);change(rch,mid+,y,z);
end;
pushup(k);
end;
end;
function getsum(k,x,y:longint):int64;
begin
with t[k] do
begin
if (l=x) and (r=y) then exit(sum);
pushdown(k);
if y<=mid then exit(getsum(lch,x,y))
else if x>mid then exit(getsum(rch,x,y))
else exit(getsum(lch,x,mid)+getsum(rch,mid+,y));
end;
end;
function getsqr(k,x,y:longint):int64;
begin
with t[k] do
begin
if (l=x) and (r=y) then exit(pf);
pushdown(k);
if y<=mid then exit(getsqr(lch,x,y))
else if x>mid then exit(getsqr(rch,x,y))
else exit(getsqr(lch,x,mid)+getsqr(rch,mid+,y));
end;
end; procedure init;
begin
readln(n,m);
for i:= to n do read(a[i]);readln;
build(,,n);
end;
procedure solvechange;
begin
readln(x,y,z);
change(,x,y,z);
end;
function gcd(x,y:int64):int64;
begin
if y= then exit(x) else exit(gcd(y,x mod y));
end;
procedure solveaverage;
begin
readln(x,y);
xx:=getsum(,x,y);yy:=y-x+;
z:=gcd(xx,yy);
if xx= then writeln('0/1')
else writeln(xx div z,'/',yy div z);
end;
procedure solvefangcha;
begin
readln(x,y);
yy:=y-x+;
xx:=yy*getsqr(,x,y)-sqr(getsum(,x,y));
yy:=yy*yy;
z:=gcd(xx,yy);
if xx= then writeln('0/1')
else writeln(xx div z,'/',yy div z);
end; procedure main;
begin
for i:= to m do
begin
read(ch);
case ch of
:solvechange;
:solveaverage;
:solvefangcha;
end;
end;
end; begin
init;
main;
end.
C.皇后游戏
题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOIP模拟赛)/皇后游戏
题解:不会捉。。。按a排序,骗了30分。。。
代码:
1.我的
const maxn=+;
var a,b,c:array[..maxn] of int64;
ans,sum:int64;
i,j,n,cs:longint;
function max(x,y:int64):int64;
begin
if x>y then exit(x) else exit(y);
end; procedure sort(l,r:longint);
var i,j:longint;x,y:int64;
begin
i:=l;j:=r;x:=c[(i+j)>>];
repeat
while c[i]<x do inc(i);
while c[j]>x do dec(j);
if i<=j then
begin
y:=a[i];a[i]:=a[j];a[j]:=y;
y:=b[i];b[i]:=b[j];b[j]:=y;
y:=c[i];c[i]:=c[j];c[j]:=y;
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if j>l then sort(l,j);
end;
procedure init;
begin
readln(n);
for i:= to n do readln(a[i],b[i]);
for i:= to n do c[i]:=a[i];
end;
procedure main;
begin
sort(,n);sum:=;ans:=;
for i:= to n do
begin
sum:=sum+a[i];
ans:=max(ans,sum)+b[i];
end;
writeln(ans);
end; begin
readln(cs);
while cs> do
begin
dec(cs);
init;
main;
end;
end.
2.标程 也是排序,不过好高大上啊。。。
//#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <set>
#include <algorithm>
#define mp make_pair
#define fi first
#define se second using namespace std; typedef long long int64;
typedef pair<int,int> PII;
const int MAXN=; int n;
PII a[MAXN];
int64 dp[MAXN]; inline bool cmp(const PII &x,const PII &y)
{
return min(x.fi,y.se)<min(x.se,y.fi);
} int main()
{
int testCase;
for (scanf("%d",&testCase);testCase;testCase--)
{
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d%d",&a[i].fi,&a[i].se);
sort(a+,a+n+,cmp);
int64 s=;
for (int i=;i<=n;i++)
{
s+=a[i].fi;
dp[i]=max(s,dp[i-])+a[i].se;
}
cout<<dp[n]<<endl;
}
return ;
}
CH Round #52 - Thinking Bear #1 (NOIP模拟赛)的更多相关文章
- CH Round #52 还教室[线段树 方差]
还教室 CH Round #52 - Thinking Bear #1 (NOIP模拟赛) [引子]还记得 NOIP 2012 提高组 Day2 中的借教室吗?时光飞逝,光阴荏苒,两年过去了,曾经借教 ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #49 - Streaming #4 (NOIP模拟赛Day2)
A.二叉树的的根 题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模 ...
- CH Round #48 - Streaming #3 (NOIP模拟赛Day1)
A.数三角形 题目:http://www.contesthunter.org/contest/CH%20Round%20%2348%20-%20Streaming%20%233%20(NOIP模拟赛D ...
- CH Round #55 - Streaming #6 (NOIP模拟赛day2)
A.九九归一 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2355%20-%20Streaming%20%236%20(NOIP模拟赛day2)/九九归一 题 ...
- CH Round #54 - Streaming #5 (NOIP模拟赛Day1)
A.珠 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2354%20-%20Streaming%20%235%20(NOIP模拟赛Day1)/珠 题解:sb题, ...
随机推荐
- win7 下配置 java 环境变量
首先,你应该已经安装了 java 的 JDK 了,笔者安装的是:jdk-7u7-windows-x64 接下来主要讲怎么配置 java 的环境变量,也是为了以后哪天自己忘记了做个备份 1.进入“计算机 ...
- SQLServer 触发器----增删改触发,两张表
ALTER TRIGGER [dbo].[PriceRange] ON [dbo].[Tab_SaleAndCarStyle] for update,insert,deleteASdecla ...
- 用户组,AD域控简介
“自由”的工作组 工作组(WORK GROUP)就是将不同的电脑按功能分别列入不同的组中,以方便管理.比如在一个网络内,可能有成百上千台工作电脑,如果这些电脑不进行分组,都列在“网上邻居”内,可 ...
- 由App的启动说起(转)
The two most important days in your life are the day you are born and the day you find out why. -- M ...
- c读写文件相关
1.打开文件: 函数原型: FILE * fopen(const char * path,const char * mode); 返回值: 文件顺利打开后,指向该流的文件指针就会被返回.如果文件打开失 ...
- mysql更新密码为空
1.进入命令行 mysql -u root -p 'oldpassword'; 2 修改root用户的密码:mysql> update mysql.user set password=PASSW ...
- paramiko SSH 模块简单应用。
目的:需要ssh链接到Linux主机,执行telnet 命令,抓回显匹配制定内容. ssh --->执行telnet到本地端口--->执行类似 ls 的命令.匹配命令执行后的特定回显字段. ...
- extend简单用法
eg:var obj1=[{a:1,b:2},{a:2,b:3}] var obj2=[{c:3,d:2},{c:4,d:3}] var resultArray=[]; for (var i = 0; ...
- scons小结
scons是用python写的,据说是比make要方便很多,其实我都没写过makeFile...... 1.安装 方式1:下载安装包安装,需要用python setup.py install去编译 方 ...
- 4种检测是否支持HTML5的方法,你知道几个?
4种检测是否支持HTML5的方法,你知道几个? 1,检查特定的属性是否存在于全局的对象里面,比如说window或navigator. 比如geolocation,它是HTML5新加支持的新特性:它是由 ...