uva live 7637 Balanced String (贪心)】的更多相关文章

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5659 题意: 你有一个只包含"(" 和 ")" 的串,每一个位置有个数值,这个数值是当前的左括号-右括号的值. 例:()() 数值就是1010. 给你一个打乱了的数值,要你构造出字典序最小的字符串. 题解: 因为左括号比右括号…
题意:给定一个括号的序列,原先的序列是碰到左括号加1,碰到右括号减1,然后把序列打乱,让你找出字典序最小的一个答案. 析:直接从第一个括号判断就好了,优先判断左括号,如果不行就加右括号. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cma…
https://cn.vjudge.net/problem/UVA-1368 二维的hamming距离算法: For binary strings a and b the Hamming distance is equal to the number of ones (population count) in a XOR b. int hamming_distance(unsigned x, unsigned y) { ; unsigned val = x ^ y; // Count the n…
题意:给你一个长度为\(3*n\)的字符串,要求修改最少的次数,使得字符串中\(0,1,2\)的个数相同,并且在最少次数的情况下使字典序最小. 题解:贪心,\(0\)一定放在前面,\(1\)和\(2\)放后面,首先统计\(0,1,2\)的个数,因为题目要求字典序最小,所以我们先从左边开始遍历,如果\(2\)的个数大于\(n/3\),那么再看\(0\)和\(1\)的个数情况将其替换成\(0\)或\(1\),对\(1\)也是如此,然后我们再反着遍历,首先考虑\(1\)的情况,再考虑\(0\)的情况.…
原题链接 题意:给出一个打乱顺序的序列,问是否能构造出一个括号匹配的字符串.每个数字为此前读取到的左括号数减去右括号数. 分析:有左括号开始构造,不够的话就找右括号.注意特殊情况待处理.详情看代码 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<s…
Problem A - No Tipping As Archimedes famously observed, if you put an object on a lever arm, it will exert a twisting force around the lever's fulcrum. This twisting is called torque and is equal to the object's weight multiplied by its distance from…
B. Making a String 题目连接: http://codeforces.com/contest/624/problem/B Description You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied: the i-th…
  Product of digits  For a given non-negative integer number N , find the minimal natural Q such that the product of all digits of Q is equal N . Input The first line of input contains one positive integer number, which is the number of data sets. Ea…
