三道金组比较容易的题目。。

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的更多相关文章

  1. BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )

    树形dp..水 ------------------------------------------------------------------------ #include<cstdio& ...

  2. 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛

    2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 252  Solved: 1 ...

  3. 【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] ...

  4. 【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> # ...

  5. 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 ...

  6. BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 树形DP

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...

  7. Bzoj 2058: [Usaco2010 Nov]Cow Photographs 题解

    2058: [Usaco2010 Nov]Cow Photographs Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 190  Solved: 104[ ...

  8. BZOJ_2058_[Usaco2010 Nov]Cow Photographs_逆序对

    BZOJ_2058_[Usaco2010 Nov]Cow Photographs_逆序对 题意: 奶牛的图片 Farmer John希望给他的N(1<=N<=100,000)只奶牛拍照片, ...

  9. [bzoj2058][Usaco2010 Nov]Cow Photographs_树状数组_动态规划

    Cow Photographs bzoj-2058 Usaco-2010 Nov 题目大意:给定一个n的排列.每次操作可以交换相邻两个数.问将序列变成一个:$i,i+1,i+2,...,n,1,2,. ...

随机推荐

  1. Coursera-Getting and Cleaning Data-week4-R语言中的正则表达式以及文本处理

    博客总目录:http://www.cnblogs.com/weibaar/p/4507801.html Thursday, January 29, 2015 补上第四周笔记,以及本次课程总结. 第四周 ...

  2. Authcode()

    加密解密函数Authcode(): 1.  // 参数解释   2. // $string: 明文 或 密文   3. // $operation:DECODE表示解密,其它表示加密   4. //  ...

  3. LCTT 三岁啦

    导读 不知不觉,LCTT 已经成立三年了,对于我这样已经迈过四张的人来说,愈发的感觉时间过得真快.这三年来,我们 LCTT 经历了很多事情,有些事情想起来仍恍如昨日. 三年前的这一天,我的一个偶发的想 ...

  4. sscanf提取字符串中的数据php

    1.需求 理解sscanf的作用 2.例子 $str = "age:30 weight:60kg"; sscanf($str,"age:%d weight:%dkg&qu ...

  5. 【Python基础学习二】定义变量、判断、循环、函数基本语法

    先来一个愉快的Hello World吧,就是这么简单,不需要写标点符号,但是需要严格按照缩进关系,Python变量的作用域是靠tab来控制的. print("Hello World" ...

  6. BZOJ 4723: [POI2017]Flappy Bird

    Description 从一个点到一条直线,每次纵坐标只能增加或减少1,有些位置有障碍,求最少增加步数. Sol 贪心. 或许是贪心吧...反正在可到达的范围内,纵坐标尽量小... 做的时候维护一下两 ...

  7. 【ZJOI2013】k大数查询 BZOJ 3110

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...

  8. xampp本地配置多域名

    xampp内建apache服务器,也是可以配置vhosts的 1. 打开httpd.conf文件,搜索vhosts 发现这句话: # Virtual hosts Include conf/extra/ ...

  9. java18

    1:Map(掌握) (1)将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. (2)Map和Collection的区别? A:Map 存储的是键值对形式的元素,键唯一,值可以重 ...

  10. 《C#高级编程》之委托学习笔记 (转载)

    全文摘自 http://www.cnblogs.com/xun126/archive/2010/12/30/1921551.html 写得不错,特意备份!并改正其中的错误代码..     正文: 最近 ...