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. 【BZOJ3112】[Zjoi2013]防守战线 单纯形法

    [BZOJ3112][Zjoi2013]防守战线 题解:依旧是转化成对偶问题,然后敲板子就行了~ 建完表后发现跟志愿者招募的表正好是相反的,感觉很神奇~ #include <cstdio> ...

  2. ibatis实现Iterate的使用 (转)

    <iterate         property="" /*可选,              从传入的参数集合中使用属性名去获取值,              这个必须是一 ...

  3. 关于中国省市的一份js代码

    下面是一份关于中国省市的js代码,搜藏起来,非常有用. var arrCity = [ { name:"请选择", sub:[{name:"请选择"}], ty ...

  4. 我的Android进阶之旅------>Android之动画之Frame Animation实例

    ============================首先看看官网上关于Frame animation的介绍================================ 地址:http://de ...

  5. html_dom类读取

    上传类文件以后,有三种方式调用这个类:从url中加载html文档从字符串中加载html文档从文件中加载html文档 复制代码 代码如下: <?php// 新建一个Dom实例$html = new ...

  6. Android Screen Orientation

    Ref:Android横竖屏切换小结 Ref:Android游戏开发之横竖屏的切换(二十七)

  7. struts2中的ModelDriven使用

    http://www.cnblogs.com/Topless/archive/2012/01/17/2324980.html 例子都为struts2中的文档例子 JSP提交数据:   <s:fo ...

  8. 小程序获取openid和unionid java实现

    官方api:https://developers.weixin.qq.com/miniprogram/dev/api/api-login.html#wxloginobject 参考文章:https:/ ...

  9. Yii2发送短信验证码完全解决方案

    概述 在做项目的时候,需要用到短信发送验证码功能.不能不说Yii2的牛逼,很容易就搞定了.下面我整理一下具体功能和流程,分享给大家. 主要功能 通过Yii2 rules验证手机号 通过js验证是否为手 ...

  10. Maven项目结构

    maven项目主体结构: 另外,Eclipse新建项目时会生成.project..classpath及.settings目录下的文件,这些文件用于描述一个Eclipse项目, 接下来做一个简要的解析: ...