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模拟赛)的更多相关文章

  1. CH Round #52 还教室[线段树 方差]

    还教室 CH Round #52 - Thinking Bear #1 (NOIP模拟赛) [引子]还记得 NOIP 2012 提高组 Day2 中的借教室吗?时光飞逝,光阴荏苒,两年过去了,曾经借教 ...

  2. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  3. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  4. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  5. CH Round #58 - OrzCC杯noip模拟赛day2

    A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...

  6. CH Round #49 - Streaming #4 (NOIP模拟赛Day2)

    A.二叉树的的根 题目:http://www.contesthunter.org/contest/CH%20Round%20%2349%20-%20Streaming%20%234%20(NOIP 模 ...

  7. CH Round #48 - Streaming #3 (NOIP模拟赛Day1)

    A.数三角形 题目:http://www.contesthunter.org/contest/CH%20Round%20%2348%20-%20Streaming%20%233%20(NOIP模拟赛D ...

  8. CH Round #55 - Streaming #6 (NOIP模拟赛day2)

    A.九九归一 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2355%20-%20Streaming%20%236%20(NOIP模拟赛day2)/九九归一 题 ...

  9. 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题, ...

随机推荐

  1. Swift: Initialization-1

    初始化的过程包括为每一个存储属性设置一个初始值和其他步骤.通过定义构造函数来实现初始化的过程,跟oc的初始化函数不同,Swift的构造函数不返回一个值.它们的主要角色是确保一个类型的实例在初次使用前被 ...

  2. eclipse4.3 kepler中安装maven

    1.软件准备 a:Eclipse 4.3 http://www.eclipse.org/downloads/ b:maven http://maven.apache.org/download.cgi ...

  3. windows10UWP:Segoe MDL2 图标指南

    windows10 UWP 开发中,图标的使用非常广泛.为此,微软建议大家使用 Segoe MDL2 Assets 字体来获取图标.Segoe MDL2 Assets 包括了哪里图标,微软在 http ...

  4. SQL Cursor 基本用法

     1 table1结构如下  2 id    int  3 name  varchar(50)  4   5 declare @id int  6 declare @name varchar(50) ...

  5. Gprinter Android SDK V2.1.4 使用说明

    佳博打印机Android的SDK开发包,已更新到Gprinter Android SDK V2.1.4. IOS的SDK开发包更新为GprinterSDKandDemoforIOS_v1.0.8. 根 ...

  6. 修改docker默认172.17网段

    docker启动时默认使用172.17.x.x作为容器的ip地址,可以通过以下方法自定义该网段: sudo service docker stop通过命令route -n查看docker0是否存在,若 ...

  7. 关于cocoapods和swift中使用oc第三方

    mac 系统自带ruby,使用cocoapods,直接安装cocoapods就行 终端:$ sudo gem install cocoapods {安装较慢是因为有墙,查看ruby镜像列表:$ gem ...

  8. kvc简单实现

      除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性 kvc: kvc    key value coding 键值对编码 可以通过 ...

  9. js正则验证方法大全

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  10. 关于c:\fakepath\的解决办法

    (2014.11.25 最后更新) 一.碎碎念:关于访问本地图片的路径的问题,比较典型的例子就是上传头像.在以往的解决办法中,我们大多是先将图片上传到服务器然后从服务器返回图片,显示在页面上以达到预览 ...