UVaLive 4628 Jack's socks (贪心)】的更多相关文章

题意:给定一个无向图,让你把所有点的和它的任意一个相邻点匹配起来,问你是方案是不是唯一,如果是,则输出方案. 析:贪心,很容易知道,如果一个点的度数是 1,那么它只有一个相邻点,这样的话,我们就可以把它和它相邻结点匹配,然后把与它相邻结点也相邻的点的度数都减 1,然后再找度数为 1 的点,直接找不到为止,如果最后所得到的匹配数是 n / 2 个,那么方案就是唯一,否则不是无解,就是多种方案. 代码如下: #pragma comment(linker, "/STACK:1024000000,102…
题意:给定 n 个区间,让你选出最多的区间,使得每个区间不相交. 析:贪心题,贪心策略是按右端点排序,然后按着选即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #…
分析:求出最大值和最小值比较简单,使用贪心法,求最小值的时候我们让所有的0尽可能的向后延迟就可以了,求最大值则相反. 关键在于求出可以组合出的数字个数. 这就是组合数学版的dp了,我们让dp[i][j]表示当前i个0,和前j个1被接收后所能形成的数字个数,初始条件为dp[0][0] = 1; 决策有两种,第一种转移到dp[i+1][j]也就是多接收一个0,这时候定义F1[]数组记录1的发送时间,F0[]数组记录0的发送时间,那么如上方程的转移条件为 F1[j+1]+d >= F0[i+1],也就…
题目链接 题意 给出正视图和侧视图,判断最少用几个立方体 分析 若存在高度相同的立方块,则以数目多的那面为准. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ; int num1[MAXN],num2[MAXN]; int n,m; int main(){ while (scanf("%d%d&qu…
本题思路不难,但是要快速准确的AC有点儿考验代码功力. 看了大白书上的标程,大有所获. 用map和vector的结合给输入分组,这个数据结构的使用非常精美,恰到好处. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include&l…
分析: 对于网格grid[i][j]如果放向上的管道,那么grid[i][k], k>j 就只能放向上的管道了. 那么定义dp[i][j]表示第i行,最后一个放向左的管道是j的最大总矿量. j = 0表示全放向上,j = m表示全放向左. 如果grid[i][j]要往放向上的管道的话,前提是grid[i-1][j]也要是向上的管道. 因此转移为dp[i][j] = max(dp[i-1][k]+sumA(1,j)+sumB(j+1,n) ), k >= j 对于N= 500这样的规模,转移有点…
分析: 状态是一些有序的集合,这些集合互不相交,并集为所有区域.显然枚举集合元素是哪些是无法承受的, 写出期望的计算式,会发现,当每个集合的大小确定了以后,概率大的优先访问是最优的. 因此先对u从大到小排序.定义状态f[i][j]表示从j开始往后分i组的最小期望. 转移是枚举划分k,则有f[i][j] = min{f[i-1][k]+(k-j)*(sum_u(j,n))},k>j 边界f[1][j] = (n-j+1)*(u[j]) 处理出u的前缀和 复杂度O(w*n^2) #include<…
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5922 题意: 给出 $N$ 个冰淇淋球,第 $i$ 个冰淇淋球大小为 $B_i$,现在已知冰淇淋球堆叠起来可组成一个冰淇淋. 对于上下相邻的两个冰淇淋球,只有上面的那个大小不…
题目传送门 /* 题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数 贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步就是答案了 详细解释:http://blog.csdn.net/u013625492/article/details/45640735 */ #include <cstdio> #include <algorithm> #include <cstring> #include…
题目传送门 /* 贪心:按照0或1开头,若不符合,选择后面最近的进行交换.然后选取最少的交换次数 */ #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <cmath> #include <vector> #include <map> #include <queue> using namesp…
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prep…
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=648&page=show_problem&problem=5159 In normal football games, the winner team gets 3 points, loser team gets 0 point, and if there is a drawgame, bot…
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others.One day, there is another tribe become their target. The strong tribe has decide to terminate them!!!There are m villages in the other tribe. Each vil…
题意:n个工人,有n件工作a,n件工作b,每个工人干一件a和一件b,a[i] ,b[i]代表工作时间,如果a[i]+b[j]>t,则老板要额外付钱a[i]+b[j]-t;现在要求老板付钱最少: 析:贪心策略,让大的和小的搭配,小的和大的搭配,是最优的. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include &l…
Prime Bases 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2226 Descriptionww.co Given any integer base b >= 2, it is well known that every positive integer n can be uniquely rep…
Robots 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/K Description http://7xjob4.com1.z0.glb.clouddn.com/168817810b54cfa4ffaebf73d3d1b0c5 Input The input consists of multiple test cases. First line contains a single integer t indicating…
题意:给定 n 个数,要求把其中两个分成一组,然后加和,问所有的都分好,最小数是几. 析:贪心策略,最大和是小的相加,就是最优的. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <functional> #include <cstdlib> #include <cma…
Let x1, x2,..., xm be real numbers satisfying the following conditions: a) -xi ; b) x1 + x2 +...+ xm = b *  for some integers  a and  b  (a > 0). Determine the maximum value of xp1 + xp2 +...+ xpm for some even positive integer p. Input Each input li…
题目连接:3971 - Assemble 题目大意:有若干个零件, 每个零件给出的信息有种类, 名称, 价格, 质量,  现在给出一个金额, 要求在这个金额范围内, 将每个种类零件都买一个, 并且尽量让这些零件中质量最小的越大, 输出质量最小的值. 解题思路:首先可以用二分搜索确定质量, 然后在搜索的过程中要判断这个质量是否能被满足, 判断函数可以用贪心, 在每一类的零件中选择价格最低且质量大于等于当前质量的零件.(事先按照价格大小排序). #include <stdio.h> #includ…
题目连接:2911 - Maximum 题目大意:给出m, p, a, b,然后xi满足题目中的两个公式, 要求求的 xp1 + xp2 +...+ xpm 的最大值. 解题思路:可以将x1 + x2 +...+ xm = b *  两端同时乘以根号a去计算.然后按照贪心的思想去计算. #include <stdio.h> #include <math.h> int l, r; double m, p, a, b, tmp, sum; int main() { while (sca…
补:华中VJ这个题目很多标程都不能AC了,包括我下面原本AC了的代码,再交就WA掉了,感觉是样例有问题呢-- 首先左边的是必须要选的,然后右边的需要注意,有些区间是可以舍掉的.1.区间里有两个不同的A. 2.区间里有一个A,而且这个A不是这个区间对应的A. 这个题我一开始错在了第2个判定条件上,我定义的id记录的是最后一个出现位置,不能进行判断,所以干脆在结构体里记录了他对应的A值. 舍掉后留下的区间,可以按照区间左边界排序,然后求交集即可. 总体来说,贪心的思想还是不难的,就是有一些细节需要注…
题目链接:https://vjudge.net/contest/244167#problem/F 题目: Given any integer base b ≥ 2, it is well known that every positive integer n can be uniquely represented in base b. That is, we can write n = a0 + a1 ∗b + a2 ∗b∗b + a3 ∗b∗b∗b + ... where the coeffi…
题意 等价于给一个数列,每次对一个长度为$K$的连续区间减一 为最多操作多少次 题解: 看样例猜的贪心,10分钟敲了个线段树就交了... 从1开始,找$[i,i+K]$区间的最小值,然后区间减去最小值,答案加上最小值即可... 这个思路,后来我看博客上的总结,应该是没问题的, 可是,依旧不知道线段树写的对不对..因为是假数据... #include <bits/stdc++.h> #define ll long long #define IO ios::sync_with_stdio(fals…
UVALive 8513 有2种人,每个人有自己的权值$A_i$ $B_i$ 当$A_i + B_i >=K$时 两个人可以配对 问最多多少人可以配对 解法 : 把$/{ A_i /}$ 排序 $/{ B_i /}$ 扔进多重集合里 每次在集合中找大于等于 $K - A_i$ 的第一个$B_i$ 然后删除元素 可以有小优化 但是实际上我不知道我是否正确...... 因为上面挂的数据是假数据,任何输出都能过.... 本来想把17年uvalive真题切一切的,结果一上来就被打脸了... #inclu…
题目链接  题意 工程师要安装n个服务,其中服务Ji需要si单位的安装时间,截止时间为di.超时会有惩罚值,若实际完成时间为ci,则惩罚值为max{0,ci-di}.从0时刻开始执行任务,问惩罚值最大的两个服务的惩罚值之和的最小是多少? 分析 乍一看似乎要二分,但实际上并不是.贪心来做,按di从小到大安排任务,当di相等时,让完成时间短的排前,这样安排任务一定时更优的,但是并不能满足题目的要求.于是需要从两个最大惩罚值的前面挑选一个任务,并将它放置在后面,然后更新答案即可. #include<c…
耳熟能详的故事,田忌赛马,第一行给出田忌的马的速度,第二行是齐王的马的速度,田忌赢一场得200,输一场失去200,平局不得也不失,问最后田忌最多能得多少钱? 都知道在故事里,田忌用下等马对上等马,中等马对下等马,上等马对中等马,一负两胜,这就是贪心的策略.在这题中,先将速度排序,首先用田忌速度最快的马去匹配,如果大于齐王速度最快的马,就刚它,如果小于它,那么就说明这一场肯定是要输了,既然是要输,那么用速度最慢的马去输,这样可以将速度最快的马保留下来,如果和齐王的速度最快的马速度相同,那么暂时放在…
E - Maximum Square Time Limit:4500MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4867 Description Given an NxM matrix of all 1s and 0s, find the largest submatrix which is a square containing all 1s. Input Ther…
There will be several test cases in the input. Each test case will begin with a line with three integers: N A B Where N is the number of teams (1N1, 000), and A and B are the number of balloons in rooms A and B, respectively (0A, B10, 000). On each o…
ZSoft Corp. is a software company in GaoKe Hall. And the workers in the hall are very hard-working. But the elevator in that hall always drives them crazy. Why? Because there is only one elevator in GaoKe Hall, while there are hundreds of companies i…
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4924 Prime Switch Time limit: 1.000 seconds 问题描述 There are lamps (uniquely numbered from 1 to N) and K switches. Each switch has one p…