贪心策略:一个直径为X的头颅,应该让雇佣费用满足大于等于X且最小的骑士来砍掉,这样才能使得花费最少. AC代码 #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <utility> #include <string> #include <iostream> #include <map> #inc…
题目传送门 题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度. 思路: 首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b,考虑两个字符串,这两个如果min(a1,b2)小于min(a2,b1)时,第一个字符串是不是应该排在前面呢?如果两个相同的话,当然应该吧右括号比较多的放在前面,左括号比较多的放在后面. #include<iostream> #include<cstdio> #include<cm…
题意:n 个人成一个圈,每个人想要 ri 种不同的礼物,要求相邻两个人没有相同的,求最少需要多少礼物. 析:如果 n 是偶数,那么答案一定是相邻两个人的礼物总种数之和的最大值,那么如果是奇数,就没那么好做了,我们可以二分答案, 在每次判定时,我们可以有这样的贪心策略,第一个人 1 - r1,在后面的人中,编号为奇数的尽量往后取,编号为偶数的尽量往前取, 因为这样我们才能保证第 n 个人和第一个人尽量不相交. 代码如下: #pragma comment(linker, "/STACK:102400…
原题链接在这里:https://leetcode.com/problems/replace-the-substring-for-balanced-string/ 题目: You are given a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string is said to be balanced if each of its characters appears n/4 times where…
题目如下: You are given a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string is said to be balanced if each of its characters appears n/4 times where n is the length of the string. Return the minimum length of the substring that…
Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvescondescended, to write a C/C++ program just to add a set of numbers. Such a problem will simplyquestion your erudition. So, lets add some avor of ingenuity…
We would like to place n rooks, 1 n 5000, on a n nboard subject to the following restrictions• The i-th rook can only be placed within the rectan-gle given by its left-upper corner (xli; yli) and its right-lower corner (xri; yri), where 1 i n, 1 xli…
Rikka with string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 581    Accepted Submission(s): 227 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation,…
这道题真是WA得我心力交瘁,好讨厌的感觉啊! 简直木有写题解的心情了 题意: n×n的棋盘里,放置n个车,使得任意两车不同行且不同列,且第i个车必须放在给定的第i个矩形范围内.输出一种方案,即每个车的坐标,无解的话则输出“IMPOSSIBLE” 行和列是独立的,所以可以分开处理,将二维的转化成了一维区间上的取点问题: 有一个长度为n的区间,还有n个小区间,求一种方案,在每个小区间的范围取一个点,是的大区间上每个单位1的区间里都有点. 开始写的贪心是错误的: 按区间的左端点从小到大排序,然后右端点…
题目链接: C - Grid of Lamps Time Limit:1000MSMemory Limit: 0KB 问题描述 We have a grid of lamps. Some of the lamps are on, while others are off. The luminosity of a row/column is the number of its lighted lamps. You are given a permutation of the luminositie…
Commando War There is a war and it doesn't look very promising for your country. Now it's time to act. You have a commando squad at your disposal and planning an ambush on an important enemy camp located nearby. You have N soldiers in your squad. In…
题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就像字典序输出从小到大一样的意思. 这里用到贪心的方法,定义f(x)为真的条件是满足x为最大值使n本书分成k份,那么就是求x的最小值.怎样确定这个x就是用的二分法,x一定大于0小于全部值的合,不断的二分再推断是否成立,成立就取左半边,不成立说明太小了就取右半边,写的时候还是没有把二分法理解透彻,我还怕…
Children's Game Problem Description There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given N positive integer. (S)He can make…
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance…
思路:复制次数最少并且可以部分复制,那么贪心地让当前尽量多的复制,如果最后一次复制会超过n,那就部分复制.即满足并且x尽量小. AC代码 #include <stdio.h> const int maxn = 20; int bit[maxn]; void init() { bit[0] = 1; for(int i = 1; i < maxn; i++) { bit[i] = bit[i-1] * 2; } } int main() { init(); int n, kase = 1;…
题目链接  题意  在n*n的棋盘上的n个指定区间上各放1个'车’ , 使他们相互不攻击(不在同行或同列),输出一种可能的方法. 分析 每行每列都必须放车,把行列分开看,若行和列同时有解,则问题有解.这样就变成了n个区间选n个点的贪心问题.对每个点x选择包含它的最优未使用的区间,这个最优区间就是右界最小的区间.因为在给k找最优区间时,1~k-1的最优区间都已经找好了. #include <bits/stdc++.h> using namespace std; ; int xl[N], yl[N…
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4125" rel="nofollow">1379 - Pitcher Rotation 题意:n个人,m个敌人.去比赛,有得分.n个人能够反复比.可是每次比完要歇息4天,问最大得分 思路:dp[i][j][k][l][x] 表示第场比赛,前一天为j,两天为k,三天为l.四天为x,的最大得分,然后因为仅仅有每一个人5天就能…
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1603 题意 问使m个n长碱基序列汉明码最小的序列 思路 明显,取最频繁的 代码 #include <algorithm> #include <cassert> #include <cmath> #include <cstdio&…
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1178 题意 圆桌上有n个人,每个人要求a_i种不同的礼物,相邻两个人的礼物不能重复,问有至少要准备多少种礼物 思路 如刘书 1. 明显,若n=1,直接输出a[0] 2. 若n为偶数,则可以形如ABABAB,直接取最大的两个相连之和 3. 若n为奇数,则可以转化为二分判断…
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2515 题意 n*n矩阵中填入大写字母,n <= 10,要求从上到下从左到右字母序最小,相邻格子字母不同 思路 如刘书思路,状态比较小,不会导致矛盾. 感想 1. 状态较小 代码 #include <algorithm> #include <cassert>…
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范围,计算并输出使得每辆车横竖都不能相互攻击 的摆放方法,能则输出每辆车的坐标,不能则输出"IMPOSSIBLE". 解题思路 想想怎么将问题分解成几个小问题,不同行不同列的话,将其分成两个一维问题,采用DFS向下搜索,搜的时候注意每个车的 行区间和列区间,找到一种则直接返回,输出对应每辆车…
题意:给出两个等长的字符串,0可以变成1,?可以变成0和1,可以任意交换s中任意两个字符的位置,问从s变成t至少需要多少次操作. 析:先说我的思路,我看到这应该是贪心,首先,如果先判断s能不能变成t,就计算t中的1和s中的1. 然后算出t比s多多少1,然后先考虑把?变成1是最优的,如果不够就只能把0变成1,切不可把?变成1,因为这样要两步, 不是最优,而把0变成1是一步.然后把剩下的?变成1(如果1还不够)或者是0(1够了).最后是计算要交换的次数, 这个只要统计不同的位置有多少即可,别忘了1可…