三道好像都是HDU上的题QAQ

题目名称都没改,差评

T1:http://acm.hdu.edu.cn/showproblem.php?pid=5073

   被卡精度了QAQ

   先排一发序,然后发现最后未动过的点一定是一段连续的区间,且其他点都被移动至其平均数处

   所以直接O(n)乱搞即可

   P.S.HDU上好像不能用long double

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int Mx=50010;
int n,k;
long double ans,tmp,sum,ave,a[Mx];
bool cmp(long double a,long double b) { return a<b; }
int main()
{int T; scanf("%d",&T);for(int t=1;t<=T;t++){printf("Case #%d:\n",t);tmp=0,sum=0;scanf("%d%d",&n,&k);for(int i=1;i<=n;i++) scanf("%LF",&a[i]);if(n==k) { cout<<"0.000000"<<endl; continue; }sort(a+1,a+1+n,cmp);for(int i=1;i<=n-k;i++) tmp+=a[i]*a[i],sum+=a[i];ave=sum/(n-k);ans=tmp-2*ave*sum+(n-k)*ave*ave;for(int l=1,r=n-k+1;r<=n;l++,r++){tmp+=a[r]*a[r]-a[l]*a[l];sum+=a[r]-a[l];ave=sum/(n-k);ans=min(ans,tmp-2*ave*sum+(n-k)*ave*ave);}printf("%.8LF\n",ans);}return 0;
}

T2:http://acm.hdu.edu.cn/showproblem.php?pid=5833

   一言不合就数论QAQ

   首先先预处理2000以内的素数,然后将a[]质因数分解

   我们发现,一个完全平方数的各个质因子都是偶数次方,所以每次乘起来就等价于把系数^1

   所以可以构造转移矩阵,第i行j列表示第j个素数在a[i]中的系数%2

   高斯一发然后答案即为pow(2,自由元的个数)-1,记得开long long

   P.S.自由元:消完之后一行都是0

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int Mx=2010;
const long long p=1e9+7;
int n,cnt,pr[Mx],vis[Mx],tran[310][310];
void pre()
{for(int i=2;i<=2000;i++){int jud=1;for(int j=2;j*j<=i;j++)if(i%j==0) jud=0;if(jud) pr[++cnt]=i;}
}
long long power(long long a,long long b)
{long long c=1;while(b){if(b&1) c=c*a%p;a=a*a%p;b>>=1;}return c;
}
int gauss()
{int i=1;for(int j=1;i<=cnt&&j<=n;i++,j++){int tmp=i;for(int k=i+1;k<=cnt;k++)if(tran[k][j]>tran[tmp][j]) tmp=k;if(tmp!=i)for(int k=j;k<=n+1;k++) swap(tran[tmp][k],tran[i][k]);if(tran[i][j]==0) { i--; continue; }for(int k=i+1;k<=cnt;k++){if(!tran[k][j]) continue;for(int l=j;l<=n+1;l++)tran[k][l]=tran[k][l]^tran[i][l];}}return n-(i-1);
}
int main()
{pre();int T; scanf("%d",&T);for(int t=1;t<=T;t++){memset(tran,0,sizeof(tran));printf("Case #%d:\n",t);scanf("%d",&n);for(int i=1;i<=n;i++){long long x; scanf("%lld",&x);for(int j=1;j<=cnt;j++){int tot=0;while(x%pr[j]==0)x/=pr[j],tot++;tran[j][i]=tot&1;}}printf("%lld\n",(long long) power(2,gauss())-1);}return 0;
}

