uoj problem 10

题目大意:

给定任务若干,每个任务在\(t_i\)收到,需要\(s_i\)秒去完成,优先级为\(p_i\)

你采用如下策略:

每一秒开始时,先收到所有在该秒出现的任务,然后取出当前优先级最高的任务,一直工作这个任务到下一秒,该任务的需要的时间-1s,如此循环进行,直到任务全部完成.

现在有一任务的优先级未知,但知道其被完成的时间点.确定一个合法的优先级.并计算出所有任务完成的时间

题解:

其实vfk的题解很详细.

那我就写一写我的理解吧.


首先拿到这道题我们就会去想枚举优先级然后判定.

分析一下我们发现优先级是具有单调性的,所以我们可以二分.

这样直接就能拿到90%的分数.

然后题解上说:

第一问的二分是建立在单调性上的,而有单调性的题,往往可以试着利用扫描的方法去优化。

WTF...我好是第一次用扫描优化有单调性的题...

我们扫描一边所有的题目,先将未知的优先级按照\(-inf\)算,计算出所有的任务的结束时间

设未知优先级的任务编号为x,结束时间为T

那么我们计算出所有在\([t_x,T]\)内的工作量,然后我们将所有的任务按照优先级排序.

现在我们就确定一个优先级是的所有优先级\(<\)它的任务的工作量之和等于\(s_x\)

不难发现这样是对的.

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch=='-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 300010;
struct Node{
ll t;
ll s,p,id,idx;
bool friend operator < (const Node &a,const Node &b){
return a.p < b.p;
}
}a[maxn],b[maxn];
inline bool cmp(const Node &a,const Node &b){
return a.t < b.t;
}
ll n,T,Xl,Xr,ans[maxn];
inline void add(ll l,ll r,int id){
ll x = min(r,Xr) - max(l,Xl) + 1;
if(x <= 0) return ;
b[id].s += x;
}
inline void work(int n){
priority_queue<Node>q;
ll la = 0;
for(int i=1;i<=n;++i){
ll ti = a[i].t - 1;
if(ti > la){
ll x = ti - la,y = 0;
while(!q.empty() && q.top().s <= x){
Node no = q.top();q.pop();
x -= no.s;y += no.s;
ans[no.id] = la + y;
add(la+y-no.s+1,la+y,no.idx);
}
if(!q.empty() && x){
Node no = q.top();q.pop();
add(la+y+1,la+y+x,no.idx);
no.s -= x;q.push(no);
}
}
while(a[i].t == a[i+1].t){
q.push(a[i]);
++ i;
}q.push(a[i]);la = a[i].t - 1;
}
}
int main(){
read(n);int tx;
for(int i=1;i<=n;++i){
read(a[i].t);
read(a[i].s);
read(a[i].p);
a[i].id = i;
}
a[n+1].t = 1LL<<60;
a[n+1].s = 1e9;
a[n+1].p = -1e9;
a[n+1].id = n+1;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;++i){
a[i].idx = i;
b[i] = a[i];b[i].s = 0;
if(a[i].p == -1){
b[i].p = a[i].p = 0;
tx = i;
}
}
read(T);--T;
Xl = a[tx].t;Xr = T;work(n+1);
sort(b+1,b+n+1);
ll s = a[tx].s,ans1 = 0;
for(int i=1;i<=n;++i){
s -= b[i].s;
if(s == 0 && b[i].p + 1 != b[i+1].p){
ans1 = b[i].p + 1;
break;
}
}
printf("%lld\n",ans1);
a[tx].p = ans1;
work(n+1);
for(int i=1;i<=n;++i){
printf("%lld",ans[i]+1);
if(i != n) putchar(' ');
else putchar('\n');
}
return 0;
}

