三道好像都是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=;
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=;t<=T;t++)
{
printf("Case #%d:\n",t);
tmp=,sum=;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++) scanf("%LF",&a[i]);
if(n==k) { cout<<"0.000000"<<endl; continue; }
sort(a+,a++n,cmp);
for(int i=;i<=n-k;i++) tmp+=a[i]*a[i],sum+=a[i];ave=sum/(n-k);
ans=tmp-*ave*sum+(n-k)*ave*ave;
for(int l=,r=n-k+;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-*ave*sum+(n-k)*ave*ave);
}
printf("%.8LF\n",ans);
}
return ;
}

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=;
const long long p=1e9+;
int n,cnt,pr[Mx],vis[Mx],tran[][];
void pre()
{
for(int i=;i<=;i++)
{
int jud=;
for(int j=;j*j<=i;j++)
if(i%j==) jud=;
if(jud) pr[++cnt]=i;
}
}
long long power(long long a,long long b)
{
long long c=;
while(b)
{
if(b&) c=c*a%p;
a=a*a%p;
b>>=;
}
return c;
}
int gauss()
{
int i=;
for(int j=;i<=cnt&&j<=n;i++,j++)
{
int tmp=i;
for(int k=i+;k<=cnt;k++)
if(tran[k][j]>tran[tmp][j]) tmp=k;
if(tmp!=i)
for(int k=j;k<=n+;k++) swap(tran[tmp][k],tran[i][k]);
if(tran[i][j]==) { i--; continue; }
for(int k=i+;k<=cnt;k++)
{
if(!tran[k][j]) continue;
for(int l=j;l<=n+;l++)
tran[k][l]=tran[k][l]^tran[i][l];
}
}
return n-(i-);
}
int main()
{
pre();
int T; scanf("%d",&T);
for(int t=;t<=T;t++)
{
memset(tran,,sizeof(tran));
printf("Case #%d:\n",t);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
long long x; scanf("%lld",&x);
for(int j=;j<=cnt;j++)
{
int tot=;
while(x%pr[j]==)
x/=pr[j],tot++;
tran[j][i]=tot&;
}
}
printf("%lld\n",(long long) power(,gauss())-);
}
return ;
}

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 = ;
int to[N << ], nxt[N << ], 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 * ];
struct Event{
int l, r, y, val, rank;
}events[N * ];
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][]; int LCA(int x, int y){
if (dep[x] < dep[y]) swap(x, y);
int t = dep[x] - dep[y];
for(int i = ; i < ; i ++) if (t >> i & ) x = fa[x][i];
for(int i = ; i >= ; i --)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
if (x == y) return x;
return fa[x][];
} void dfs(int x){
st[x] = ++ dfs_clock;
for(int i = ; ( << i) <= dep[x]; i ++)
fa[x][i] = fa[fa[x][i - ]][i - ];
for(int i = head[x]; i; i = nxt[i])
if (st[to[i]] == ){
dep[to[i]] = dep[x] + ;
fa[to[i]][] = x;
dfs(to[i]);
}
ed[x] = dfs_clock;
} struct node{
int tag, max;
}t[N << ];
#define mid ((l + r) >> 1)
#define L (o << 1)
#define R (o << 1 | 1)
#define lch L, l, mid
#define rch R, mid + 1, r void 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 = ;
}
}
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 = ;
scanf("%d", &T);
while(cs < T){
printf("Case #%d: ", ++ cs); memset(head, , sizeof head);
memset(st, , sizeof st);
memset(fa, , sizeof fa);
memset(dep, , sizeof dep);
memset(t, , sizeof t);
cnt = dfs_clock = ;
scanf("%d%d", &n, &m);
int x, y;
for(int i = ; i < n; i ++){
scanf("%d%d", &x, &y);
add(x, y);
}
dfs();
// for(int i = 1; i <= n; i ++) printf("st[%d] = %d ed[%d] = %d\n", i, st[i], i, ed[i]);
int tot = , num = , ans = , tmp = ;
for(int i = ; 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][]){
rec[++ tot] = (Rec){st[to[j]], ed[to[j]], st[to[j]], ed[to[j]], -a[i].val};
}
if ( <= st[a[i].key] - && ed[a[i].key] + <= n){
rec[++ tot] = (Rec){, st[a[i].key] - , ed[a[i].key] + , n, -a[i].val};
rec[++ tot] = (Rec){ed[a[i].key] + , n, , st[a[i].key] - , -a[i].val};
}
if ( <= st[a[i].key] - )
rec[++ tot] = (Rec){, st[a[i].key] - , , st[a[i].key] - , -a[i].val};
if (ed[a[i].key] + <= n)
rec[++ tot] = (Rec){ed[a[i].key] + , n, ed[a[i].key] + , 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][] && LCA(to[j], a[i].chest) == to[j]){
y = to[j];
break;
}
if ( <= st[y] - ) rec[++ tot] = (Rec){, st[y] - , st[a[i].chest], ed[a[i].chest], a[i].val};
if (ed[y] + <= n) rec[++ tot] = (Rec){ed[y] + , 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][] && LCA(to[j], a[i].key) == to[j]){
y = to[j];
break;
}
if ( <= st[y] - ) rec[++ tot] = (Rec){st[a[i].key], ed[a[i].key], , st[y] - , a[i].val};
if (ed[y] + <= n) rec[++ tot] = (Rec){st[a[i].key], ed[a[i].key], ed[y] + , 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 = ;
for(int i = ; 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, };
events[++ num] = (Event){rec[i].x1, rec[i].x2, rec[i].y2 + , -rec[i].val, };
}
sort(events + , events + num + );
for(int i = ; i <= num; i ++){
if (events[i].y > events[i - ].y || i == ) ans = max(ans, t[].max);
update(, , n, events[i].l, events[i].r, events[i].val);
if (i == num) ans = max(ans, t[].max);
// printf("%d\n", t[1].max);
}
printf("%d\n", ans + tmp);
}
return ;
}

  

