[Codeforces 864A]Fair Game】的更多相关文章

Description Petya and Vasya decided to play a game. They have n cards (n is an even number). A single integer is written on each card. Before the game Petya will choose an integer and after that Vasya will choose another integer (different from the n…
题意:有N个城市,M条双向道路连接两个城市,整个图保证连通.有K种物品,但每个城市只有一种,现在它们都需要S种物品来举办展览,可以去其他城市获取该城市的物品,花费是两城市之间的最短路径长度.求每个城市举办展览的最小花费. 分析:去某个城市获取第i种物品的最小距离,这个问题可以逆向求解.把拥有第i种物品的城市当作源点,BFS求出它们到其他城市的最短路.对K种物品都如此求一遍最短路. 计算结果的时候,排序后贪心地选择花费前S小的物品即可. #include<bits/stdc++.h> using…
题意:有N个点M条边的无向图,每个点有给定的ai(1<=ai<=K,K<=200)表示该点拥有的物品编号,保证1-K在N个点全部出现.求每个点收集S个不同的物品所要走过的最短路程(边的长度为1). 分析:N是1e5,如果直接对每个点搜索肯定超时.发现K的范围很小,而且1-K全部覆盖.那么考虑对所有1-K的值BFS,用一个二维数组dp[i][j]记录i点要获取编号为j的物品最少走过的路程,并对每个点取最小的S个物品对应的路径. #include<bits/stdc++.h> u…
大意:给一张图,每个图上有一个数,问以每个点为源点,经过的点包含k种数字的最小距离. 显然跑最短路会T,但我们注意到边权一定.某次学校考试就是类似题,可以bfs做,复杂度O(n),每种货物做一次,复杂度O(kn),n=1e5,k=100,稳了. #include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <cstdio> #inclu…
解题思路: 1.对物品i bfs,更新每个小镇j获得每个物品i的最短距离. 2.时间复杂度o(n*k),满足2s的要求. 代码: #include <iostream> #include <queue> #include <list> #include <algorithm> #include <stdio.h> #include <string.h> using namespace std; typedef long long ll…
codeforces 897A Scarborough Fair 题目链接: http://codeforces.com/problemset/problem/897/A 思路: 暴力大法好 代码: #include <iostream> #include <stdio.h> #include <string.h> using namespace std; typedef long long ll; int n,m; string s; int main() { ios…
Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description Some company is going to hold a fair in Byteland. There are $n$ towns in Byteland and $m$ two-way roads between towns. Of course, you can reach any t…
F. Bear and Fair Set 题目连接: http://www.codeforces.com/contest/628/problem/F Description Limak is a grizzly bear. He is big and dreadful. You were chilling in the forest when you suddenly met him. It's very unfortunate for you. He will eat all your coo…
E. The Fair Nut and Strings 题目链接:https://codeforces.com/contest/1084/problem/E 题意: 输入n,k,k代表一共有长度为n的多少个字符串.然后给出一个最小字符串s,最大字符串t,满足对于所有的k个字符串有s<=S<=t. 最后求满足条件的k个字符串(自己构造)的不同前缀数量的和. 题解: 这题很巧妙,设dp(i)表示长度为i的前缀的数量和,一开始有dp(1)=0. 后来随着长度的增加,我们每次可以在最后加一个a或者b,…
D. The Fair Nut and the Best Path 题目链接:https://codeforces.com/contest/1084/problem/D 题意: 给出一棵树,走不重复的路径,每到一个结点加上其权值,经过一条边减去其权值,路径中途减去后不能出现负数,问怎么选择路径可以让最后得到的最大. 题解: 这题考虑用dp来做. 我们定义dp[u]为走到u点的最大值,注意这里的方向,是走到u点.题目中的意思是路径不能走回头路. 对于一个父节点u,那么我们可以根据走到其儿子结点的最…