uoj problem 10的更多相关文章

  1. Project Euler Problem 10

    Summation of primes Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of ...

  2. Problem 10

    Problem 10 # Problem_10.py """ The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. ...

  3. leetcode problem 10 Regular Expression Matching(动态规划)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  4. (Problem 10)Summation of primes

    The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two milli ...

  5. uoj problem 31 猪猪侠再战括号序列

    题目大意: 给定一个长度为2n的括号序列.定义一个关于区间[l,r]的翻转操作为位置平移对调. 即翻转")))()("可以得到"()()))((" 用不超过n次 ...

  6. uoj problem 14 DZY Loves Graph

    题目: DZY开始有 \(n\) 个点,现在他对这 \(n\) 个点进行了 \(m\) 次操作,对于第 \(i\) 个操作(从 \(1\) 开始编号)有可能的三种情况: Add a b: 表示在 \( ...

  7. uoj problem 21 缩进优化

    题目: 小O是一个热爱短代码的选手.在缩代码方面,他是一位身经百战的老手.世界各地的OJ上,很多题的最短解答排行榜都有他的身影.这令他感到十分愉悦. 最近,他突然发现,很多时候自己的程序明明看起来比别 ...

  8. uoj problem 11 ydc的大树

    题目大意: 给定一颗黑白树.允许删除一个白点.最大化删点后无法与删点前距自己最远的黑点连通的黑点个数.并求出方案数. 题解: 这道题很棒棒啊. 一开始想了一个做法,要用LCT去搞,特别麻烦而且还是\( ...

  9. Problem 10: Summation of primes

    def primeslist(max): ''' 求max值以内的质数序列 ''' a = [True]*(max+1) a[0],a[1]=False,False for index in rang ...

随机推荐

  1. 记录-JQuery日历插件My97DatePicker日期范围限制

    对于日期控件,有时会有不能选择今天以前的日期这种需求..... My97DatePicker是一个非常优秀的日历插件,不仅支持多种调用模式,还支持日期范围限制. 常规的调用比较简单,如下所示: 1 & ...

  2. Java NIO Buffer(netty源码死磕1.2)

    [基础篇]netty源码死磕1.2:  NIO Buffer 1. Java NIO Buffer Buffer是一个抽象类,位于java.nio包中,主要用作缓冲区.Buffer缓冲区本质上是一块可 ...

  3. greenlet和gevent模块的区别?

    协程是一中多任务实现方式,它不需要多个进程或线程就可以实现多任务. yield能实现协程,不过实现过程不易于理解,greenlet是在这方面做了改进,通过switch. greenlet可以实现协程, ...

  4. linux c编程:管道

    2在前面介绍过,进程之间交换信息的唯一途径就是传送打开的文件.可以经由fork或者exec来传送.这一章将介绍新的进程共享方式 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都 ...

  5. 跟我一起用Symfony写一个博客网站;

    我的微信公众号感兴趣的话可以扫一下, 或者加微信号   whenDreams 第一部分:基础设置,跑起一个页面-首页 第一步: composer create-project symfony/fram ...

  6. HackerRank - beautiful-binary-string 【字符串】

    题意 给出一个 N 位的 01 串 然后 每次 改动 可以将其中的 (0 -> 1) 或者 (1 -> 0) 然后 求 最少几次 改动 使得 这个 01 串 当中 不存在 连续的 010 ...

  7. iOS 发大招 otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法 && 编写通用类的时候关于可变参数的处理

    开始 我  以为 这个 alertView 里面 ...的写法  应该 是一个 普通的数组  然 并没有 分享一篇好文 http://www.tekuba.net/program/290/ IOS实现 ...

  8. Caffe学习系列(三)Docker安装及一些问题的记录

    前言: Docker安装倒是很简单,按照步骤轻松完成,但是在联网方面还是出现问题,大概是伟大的祖国防火墙将其拦下,但在开发中要遇山开山,见水搭桥.在其中我将解决方法记录下来,每次解决了困难想分享找不到 ...

  9. 第三篇、dom操作续

    一.属性操作 属性操作 attributes // 获取所有标签属性 setAttribute(key,value) // 设置标签属性 getAttribute(key) // 获取指定标签属性 r ...

  10. Spring Cloud之网关搭建

    统一由网关进行拦截判断 要不放到每个服务里面就很不合适了 冗余 主要的: <dependency> <groupId>org.springframework.cloud< ...