CodeForces - 158B.Taxi (贪心)】的更多相关文章

CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一种是3多,那么结果就加上3的个数,再单独处理2. 对于1和2组队处理的讨论:首先分配2,有2种情况,一种是2正好分配完了,另外一种就2还剩下2个人(正好剩下1组).就一起处理这个2个人和1剩下的人. 把每次处理的结果都加起来即可. 代码 #include <iostream> #include &…
贪心,注意优先级,4单独,3与1先匹配,2与2匹配(注意判断2有没有剩下),然后2与两个1匹配,最后4个1匹配就可以了. #include<iostream> #include<cstdio> #include<cstring> using namespace std; int main() { int n; ],num; while(~scanf("%d",&n)) { memset(tot,,sizeof(tot)); ;i < n…
B. Taxi time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that th…
http://codeforces.com/problemset/problem/158/B B. Taxi time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output After the lessons n groups of schoolchildren went outside and decided to visit Polyca…
[链接] 我是链接,点我呀:) [题意] 每辆车可以载重4个人. 一共有n个组,每个组分别有s[i]个人. 要求每个组的人都在同一辆车里面. 问最少需要多少辆车 [题解] 将每个组的人数从小到大排序. 然后逆序枚举每个组r. 显然这个组肯定要占用一辆车. 那么现在问题就变成尽可能多带几个人在这一辆车里面. 那么就找人数最小的几个组就好(这样一辆车里面的人能多一点.) [代码] import java.util.Arrays; import java.util.Scanner; public cl…
题目链接:http://codeforces.com/contest/724/problem/D 题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有s的位置(不是字符),求所有s在sort后字典序最小的那个字符串. 思路:对字符排序后,从最后一个开始贪心,判断删除该字符后是否符和题意,当删除后不符合题意时,贪心到该相同字符对应的第一个位置为止. 比如对于test3来说 排序后为a a a b b b b c c c c 删除到(b,6)时发现不…
G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input output:standard output Johnny is at a carnival which has n raffles. Raffle i has a prize with value pi. Each participant can put tickets in whichever ra…
K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪心 如果子树中的点是偶数 就可以直接切了 最后答案要减一 因为原来的树也是偶数个节点 会被统计进去 #include <cstdio> #include <vector> #include <algorithm> using namespace std; ; int res…
传送门:CodeForces - 940E - Cashback 题意:在一个长度为n的数组中,可以分出长度为 k 连续的多个数组b(每个数组 b 的 k 可不相同),然后,可以对每个数组 b 进行删去 k / c 个数的操作: 输出最小的全部数组b的和: 思路:首先要贪心的想到,这个 k 要么等于 c ,要么等于 1 ,才能使总和最小: 所以列出递推方程:hh [ i -1 ] = min(hh[i - 1] + a[ i ]   , hh[ i - c]+sum[ i ] - sum[ i…
题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’s define f(x)f(x)for positive integer x as a product of factorials of its digits. For example, f(135)=1!∗3!∗5!f(135)=1!∗3!∗5! First, they choose a dec…
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…
题目链接:http://codeforces.com/contest/732/problem/E 题意:有n台计算机,m个插座,每台计算机有一个值a[i],每个插座有一个值b[i],每个插座最多只能对应一台计算机,且只有a[i] == b[j]时才能配对.现有无限台适配器,适配器能使b[i]减半,求最多能使多少计算机与插座配对(c),以及对应的最小的适配器个数(u)(即先考虑c最大,再使u最小). 思路:先对计算机和插座按值排序,不断减半,发现能配对就配对. #include<bits/stdc…
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. 思路: 转化为判定性问题.二分天数. 然后贪心,在规定天数以内,最后一天通过即可.需要保证每一科最后一天之前剩下还没考试的科目都来得及复习.(同时注意要要加上考试时间) 最后还要判定是不是所有的科目都通过了. */ #include<bits/stdc++.h> using namespace…
题目链接:CodeForces #100  C 题意:现在给出n个snowball的半径,3个半径严格递增或递减的snowball,可以组成1个snowmen.问最多能组成多少个snowmen.并且按照半径递减的顺序输出每个snowmen的组成. 思路:嗯...每次都从前三个个数最多的snowball里选择,最后组成的snowmen最多... ...可以用优先队列写..但是感觉set+map写的太优雅了...map当然不等于数组了...哼. #include <stdio.h> #includ…
A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at…
Problem Population Size 题目大意 给一个长度为n的序列,由 -1 和正整数组成,-1表示任意的正整数. 将序列分成若干段,使得任意段都是等差数列,求最少段数. 解题分析 可以发现对于某一段序列,越长越好.贪心加点,保证每段都是最长就可以了. Tips:一段相同的数也可以算是等差数列. 参考程序 #include <map> #include <set> #include <stack> #include <queue> #includ…
题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的衣服或者烘干的衣服较多来不及进行下一个步骤,则从一开始就顺延洗衣服的时间,贪心的思想也是体现在这里. 3.关键在于烘干衣服的顺延如何处理,因为需要调整洗衣服的起始时间,其实我们只要对烘干衣服的时间进行顺延处理就可以了,因为即使没有调整洗衣服的起始时间,那么下次到了烘干衣服的时间的时候因为烘干衣服的数…
转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud    Banana Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a…
Elections in Berland are coming. There are only two candidates — Alice and Bob. The main Berland TV channel plans to show political debates. There are nn people who want to take part in the debate as a spectator. Each person is described by their inf…
There are nn players sitting at the card table. Each player has a favorite number. The favorite number of the jj-th player is fjfj. There are k⋅nk⋅n cards on the table. Each card contains a single integer: the ii-th card contains number cici. Also, y…
有货车运量V:有若干物品A 占2单位体积,有若干物品B占1单位体积:相同种类的物品价值不一定一样: 序号按照输入顺序而定:问货车可以拉走最多多少价值的物品,并输出所选物品的序号: 1)常规的解法思路:贪心,先把A填到不能填,然后再补B,补完B后再考虑用B去替换前面A,此策略必定最优:考虑到B物品的可补行!因为涉及变量较多,时刻注意A,B,空余V的数量关系:代码如下: n,v = map(int,input().split())l1 = []l2 = []for i in range(n):   …
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1~n的不重复数字, 要求这k个数字为下标的对应a和b中的数的和乘以2的值  分别大于a和b 的数组总和. 思路:首先对a进行降序排序,然后输出最大值的下标,随后进行幅度为2的枚举,对排序后的a2~an进行选择性输出下标,(注意,排序的时候用一个新数组开两个变量,一个index,一个v进行排序,可以用…
C. Voltage Keepsake time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is…
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings. Your task is to replace minimum number of characters in this string with other characters to obtain…
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends. There are n schools numerated from 1 to n. One can travel between each pair of them, to d…
题目链接 题意有点坑: 给你一个N*N的矩阵,让你填入K个1,使之整个矩阵关于左上到右下的对角线对称,并且这个要求这个矩阵的字典序最大. 对矩阵的字典序的定义是从每一行的第一个元素开始比较,大着为字典序较大. 思路: 根据字典序的定义贪心的从第一个元素开始走,如果没被填1,就填1并且关于对角线的对称的位置也填1,共计消耗两个k. 如果是i==j,即对角线的位置只需要消耗一个k. 我的AC代码: #include <iostream> #include <cstdio> #inclu…
After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working…
大意: 给N个点,起点S终点T,每个点有X,A,B,C,D,根据I和J的X坐标可得I到J的距离计算公式 |xi - xj| + ci + bj seconds if j< i |xi - xj| + di + aj seconds otherwise (j > i). 求从起点到终点,经过N个点恰好一次的最短路 这题好巧妙啊, 以前都没见过这种做法, 看题解说是要维护一条从S->T的链, 每次贪心往链里添点就行了, 显然这个贪心是与顺序无关的, 所以一定最优. 复杂度是$O(n^2)$的…
大意: 第i天可以花$a_i$元买入或卖出一股或者什么也不干, 初始没钱, 求i天后最大收益 考虑贪心, 对于第$x$股, 如果$x$之前有比它便宜的, 就在之前的那一天买, 直接将$x$卖掉. 并不需要计算出最优的卖出时间, 因为若$x$后有更优价格, 可以再买回$x$, 不影响贪心结果 #include <iostream> #include <algorithm> #include <math.h> #include <cstdio> #include…
实在太冷了今天 taxi :错误原因1 忽略了 1 1 1 1 和 1 2 1 这种情况,直接认为最多两组一车了 2 语句顺序错 double cola: 忘了减去n的序号1,即n-- B. Taxi time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output After the lessons n groups of schoolchi…