2017-3-01 test的更多相关文章

  1. 调试大叔V1.0.1(2017.09.01)|http/s接口调试、数据分析程序员辅助开发神器

    2017.09.01 - 调试大叔 V1.0.1*支持http/https协议的get/post调试与反馈:*可保存请求协议的记录:*内置一批动态参数,可应用于URL.页头.参数:*可自由管理cook ...

  2. Feed back TFS 2017 RC upgrade status to product team in product group 2017.03.01

    作为微软的MVP,有一个我最喜欢的好处,就是可以与产品组(产品研发部门)有零距离接触,可以最先拿到即将发版的产品,并且和产品组沟通,对产品中出现的问题实时反馈. 看到TFS产品组吸收了自己的建议和反馈 ...

  3. Cheatsheet: 2017 03.01 ~ 03.31

    Web New Year, New Blog Day 10 - Using JetBrains Rider with a .NET Core Console Application JavaScrip ...

  4. Work 3(工作类) (2017.07.01)

  5. ROS机器人程序设计(原书第2版)补充资料 (零) 源代码、资料和印刷错误修订等 2017年01月01日更新

    ROS机器人程序设计(原书第2版)补充资料 (零) 源代码等 ROS官网 版)部分内容修订 页:第1行,删去$ 页:第6行,float64 y 前面加一个空格 页:中间创建主题:下面程序不用换行,(& ...

  6. Cheatsheet: 2017 10.01 ~ 12.31

    Mobile Updating Your App for iOS 11 Get Started With Natural Language Processing in iOS 11 Getting S ...

  7. Cheatsheet: 2017 08.01 ~ 09.30

    Golang Building a Worker Pool in Golang A Million WebSockets and Go Writing Plugins in Go imgproxy:R ...

  8. Cheatsheet: 2017 07.01 ~ 07.31

    Other 8 Key Application Performance Metrics & How to Measure Them The Code Review: The Most Impo ...

  9. Cheatsheet: 2017 06.01 ~ 06.30

    .NET Porting a .NET Framework library to .NET Core Performance Improvements in .NET Core High-perfor ...

  10. Cheatsheet: 2017 05.01 ~05.31

    Web Configuring Your .npmrc for an Optimal Node.js Environment Web Developer Security Checklist HTTP ...

随机推荐

  1. 【wiki】红帽linux

    Red Hat Enterprise Linux From Wikipedia, the free encyclopedia wiki 上面红帽的版本信息. https://en.wikipedia. ...

  2. java编程基础——二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 题目代码 /** * @program: JavaCode * @description: 操作给定的二叉树,将其变换为源二叉树的镜像. * 二 ...

  3. Scanner和 Random类,控制语句的例题,商品管理(直接赋值)

    Scanner类的使用: import java.util.Scanner; class Demo02 { public static void main(String[] args) { //1.导 ...

  4. IBM MQ安装

    一.下载MQ 可以去官方网站下载,我这次下了一个下载器从官方,然后通过下载器进行MQ的下载. 地址:https://www.ibm.com/developerworks/cn/downloads/ws ...

  5. jrtplib库使用简解

    RTP有效载荷类型即时间截解释 =============================== https://www.cnblogs.com/wyqfighting/archive/2013/03/ ...

  6. Linux常用关机重启命令

    # shutdown -h 10       //计算机将在10分钟后 关机,且会显示在登录用户的当前屏幕中 # shutdown -h now    //立即 关机 # shutdown -h 20 ...

  7. Python通过RabbitMQ实现RPC

    Client端代码: #!/usr/bin/env python # -*- coding:utf-8 -*- import pika import uuid import time class Fi ...

  8. windows环境下安装npm、cnpm、bower

    什么是npm.cnpm.bower? 简单地说,就是帮你下载好你需要的css或者js库,而且三者功能也都是一样的.那为什么要下载这3个不同的呢?据说npm容易被墙……而cnpm是淘宝的镜像,所以通常用 ...

  9. 【CSS】css控制模块到顶层或底层

    举例子,分别有div1和div2现要把div1控制在div2的顶层,可以这样做: } div.div2{} 两个要点:一.设置div的position为absolute,即绝对定位.二.z-index ...

  10. 动态规划:HDU1248-钱币兑换问题

    解题心得: (青蛙跳台阶:有n阶台阶,青蛙可以一次跳一阶也可以一次跳两阶,问总共有多好中跳法) 1.之前把这个问题的思路弄错了,以为是递推,就像青蛙跳台阶,用斐波那契求解.但是用斐波那契肯定会超范围. ...