T3:http://acm.hdu.edu.cn/showproblem.php?pid=5770

   好麻烦啊QAQ调了半天调不出来只能把std放上去了QAQ

   orz ZSQ:http://blog.csdn.net/v5zsq/article/details/52170616

   对于一组钥匙和宝箱u,v及其lca,我们可以分类讨论,有以下四种情况

   1、u!=lca&&v!=lca:要想拿到宝箱,起点必须在u的子树中,终点必须在v的子树中

   2、u==lca:起点不能在u的子树中,终点必须在v的子树中

   3、v==lca:起点不能在v的子树中,终点必须在v的子树中

   4、u==v:起点和终点在lca的不同子树中 或 起点or终点在lca的子树中,另一个点不在lca的子树中

    这种情况为保证复杂度所以要反过来求不包含该节点的路径

    ①起点和终点都在lca的同一个儿子中

    ②起点和终点都不在在lca的子树中,即起点和终点x的dfs序都满足(x<l || x>r)

   考虑维护DFS序,将以i为根的子树处理成一个区间[l,r],则每个宝箱可以映射为一个带权矩阵,对应上述情况

   建立一个二维平面,平面上的点(x,y)表示从x走到y的价值,点权即为包含该点的所有矩阵的和

   最后用扫描线+线段树即可解决

   默默吐槽一下,std代码好吃藕啊QAQ(逃~

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;const int N = 200010;
int to[N << 1], nxt[N << 1], head[N], cnt;
void add(int x, int y){to[++ cnt] = y; nxt[cnt] = head[x]; head[x] = cnt;to[++ cnt] = x; nxt[cnt] = head[y]; head[y] = cnt;
}struct Rec{int x1, x2, y1, y2, val;
}rec[N * 10];
struct Event{int l, r, y, val, rank;
}events[N * 10];
bool operator < (const Event &a, const Event &b){return a.y < b.y || (a.y == b.y && a.rank < b.rank);
}struct treasure{int key, chest, val;
}a[N];
int st[N], ed[N], n, m, dfs_clock, dep[N], fa[N][20];int LCA(int x, int y){if (dep[x] < dep[y]) swap(x, y);int t = dep[x] - dep[y];for(int i = 0; i < 20; i ++) if (t >> i & 1) x = fa[x][i];for(int i = 19; i >= 0; i --)if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];if (x == y) return x;return fa[x][0];
}void dfs(int x){st[x] = ++ dfs_clock;for(int i = 1; (1 << i) <= dep[x]; i ++)fa[x][i] = fa[fa[x][i - 1]][i - 1];for(int i = head[x]; i; i = nxt[i])if (st[to[i]] == 0){dep[to[i]] = dep[x] + 1;fa[to[i]][0] = x;dfs(to[i]);}ed[x] = dfs_clock;
}struct node{int tag, max;
}t[N << 2];
#define mid ((l + r) >> 1)
#define L (o << 1)
#define R (o << 1 | 1)
#define lch L, l, mid
#define rch R, mid + 1, rvoid Push_up(int o){t[o].max = max(t[L].max, t[R].max);
}
void change(int o, int v){t[o].tag += v; t[o].max += v;
}
void Push_down(int o){if (t[o].tag){change(L, t[o].tag);change(R, t[o].tag);t[o].tag = 0;}
}
void update(int o, int l, int r, int ql, int qr, int v){if (ql > qr) return;if (ql <= l && qr >= r) change(o, v);else{Push_down(o);if (ql <= mid) update(lch, ql, qr, v);if (qr >  mid) update(rch, ql, qr, v);Push_up(o);}
}int main(){int T, cs = 0;scanf("%d", &T);while(cs < T){printf("Case #%d: ", ++ cs);memset(head, 0, sizeof head);memset(st, 0, sizeof st);memset(fa, 0, sizeof fa);memset(dep, 0, sizeof dep);memset(t, 0, sizeof t);cnt = dfs_clock = 0;scanf("%d%d", &n, &m);int x, y;for(int i = 1; i < n; i ++){scanf("%d%d", &x, &y);add(x, y);}dfs(1);
//        for(int i = 1; i <= n; i ++) printf("st[%d] = %d ed[%d] = %d\n", i, st[i], i, ed[i]);int tot = 0, num = 0, ans = 0, tmp = 0;for(int i = 1; i <= m; i ++){scanf("%d%d%d", &a[i].key, &a[i].chest, &a[i].val);int lca = LCA(a[i].key, a[i].chest);if (a[i].key == a[i].chest){tmp += a[i].val;for(int j = head[a[i].key]; j; j = nxt[j])if (to[j] != fa[a[i].key][0]){rec[++ tot] = (Rec){st[to[j]], ed[to[j]], st[to[j]], ed[to[j]], -a[i].val};}if (1 <= st[a[i].key] - 1 && ed[a[i].key] + 1 <= n){rec[++ tot] = (Rec){1, st[a[i].key] - 1, ed[a[i].key] + 1, n, -a[i].val};rec[++ tot] = (Rec){ed[a[i].key] + 1, n, 1, st[a[i].key] - 1, -a[i].val};}if (1 <= st[a[i].key] - 1)rec[++ tot] = (Rec){1, st[a[i].key] - 1, 1, st[a[i].key] - 1, -a[i].val};if (ed[a[i].key] + 1 <= n)rec[++ tot] = (Rec){ed[a[i].key] + 1, n, ed[a[i].key] + 1, n, -a[i].val};}else if (a[i].key == lca){for(int j = head[a[i].key]; j; j = nxt[j])if (to[j] != fa[a[i].key][0] && LCA(to[j], a[i].chest) == to[j]){y = to[j];break;}if (1 <= st[y] - 1) rec[++ tot] = (Rec){1, st[y] - 1, st[a[i].chest], ed[a[i].chest], a[i].val};if (ed[y] + 1 <= n) rec[++ tot] = (Rec){ed[y] + 1, n, st[a[i].chest], ed[a[i].chest], a[i].val};}else if (a[i].chest == lca){for(int j = head[a[i].chest]; j; j = nxt[j])if (to[j] != fa[a[i].chest][0] && LCA(to[j], a[i].key) == to[j]){y = to[j];break;}if (1 <= st[y] - 1) rec[++ tot] = (Rec){st[a[i].key], ed[a[i].key], 1, st[y] - 1, a[i].val};if (ed[y] + 1 <= n) rec[++ tot] = (Rec){st[a[i].key], ed[a[i].key], ed[y] + 1, n, a[i].val};}else{rec[++ tot] = (Rec){st[a[i].key], ed[a[i].key], st[a[i].chest], ed[a[i].chest], a[i].val};}
//            if (rec[tot].x1 == 0) printf("%d %d %d lca = %d\n", a[i].key, a[i].chest, a[i].val, lca);
        }
//        for(int i = 1; i <= tot; i ++)
//            printf("%d %d %d %d %d\n", rec[i].x1, rec[i].x2, rec[i].y1, rec[i].y2, rec[i].val);int tt = 0;for(int i = 1; i <= tot; i ++){
//            if (rec[i].x1 == 0 && rec[i].x2 == 0) printf("%d\n", ++ tt);events[++ num] = (Event){rec[i].x1, rec[i].x2, rec[i].y1, rec[i].val, 1};events[++ num] = (Event){rec[i].x1, rec[i].x2, rec[i].y2 + 1, -rec[i].val, 0};}sort(events + 1, events + num + 1);for(int i = 1; i <= num; i ++){if (events[i].y > events[i - 1].y || i == 1) ans = max(ans, t[1].max);update(1, 1, n, events[i].l, events[i].r, events[i].val);if (i == num) ans = max(ans, t[1].max);
//            printf("%d\n", t[1].max);
        }printf("%d\n", ans + tmp);}return 0;
}

  

