Codeforce 545D. Queue

Little girl Susie went shopping with her mom and she wondered how to improve service quality.

There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed.

Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.

Input

The first line contains integer n (1?≤?n?≤?105).

The next line contains n integers ti (1?≤?ti?≤?109), separated by spaces.

Output

Print a single number — the maximum number of not disappointed people in the queue.

从小到大排序后贪心,如果到当前人是不满意的话就跳过(也就是相当于把他换到后面),否则就去服务他,sum+=ti

#include <cstdio>
#include <cctype>
#include <stdlib.h>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;

int n;
LL sum = 0;
LL t[100008];

int main() {
  // freopen("test.in","r",stdin);
  cin >> n;
  for (int i=0;i<n;i++){
    cin >> t[i];
  }
  sort(t,t+n);
  int total = 0;
  for (int i=0;i<n;i++){
    // cout << sum << " " << t[i] << endl;
    if (sum > t[i]){
      continue;
    }
    else {
      total ++;
    }
    sum += t[i];
  }
  cout << total;
  return 0;
}

时间: 2024-12-19 04:09:08

Codeforce 545D. Queue的相关文章

Codeforces 545D - Queue

545D - Queue 思路:忍耐时间短的排在前面,从小到大排序,贪心模拟,记录当前等待时间,如过等待时间大于当前的这个人得忍耐时间,那么就把这个人扔到最后面,不要管他了(哼╭(╯^╰)╮,谁叫你那么没耐心呢),所以也就不用记录为他服务的时间. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset((a),(b),si

CodeForces 545D Queue (排序模拟)

[题目链接]:click here~~ [题目大意]: 有n个人,每个人都有一个等待时间,如果对于当前的人来说总等待时间超过自己的等待时间,那么这个人就会失望,问换一下顺序,使失望的人最少,问最多有多少个人不失望. [思路]:排一下序然后加然后与当前的比较.如此.. 代码: /* * Problem: CodeForces 545D * Running time: 46MS * Complier: G++ * Author: herongwei * Create Time: 8:20 2015/

【CodeForces】545D Queue

传送门 题目描述 Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more th

545D. Queue

http://codeforces.com/problemset/problem/545/D 题意:n个数的服务请求数组,求在其服务时间内,最大的可满足服务的请求数量 首先对服务请求数组按照从小到大排序. 下面判断其服务时间t 与 前面服务时间之和sum的关系 若 t< sum :  等待服务数+1 若t>sum : 说明在服务待时间t内能够被服务.已经服务的时间sum+改请求的时间t 就是新的已经服务的时间和 注意: sum是满足条件的服务时间和 Java程序: import java.ut

codeforce 605BE. Freelancer&#39;s Dreams

题意:给你n个工程,做了每个工程相应增长x经验和y钱.问你最少需要多少天到达制定目标.时间可以是浮点数. 思路:杜教思路,用对偶原理很简易.个人建议还是标准解题法,凸包+线性组合. 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cstdio> 6 #include<set> 7 #include&

Codeforce 505D - Mr. Kitayuta&#39;s Technology 弱联通分量+拓扑排序

对于有向图M,若将其所有的边转化为无向边,则得到其基图M',若M'是联通的,则称有向图M是弱联通. 对于有向图M,若图中任意两点u,v(u != v)均满足u到v可达,v到u可达,则称此图为强联通. 根据以上定义显然可知,强联通图一定也满足弱联通. 此题首先我们需要找到其所有的弱联通分量. 对于每一个弱联通分量,设此弱联通分量内点的个数为ans,如果此联通分量无环,则需要的边数为ans-1,若有环则为ans. 太挫了,这种题都不会了,怎么变黄!!! #include <algorithm> #

CodeForce 117C Cycle DFS

A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes u and v (u ≠ v) exists either an edge going from u to v, or an edge from v to u. You are give

UVALive-7304 - Queue of Soldiers 【动态规划】【组合函数】【好题】

UVALive- 7304 - Queue of Soldiers 题目链接:7304 题目大意:士兵过山洞,必须以类似7 6 5 4 3 2 1顺序过.在第i个人之后,比i高的人都会被杀死,问如果要杀死k个人,有几种排队方法. 题目思路:先将士兵的身高离散化.假设N表示不同身高的数目.cnt[i] 表示i这个身高的人有多少个.(i的范围为1~N)sum[i]表示小于等于该身高段的士兵数目 然后开始dp,dp[i][j]表示已经到第i个士兵,已经死了j个人的方法数. 第三维遍历,q表示,第i+1

Java集合类: Set、List、Map、Queue使用

目录 1. Java集合类基本概念 2. Java集合类架构层次关系 3. Java集合类的应用场景代码 1. Java集合类基本概念 在编程中,常常需要集中存放多个数据.从传统意义上讲,数组是我们的一个很好的选择,前提是我们事先已经明确知道我们将要保存的对象的数量.一旦在数组初始化时指定了这个数组长度,这个数组长度就是不可变的,如果我们需要保存一个可以动态增长的数据(在编译时无法确定具体的数量),java的集合类就是一个很好的设计方案了. 集合类主要负责保存.盛装其他数据,因此集合类也被称为容