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,. ...
随机推荐
- [Search Engine] 搜索引擎技术之倒排索引
倒排索引是搜索引擎中最为核心的一项技术之一,可以说是搜索引擎的基石.可以说正是有了倒排索引技术,搜索引擎才能有效率的进行数据库查找.删除等操作. 1. 倒排索引的思想 倒排索引源于实际应用中需要根据属 ...
- URL、URN、URI的区别?
URL.URN.URI区别 既然Web应用程序的文件等资源是放在服务器上,而服务器是因特网(Internet)上的主机,当然必须要有个方法,告诉浏览器到哪里取得文件等资源.通常会听到有人这么说:“你要 ...
- 【重装系统】线上Linux服务器(2TB)分区参考方案
如果是线上服务器,假设它是 2TB 的 SATA 硬盘.8GB 内存,建议按如下方式进行分区: / 20480M(20G)(主分区) /boot 128M swap 10240M /data 2016 ...
- 如何取消 DiscuzX 帖子被系统自动隐?
设置路径: 全局 -> 站点功能 -> 帖子阅读 -> 启用隐藏水帖,选择“否”
- JsonFormatter PrettyPrint
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- PYTHON 深拷贝,浅拷贝
声明:本篇笔记,模仿与其它博客中的内容 浅拷贝 浅拷贝,在内存中只额外创建第一层数据 import copy n1 = {"k1": "wu", "k ...
- Python演讲笔记1
参考: 1. The Clean Architecture in Python (Brandon Rhodes) 2. Python Best Practice Patterns (Vladimir ...
- 使用eclipse+fiddler+微信web开发者工具调试本地微信页面
前面已经说了调试服务器上的微信页面,放链接:http://www.cnblogs.com/Gabriel-Wei/p/5977850.html 还有fiddler调试链接:http://www.cnb ...
- [LeetCode] Reverse Linked List
Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...
- Jetty使用教程(四:24-27)—Jetty开发指南
二十四.处理器(Handler ) 24.1 编写一个常用的Handler Jetty的Handler组件用来处理接收到的请求. 很多使用者不需要编写Jetty的Handler ,而是通过使用Serv ...