codeforces 484A A. Bits(贪心)】的更多相关文章

题目链接: A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x. You are given mu…
[链接] 我是链接,点我呀:) [题意] 让你求出l~r当中二进制表示1的个数最多的数x [题解] 最多有64位 我们可以从l开始一直增大到r 怎么增大? 找到l的二进制表示当中0所在的位置 假设i这一位的0经过加法变成了1 那么我们再从低位到高位依次枚举那一位为1就好 然后把这个二进制转换成十进制 看看它是不是比ri来的小 如果是,则尝试更新最小值 a[i]=(int)一个long类型的数字%2 a[i]是Int 会出错.. 另解 从低位到高位依次让每个0bit都变成1bit 看看新变成的数字…
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x)+a.lr+b.rl,遍历完所有的点,问你最后的花费是多少 思路:每次选一个点的时候,在当前确定的每个点比较一下,选最短的距离. 为什么可以贪心?应为答案唯一,那么路径必定是唯一的,每个点所在的位置也一定是最短的. #include <bits/stdc++.h> using namespace…
CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <sstream> #include <set> #include <map…
CodeForces484A Bits(贪心) CodeForces484A 题目大意:给出范围[A.B].期望你给出某个数X满足X属于[A,B],而且X转成二进制的1的个数最多.假设有多个给出最小的数. 解题思路:由于须要1最多,那么我们先将每一个位都放上1,假设这个数减掉了某一位的1还是超出了范围,那么就能够去掉这个1.假设去掉后发现比A小了,那么这个位置上的1是不能去掉的.直到找到一个数在范围内即为所求的.每次去掉的是高位的1那么在保证1的个数同样的情况下求得的自然是最小的数. 代码: #…
C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x. You are given multiple que…
A. Bits   Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x. You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is m…
A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x. You are given multiple…
这题可以根据l, r 在二进制下的长度进行分类. l  的长度小于 r 的时候,有两种可能,一种是r 在二进制下是 1* 这种样子,故答案取 r : 一种是取答案为  (1LL << (rcnt - 1)) - 1 ,意思为比 r 小一位长度,也是 1* 这种样子的数. l 的长度等于 r 的时候,答案从 l 开始找 , 按位 与 1,同时要满足答案不大于 r  即可. source code (有参考): //#pragma comment(linker, "/STACK:1677…
意甲冠军: 10000询价  每次查询输入L和R(10^18)  在区间的二进制输出指示1大多数数字  1个数同样输出最小的 思路: YY一下  认为后几位全是1的时候能保证1的个数多  那么怎样构造出这个数字呢?? 将L和R都变成二进制  从高位到低位  L和R同样的那几位一定是不变的  由于要保证构造出的数字在区间内  然后分两种情况 一是L和R一直同样  那就没什么好说的了  就是它了 二是发现了有一位不同  这时R的那个位一定是1  L的一定是0  那么仅仅要把R的那个1变成0  然后把…
题意:给出区间[ll,rr],求中间一个数二进制表示时一的个数最多. 写出ll和rr的二进制,设出现第一个不同的位置为pos(从高位到低位),找的数为x,那么为了使x在[ll,rr]内,前pos-1个位必须也相同.而rr在pos和pos后如果都为1,那么pos和pos后都取1,否则pos取0,pos后取1. 乱码: //#pragma comment(linker,"/STACK:1024000000,1024000000") #include<iostream> #inc…
题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车中有凳子,那么这辆购物车中最便宜的那个物品的价格能减少50%,问你如何放这些物品才能使总价钱最少. 思路: 简单贪心,判断凳子数量是否大于等于k行. #include <bits/stdc++.h> using namespace std; typedef long long LL; typede…
Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at al…
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopping. She will walk through a mall along a straight street, where N individual shops (numbered from 1 to N) are aligned at regular intervals. Each shop…
题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可充电电池能够充一格电,就用普通电池跑(让可充电池充电),否则就用可充电电池走. AC代码: #include<bits/stdc++.h> using namespace std; ; int n,a,b; bool s[maxn]; int main() { cin>>n>&g…
题目链接: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can…
题目链接: A. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly …
题意: 给n个奖池,t张彩票,q次操作. 每个奖池的奖金为pi. 每个奖池现有的彩票的数量为ai,保证ai>=1: q次操作,每次有两种,第i个奖池的现有彩票数量加一,或减一. 不允许投票的数量多于奖池数量的二分之一. 保证: n,t,q<=2e5 ai<=1000 pi<=1000 求在采用最佳策略的前提下获得奖金的期望. 思路: 首先要证明贪心的正确性,即把某张票投入某奖池之后其下一张票给期望做出的贡献要小于上一张彩票... 把式子写一下,求导,发现导数是单调递减的... 然后…
题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要是对称位置不同样的.那么指针肯定要先移动到这里,改动字符仅仅须要考虑两种方向哪种更优即 可. 然后将全部须要到达的位置跳出来.贪心处理. #include <cstdio> #include <cstring> #include <cstdlib> #include <…
codeforces 893D Credit Card 题目大意: 有一张信用卡可以使用,每天白天都可以去给卡充钱.到了晚上,进入银行对卡的操作时间,操作有三种: 1.\(a_i>0\) 银行会给卡充入\(a_i\)元 2.\(a_i<0\) 银行从卡中扣除\(a_i\)元 3.\(a_i=0\) 银行对你的卡进行评估,违背了规则就无权再使用此卡 规则1:卡内的余额不得超过\(d\)元 规则2:当\(a_i=0\)时,卡内的余额不能是负数 现在问为了维持信用的平衡,最少去银行几次.(去一次,充…
题目链接 题意 给定序列,$q(1\leq q \leq 100000) $次询问,每次查询给定区间内的最大异或子集. 思路 涉及到最大异或子集肯定从线性基角度入手.将询问按右端点排序后离线处理询问,对线性基的每一位贪心的保留靠后的. 代码 #include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " << x << endl; using namespace std;…
Tennis Championship 题目链接:http://codeforces.com/problemset/problem/735/C ——每天在线,欢迎留言谈论. 题目大意: 给你一个 n (2≤n≤10^18),代表一共有n位参加比赛的选手. 游戏规则: ①每次比赛,输的选手将离开赛场 ②相互比赛的选手 他们的获胜的次数相差不能超过1(获胜4次的选手只能跟3或5次的选手比赛) 问题:最终赢得比赛的选手,胜场最多能为多少. 思路: 贪心:①选一名选手让他一直获胜且优先让他参加比赛 ②当…
题目:CodeForces - 363D 题意:给定n个学生,其中每个学生都有各自的私己钱,并且自己的私己钱只能用在自己买自行车,不能给别人. 给定m个自行车,每个自行车都有一个价格. 给定公有财产a.    然后求出这些学生能买自行车的最大数量,并且求当买下最大自行车数量时,总体花费私己钱的最少的钱. 我先来说以下二分搜索模板: //右值点不能取到的情况 int binary_search(vector<int>& nums,int left,int right, int targe…
Planning time limit per test 1 second memory limit per test 512 megabytes input standard input output standard output Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n flights that must depart today,…
题目传送门 这是一条通往vjudge的高速公路 这是一条通往Codeforces的高速公路 题目大意 给定一个$n$阶完全图,每个点有一个权值$a_{i}$,边$(i, j)$的权值是$(a_{i}\  xor\  a_{j})$.一个生成树的权值是各边的权值和.问最小生成树的权值. 设值域为$[0, 2^{k})$. 当$k = 1$的时候,显然将点权为0的点之间互相连边,点权为1的点之间互相连边,中间随便连一条. 当$k = x\ (x > 1)$的时候,将这些点按照二进制的第$k$位分成两…
Treeland is a country in which there are n towns connected by n - 1 two-way road such that it's possible to get from any town to any other town. In Treeland there are 2k universities which are located in different towns. Recently, the president signe…
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbi…
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about…
题目链接:http://codeforces.com/problemset/problem/777/E 题意:有n个环给你内环半径.外环半径和高度,叠这些环还要满足以下要求: ①:下面的环的外径要>=上面的环 ②:环不能掉下去,所以下面的环的内径要<上面的环的外径 ③:叠出最大高度 思路:贪心思想,先把这些环按外径从大到小,内径从大到小排好序,内径从大到小是为了尽可能让后面的环能放上去.接下来就可以用栈模拟了,放1~n个环的时每次都要判断,如果当前最顶上的环的内径大于我要放的环的内径也就是放不…
贪心:让代价大的尽量移到靠前的位置. 做法:先让前k个数加进堆里,枚举k+1~n+k,每次把新元素加进堆后找到最大代价放在当前位置即可. #include<bits/stdc++.h> #define ll long long using namespace std; ; struct poi{int c,pos;}; priority_queue<poi>q; bool operator<(poi a,poi b){return a.c<b.c;} int n,k;…