ZOJ Monthly, June 2018 Solution
A - Peer Review
Water.
#include <bits/stdc++.h>
using namespace std; int t, n; int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = ; i <= n; ++i) printf("0%c", " \n"[i == n]);
}
return ;
}
B - Boring Game
Unsolved.
C - Enigma Machine
Unsolved.
D - Number Theory
Unsolved.
E - Chasing
Solved.
题意:两个人在一个二维平面上,刚开始两个人在第二象限或者第三象限,A跑到Y轴B就不能追了,B的速度是A的K倍,求B能否追到B
思路:k < 0 那么必然不行
k == 0 判断是不是在同一高度,如果是判断起始位置
k > 0 假设y轴上存在某个点 使得A B 能够在该点相遇,列方程求解
#include<bits/stdc++.h> using namespace std; double xa, xb, ya, yb, k; int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%lf %lf %lf %lf %lf", &xa, &ya, &xb, &yb, &k);
if(k < 1.0)
{
puts("N");
}
else if(k == 1.0)
{
if(ya != yb) puts("N");
else if(xb < xa) puts("N");
else puts("Y");
}
else
{
double a = k * k - 1.0;
double b = -2.0 * k * k * ya + 2.0 * yb;
double c = k * k * xa * xa - xb * xb + k * k * ya * ya - yb * yb;
double d = b * b - 4.0 * a * c;
if(d >= ) puts("N");
else puts("Y");
}
}
return ;
}
F - Carrot Gathering
Unsolved.
G - Virtual Singers
Upsolved.
题意:
H - How Many Palindromes
Unsolved.
题意:
有$n个A数和m个B数 m <= n,对每个B数都匹配一个A数,使得\sum_{i = 1}^{i = m} |B[i] - A[i]| 最小$
思路:
考虑费用流的思想,维护三个堆
将$A、B 数放在一起排序,然后从左往右枚举$
$如果当前数是A$
如果左边有未匹配的$B_1数,那么与之匹配即可,此操作不用考虑反悔操作$
$因为假如有另一个B_2,使得B_2 与 这个A匹配更优$
那么后面必然要有另一个$A_2 来匹配这个B_1 $
$贡献是 A_2 - B_1 + B_2 - A_1 这个式子显然小于 A_2 - B_2 + A_1 - B_1$
因为这四个数字存在大小关系
$A_2 - B_1 大于 A_2 - B_2 + A_1 - B_1$
如果左边没有未匹配的$B_1数,那么考虑B的反悔操作是否更优$
$更优的话就反悔并且将代价取反并算上A往右匹配的贡献放入堆Q_A中, 相当于反悔操作$
$如果当前数是B$
$如果左边有未匹配的A数,那么与之匹配,并且将代价取反并且算上B往右匹配的贡献放入堆Q_C中$
否则放入堆$Q_B$中表示还未匹配
#include <bits/stdc++.h>
using namespace std; #define ll long long
#define N 100010
int t, n, m; ll x;
struct node
{
ll v; int Type;
node () {}
node (ll v, int Type) : v(v), Type(Type) {}
bool operator < (const node &r) const { return v < r.v; }
}arr[N << ];
priority_queue <ll, vector <ll> , greater <ll> > A, B, C; int main()
{
scanf("%d", &t);
while (t--)
{
while (!A.empty()) A.pop();
while (!B.empty()) B.pop();
while (!C.empty()) C.pop();
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i)
{
scanf("%lld", &x);
arr[i] = node(x, );
}
for (int i = ; i <= m; ++i)
{
scanf("%lld", &x);
arr[n + i] = node(x, );
}
sort(arr + , arr + + n + m);
ll res = ;
for (int i = ; i <= n + m; ++i)
{
if (arr[i].Type == )
{
if (!A.empty())
{
x = A.top(); A.pop();
res += arr[i].v + x;
C.push(-x - * arr[i].v);
}
else
B.push(-arr[i].v);
}
else
{
if (!B.empty())
{
x = B.top(); B.pop();
res += arr[i].v + x;
}
else if (!C.empty())
{
x = C.top();
if (arr[i].v + x < )
{
C.pop();
res += arr[i].v + x;
A.push(-x - * arr[i].v);
}
else
A.push(-arr[i].v);
}
else
A.push(-arr[i].v);
}
}
printf("%lld\n", res);
}
return ;
}
I - District Division
Solved.
题意:给出一棵树,求能不能分成$\frac{n}{k}$ 个连通块,使得每块个数为k
思路:如果存在合法分法,那么必然存在某个子树大小为k,DFS下去,再回溯上来判断即可,遇到大小为k的子树就分块
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + ; int n, k; int val[maxn];
int fa[maxn];
int vis[maxn];
int cnt;
vector<int>root;
vector<int>G[maxn]; void Init()
{
cnt = ;
root.clear();
for(int i = ; i <= n; ++i)
{
val[i] = vis[i] = ;
fa[i] = i;
G[i].clear();
}
} void DFS(int u, int pre)
{
val[u] = ;
fa[u] = pre;
for(int i = , len = G[u].size(); i < len; ++i)
{
int v = G[u][i];
if(v == pre) continue;
DFS(v, u);
val[u] += val[v];
}
if(val[u] == k)
{
cnt++;
val[u] = ;
root.push_back(u);
}
} void DFS2(int u, int pre)
{
if(vis[u]) return ;
vis[u] = ;
printf("%d%c", u, " \n"[cnt == k]);
cnt++;
for(int i = , len = G[u].size(); i < len; ++i)
{
int v = G[u][i];
if(v == pre) continue;
DFS2(v, u);
}
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &n, &k);
Init();
for(int i = ; i < n; ++i)
{
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
DFS(, -);
if(cnt != n / k)
{
puts("NO");
continue;
}
puts("YES");
for(int i = , len = root.size(); i < len; ++i)
{
cnt = ;
DFS2(root[i], fa[root[i]]);
}
}
return ;
}
J - Good Permutation
Solved.
题意:每次能够交换相邻两个数,求最少交换次数使得满足题目给出的序列条件
思路:通过枚举每次在第一位的数,从而递推下一种状态,比如12345推出51234即加上5移到第一位的步数再减去5带来的贡献
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + ; int n;
int arr[maxn];
int pos[maxn];
int a[maxn]; int lowbit(int p)
{
return p & (-p);
} void update(int p)
{
while(p <= n)
{
a[p]++;
p += lowbit(p);
}
} int query(int p)
{
int res = ;
while(p)
{
res += a[p];
p -= lowbit(p);
}
return res;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
ll ans = ;
for(int i = ; i <= n; ++i) a[i] = ;
for(int i = ; i <= n; ++i)
{
scanf("%d", arr + i);
pos[arr[i]] = i;
}
for(int i = n; i >= ; --i)
{
ans += query(arr[i] - );
update(arr[i]);
}
ll tmp = ans;
for(int i = ; i <= n; ++i)
{
tmp = tmp + n - pos[i] - (pos[i] - );
ans = min(ans, tmp);
}
printf("%lld\n", ans);
}
return ;
}
ZOJ Monthly, June 2018 Solution的更多相关文章
- ZOJ Monthly, March 2018 Solution
A - Easy Number Game 水. #include <bits/stdc++.h> using namespace std; #define ll long long #de ...
- ZOJ Monthly, January 2018 Solution
A - Candy Game 水. #include <bits/stdc++.h> using namespace std; #define N 1010 int t, n; int a ...
- ZOJ 4010 Neighboring Characters(ZOJ Monthly, March 2018 Problem G,字符串匹配)
题目链接 ZOJ Monthly, March 2018 Problem G 题意 给定一个字符串.现在求一个下标范围$[0, n - 1]$的$01$序列$f$.$f[x] = 1$表示存在一种 ...
- ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树 + 永久标记)
题目链接 ZOJ Monthly, March 2018 Problem F 题意很明确 这个模数很奇妙,在$[0, mod)$的所有数满足任意一个数立方$48$次对$mod$取模之后会回到本身. ...
- ZOJ Monthly, March 2018 题解
[题目链接] A. ZOJ 4004 - Easy Number Game 首先肯定是选择值最小的 $2*m$ 进行操作,这些数在操作的时候每次取一个最大的和最小的相乘是最优的. #include & ...
- ZOJ Monthly, January 2018 训练部分解题报告
A是水题,此处略去题解 B - PreSuffix ZOJ - 3995 (fail树+LCA) 给定多个字符串,每次询问查询两个字符串的一个后缀,该后缀必须是所有字符串中某个字符串的前缀,问该后缀最 ...
- ZOJ Monthly, June 2014 月赛BCDEFGH题题解
比赛链接:点击打开链接 上来先搞了f.c,,然后发现状态不正确,一下午都是脑洞大开,, 无脑wa,无脑ce...一样的错犯2次.. 硬着头皮搞了几发,最后20分钟码了一下G,不知道为什么把1直接当成不 ...
- ZOJ Monthly, January 2018
A 易知最优的方法是一次只拿一颗,石头数谁多谁赢,一样多后手赢 #include <map> #include <set> #include <ctime> #in ...
- ZOJ Monthly, March 2018
A. Easy Number Game 贪心将第$i$小的和第$2m-i+1$小的配对即可. #include<cstdio> #include<algorithm> usin ...
随机推荐
- storm中的基本概念
Storm是一个流计算框架,处理的数据是实时消息队列中的,所以需要我们写好一个topology逻辑放在那,接收进来的数据来处理,所以是通过移动数据平均分配到机器资源来获得高效率. Storm的优点是全 ...
- 【RF库Collections测试】Copy Dictionary
Name: Copy DictionarySource:Collections <test library>Arguments:[ dictionary ]Returns a copy o ...
- 【RF库Collections测试】Convert To List
Name:Convert To ListSource:Collections <test library>Arguments:[ item ]Converts the given `ite ...
- Python 处理输入输出
sys.stdin.read() 用于接收标准输入,也就是让用户通过键盘进行输入sys.stdout.write() 用于打印标准输出,也就是把输入的数据输出到屏幕sys.stderr.write() ...
- cocos2d-x游戏引擎核心之十——网络通信
一.建立基本的http通信并得到返回信息 1.创建cocos2dx工程 2.项目引用外部库 如果要使用cocos2dx的CCHttpClient来进行网络访问,则需要引入cocos2dx的相关库,详细 ...
- shiro-filter执行流程
web中 在xml中配置 <filter> <filter-name>shiroFilter</filter-name> <filter-class>o ...
- linux的setup命令设置网卡和防火墙等
以前在centos上配置网卡都是纯命令行,今天发现linux原来还有一个setup那么好用的命令,真是相见恨晚,以后防火墙.网卡.其他网络配置.系统配置(开机启动项)都可用他来完成了
- poj_3630 trie树
题目大意 给定一系列电话号码,查看他们之间是否有i,j满足,号码i是号码j的前缀子串. 题目分析 典型的trie树结构.直接使用trie树即可.但是需要注意,若使用指针形式的trie树,则在大数据量下 ...
- JS-制作留言提交系统(支持ctrl+回车)
弹出键值说明: //console.log(ev.keyCode)//回车:13//ctrl:17 <!DOCTYPE html> <html> <head> &l ...
- 【BZOJ4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+动态规划
[BZOJ4922][Lydsy六月月赛]Karp-de-Chant Number Description 卡常数被称为计算机算法竞赛之中最神奇的一类数字,主要特点集中于令人捉摸不透,有时候会让水平很 ...