bzoj 2058+2059+2060 Usaco2010 Nov
三道金组比较容易的题目。。
2058
首先交换次数就是逆序对数,因为只能交换相邻的两数
先对原序列找逆序对数
用树状数组nlogn求出
然后O(n)依次求出其循环序列的逆序对数
比如 3 5 4 2 1
循环之后相对应的位置变成
2 4 3 1 5
表示第一个数到位置2,第二个数到位置4,第三个数到位置1 etc.
那么依次减一的那些数的逆序对数是不变的。只有1变成5的那个增加或减少了逆序对数
由于5是序列里最大的数,设其所在位置为i,因此增加了n-i对,减少了i-1对
这样总复杂度就是nlogn的
记得开Long long
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
using namespace std;
struct node{
int v,id;
}a[];
LL tot,ans,p[];
int n;
bool cmp(node a, node b){
return a.v<b.v;
}
void add(int x){
while (x<=n){
p[x]++;
x+=x&-x;
}
}
LL sum(int x){
LL ret=;
while (x){
ret+=p[x];
x-=x&-x;
}return ret;
}
int main(){
scanf("%d", &n);
; i<=n; i++) scanf(;
tot=;
; i<=n; i++){
add(a[i].v);
tot+=(LL)i-sum(a[i].v);
}
sort(a+,a++n,cmp);
ans=tot;
; i<=n; i++){
);
tot+=(LL)del;
ans=min(ans,tot);
}
printf("%lld\n", ans);
;
}
2059
单调队列DP,降复杂度为O(NK)
分n阶段进行单调队列的优化dp
设dis=a[i].x-a[i-1].x
f[i][k]=cost[i]*k+min{f[i-1][j]-cost[i]*j+dis*j*j};
显然f[i][k]只与f[i-1][k]有关所以省略一维,并且先插入队列(此时为f[i-1][k]的值),然后再更新为f[i][k]
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
using namespace std;
;
struct node{
int x,f;
LL c;
}a[maxn];
struct que{
int v;
LL val;
}q[maxn*];
int K,end,n;
LL f[maxn];
bool cmp(node a, node b){
return a.x<b.x;
}
int main(){
scanf("%d%d%d", &K, &end, &n);
; i<=n; i++) scanf("%d%d%lld", &a[i].x, &a[i].f, &a[i].c);
sort(a+,a++n,cmp);
memset(f,0x3f3f3f3f,sizeof(f));
f[]=;
; i<=n; i++){
, tail=;
LL dis=a[i].x-a[i-].x;
q[tail].v=,q[tail].val=,tail++;
; j<=K; j++){
LL now=f[j]-(LL)j*a[i].c+dis*(LL)j*(LL)j;
].val>=now) tail--;
q[tail].v=j, q[tail].val=now, tail++;
while (head<tail && q[head].v+a[i].f<j) head++;
f[j]=q[head].val+(LL)j*a[i].c;
}
}
printf("%lld\n", f[K]+(LL)K*(LL)K*(LL)(end-a[n].x));
;
}
2060
树形dp的入门水题。。
f[u]表示选u的最大值
g[u]表示不选u的最大值
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
;
struct node{
int to,next;
}e[maxn];
int g[maxn],f[maxn],n,head[maxn],tot,u,v;
void insert(int u, int v){
e[++tot].to=v; e[tot].next=head[u]; head[u]=tot;
}
void dfs(int x, int fa){
g[x]=; f[x]=;
; i=e[i].next){
int v=e[i].to;
if (v==fa) continue;
dfs(v,x);
g[x]+=max(g[v],f[v]);
f[x]+=g[v];
}
}
int main(){
scanf("%d", &n);
tot=; memset(head,-,sizeof(head));
; i<n; i++){
scanf("%d%d", &u, &v);
insert(u,v); insert(v,u);
}
dfs(,);
printf(],f[]));
;
}
bzoj 2058+2059+2060 Usaco2010 Nov的更多相关文章
- BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )
树形dp..水 ------------------------------------------------------------------------ #include<cstdio& ...
- 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 252 Solved: 1 ...
- 【BZOJ】2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛(树形dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=2060 裸的树形dp d[x][1]表示访问x的数量,d[x][0]表示不访问x的数量 d[x][1] ...
- 【BZOJ】2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
[算法]树形DP [题解]没有上司的舞会?233 f[x][0]=∑max(f[v][0],f[v][1]) f[x][1]=(∑f[v][0])+1 #include<cstdio> # ...
- bzoj 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛【树形dp】
设f[u][0/1]为u这个点不选/选,转移的时候从儿子转移,f[u][1]=sum(f[son][0])+1,f[u][0]=sum(max(f[son][0],f[e[i].to][1])) #i ...
- BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 树形DP
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...
- Bzoj 2058: [Usaco2010 Nov]Cow Photographs 题解
2058: [Usaco2010 Nov]Cow Photographs Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 190 Solved: 104[ ...
- BZOJ_2058_[Usaco2010 Nov]Cow Photographs_逆序对
BZOJ_2058_[Usaco2010 Nov]Cow Photographs_逆序对 题意: 奶牛的图片 Farmer John希望给他的N(1<=N<=100,000)只奶牛拍照片, ...
- [bzoj2058][Usaco2010 Nov]Cow Photographs_树状数组_动态规划
Cow Photographs bzoj-2058 Usaco-2010 Nov 题目大意:给定一个n的排列.每次操作可以交换相邻两个数.问将序列变成一个:$i,i+1,i+2,...,n,1,2,. ...
随机推荐
- CSS3 之 flexbox 响应式的未来
CSS3 之 flexbox 响应式的未来 flexbox 伸缩盒模型 . flex: CSS3中一个重要的而且非常有用的属性,用来制作弹性布局是非常的方便而又强大. . flex布局:旨在提供一个更 ...
- 【Android学习】《Android开发视频教程》第二季笔记(未完待续)
视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时22 Activity生命周期(一) 1.如何在一个应用中添加新 ...
- 64位操作系统 通过ODP.NET 访问ORACLE 11g
摘要:64位操作系统部署.NET 程序访问oracle时,无法连接问题.(注意:客户端是64位系统 ,服务端是否64位 还是32位无关.) 1.到oracle 官网搜索相关版本的 ODAC网址: ht ...
- javascript判断图片是否加载完成方法整理
有时候我们在前端开发工作中为了获取图片的信息,需要在图片加载完成后才可以正确的获取到图片的大小尺寸,并且执行相应的回调函数使图片产生某种显示效果.本文主要整理了几种常见的javascipt判断图片加载 ...
- BZOJ 4725: [POI2017]Reprezentacje ró?nicowe
Description 一个数列. \(a_1=1,a_2=2\) 当 \(n>2\) 时 \[a_n = \{ \begin {matrix} 2a_{n-1},\text{n is an ...
- web应用 http 响应 url uri
动态web 应用结构 WEB-INF --classes --lib web.xml 响应: url uri
- NEFU 558 迷宫寻路
题目链接 简单搜索题 #include <cstdio> #include <iostream> #include <cstring> using namespac ...
- SSO 单点登录实现
.NET基于Redis缓存实现单点登录SSO的解决方案 http://www.cnblogs.com/yinrq/p/5276628.html 共享cookie的方案 http://www.codep ...
- ASP.NET知识总结(7.状体保持)
客户端的状态保持方案:ViewState.隐藏域.Cookies.控件状态.URL查询参数 服务端的状态保持方案:Session(会话).Application.Caching(缓存).DataBas ...
- 仿qq联系人 学习笔记---ExpandableListActivity的使用
[转]原地址 http://blog.163.com/xygzx@126/blog/static/237809502011102010100331/ 效果显示图: 1.布局文件 main.xml(E ...