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. 【ES6】Set和Map中的NaN

    在JavaScript中,有个有意思的的式子:NaN !== NaN.在Set中的元素的重复检查或者Map键的定位过程中,都是用的类似恒等的检查逻辑.该逻辑和恒等检查的主要区别就是:NaN等于自身.

  2. css 权威指南笔记(二)元素

    替换元素 用来替换元素内容的部分并非有文档内容直接表示. img input 非替换元素 其内容由用户代理(通常是一个浏览器)在元素本身生成的框中显示. 块级元素 块级元素生成一个 元素框,(默认)会 ...

  3. 怎样写好一份IT技术岗位的简历

    10月是校园招聘的旺季,很多应届毕业生都忙碌起来了,从CSDN笔试-面试文章的火热程度,从我收到的简历就看得出来. 我很久没有参与笔试和面试了,所以只能从“简历”来阐述下我的看法. 截至目前,已经帮8 ...

  4. VBA开发中的前绑定与后绑定

    凡是能用createobject创建的对象,都可以在引用相对应的运行库(library)文件之后在对象浏览器中得到它的方法.属性.枚举和事件列表,比如Shell.Application对象在Shell ...

  5. 自己写的自动生成动态边框的jquery小插件

    思路就是在元素四周添加<ul>列表,然后周期性地改变它的颜色,实现动态的效果,不支持ie7.ie8 预览链接http://gorey.sinaapp.com/myBorder/border ...

  6. iBatis 的删除一条记录

    Student.xml 设置删除参数的类型,可以是一个对象的 <delete id="delStudent" parameterClass="int" & ...

  7. WPF MediaElement.Position属性

    Position 属性定义:获取或设置媒体播放时间的当前进度位置. // // 摘要: // 通过媒体播放时获取或设置进度的当前位置. // // 返回结果: // 媒体时自以来的.默认值为 00:0 ...

  8. zabbix 基于JMX的Tomcat监控

    zabbix 基于JMX的Tomcat监控 一.环境 ubuntu14.04 LTS Java 1.7.0 zabbix 2.4.5 二.安装配置 1.安装JavaGateway 在ubuntu14. ...

  9. A除以B_2

    本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...

  10. Linux下使用NMON监控、分析系统性能 -转载

    原帖地址:http://blog.itpub.net/23135684/viewspace-626439/ 谢谢原帖大人 一.下载nmon. 根据CPU的类型选择下载相应的版本:http://nmon ...