转载于:https://www.cnblogs.com/xiaoxubi/p/6484441.html

2017-3-01 test相关推荐

  1. javaScript ie8 不支持 new Date(2017-07);只支持new Date(2017/07/01)

    今天看以前代码的时候看到的JS的注释,记得当时调了老半天,ie8以上 以及谷歌,火狐没有这个问题. //ie8 不支持 new Date("2017-07");只支持new Dat ...

  2. 黑武器linux下载地址,酷毙了!暗黑版 Arch,BlackArch Linux 2017.03.01发布

    By - 3月, 2nd 2017 作者:闻其详 Arch 是大名鼎鼎的滚动更新的 Linux 发行版,江湖上哪个不知,谁个不晓,练就顶上的 Linux 功夫基本都不会绕路于它,然而就像杂粮中出有白米 ...

  3. 阿里数据库内核月报:2017年01月

    摘要: 阿里数据库内核月报:2017年01月 # 01 MySQL · 引擎特性 · InnoDB 同步机制 # 02 MySQL · myrocks · myrocks index conditio ...

  4. 跟锦数学2017年01月

    (170131) 设 $u$ 为 $n$ 维欧氏空间 $\bbR^5$ 中的单位向量, 定义 $T_u(x)=x-2\sef{x,u}u$. 现设 $\al,\be$ 是 $\bbR^5$ 中线性无关 ...

  5. 2017年01月。。

    2016年12月 - 2017年02月 ● 2017 2017=(987+654+321)+(1+2+3+4+5+6+7+8+9+10),2017=7^3+7^3+11^3,2+0+1+7=10.(1 ...

  6. Java的直接量——2017.08.01

    根据常量池的定义来比较下面结果: . package Collection;public class Constantpool {public static void main(String[] ar ...

  7. 2017.05.01

    15:09:25 刚开通博客,以后有什么随笔.记录就发这了 转载于:https://www.cnblogs.com/shadowdoor/p/6791965.html

  8. 阿里内核月报2017年01月

    Controlling access to the memory cache 控制对Cache的访问 cpu对内存的访问一直以来都会通过L1/L2/L3缓存来加速,我们都知道当你打算严肃地去考察性能问 ...

  9. 《电脑爱好者》2017 年 01 -06期

    猛击我下载  此PDF格式电子版需要PDF阅读器软件打开, 推荐个软件Foxit Reader(福昕PDF阅读器单文件版) :下载地址 本文转自haiyang45751CTO博客,原文链接:http: ...

  10. 2017.04.01号最新cpu天梯图 包含7代cpu

最新文章

  1. LeetCode-剑指 Offer 15. 二进制中1的个数
  2. 【MM配置】Delivery Costs 交货成本
  3. C#编写运行在Linux环境下的采用Mediainfo来获取多媒体文件信息的代码
  4. Python gevent高并发(限制最大并发数、协程池)
  5. 04简单线性回归实战
  6. git学习笔记-(15-远程跟踪分支)
  7. 【图论】spfa算法详解
  8. DirectX9.0b SDK下载地址及安装说明
  9. ubuntu1804安装python3.8+odoo14
  10. 增强游戏打击感可从游戏音效增强
  11. 阿里云盘迎来了第三方客户端小白羊版
  12. 情态动词+have+done用法整理
  13. 伯禹公益AI《动手学深度学习PyTorch版》Task 05 学习笔记
  14. VS2010开发体验系列之二 - 语言C#4.0
  15. CSS中的:before和 :after
  16. 性能测试结果分析结果
  17. ASSERT(FALSE)
  18. 大家都来学 Java(一)快乐入门
  19. 老人与海好词100英文带翻译_老人与海英文读后感100字
  20. STM32CUBEMX开发GD32F303(12)----输出PWM及修改PWM频率与占空比

热门文章

  1. VScode单步跟踪Nginx(虚拟机中搭建Nginx)源码
  2. [渝粤教育] 天水师范学院 离散数学 参考 资料
  3. 求解偏微分方程开源有限元软件deal.II学习--Step 48
  4. 教大家如何修改博客背景
  5. Codeforces Round #524 (Div. 2) Masha and two friends
  6. Git-Git库管理
  7. MySQL查询当天、本周,本月,上一个月的数据
  8. 卷积神经网络学习笔记与心得(2)数据集
  9. Django 2.0.1 官方文档翻译: 文档目录 (Page 1)
  10. Django框架基础之session