Codeforces 523C&D C.Name Quest【贪心】、D.Statistics of Recompressing Videos【模拟】

来源:互联网 发布:java在线 编辑:程序博客网 时间:2024/04/30 03:06

C. Name Quest
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets happy. For example, ifsaba», then strings «baobab», «aabbaa», «helloabahello» make him very happy and strings «aab», «baaa» and «helloabhello» do not.

However rather than being happy once, he loves twice as much being happy twice! So, when he got stringt as a present, he wanted to cut it in two parts (the left part and the right part) so that each part made him happy.

Help s determine the number of distinct ways to cut the given stringt into two parts in the required manner.

Input

The first line contains string s, consisting of lowercase English letters. The length of strings is from 1 to 1000 letters.

The second line contains string t, that also consists of lowercase English letters. The length of stringt is from 1 to 106 letters.

Output

Print the sought number of ways to cut string t in two so that each part mades happy.

Examples
Input
ababaobababbah
Output
2
Input
marssunvenusearthmarsjupitersaturnuranusneptune
Output
0

C题目大意:

给你一个字符串a,再给你一个字符串b,让你割开字符串b变成两部分,使得每个部分都包含字符串a(按位相对包含即可);

问一共有多少种可行方案。


思路:


手写两组数据不难发现,我们贪心去找这些位子的话,不妨去找极端位子。

那么我们从前向后扫,找到b字符串中,第一个包含了字符串a的位子LL.

然后在从后向前扫,找到b字符串中,从后向前看第一个包含了字符串a的位子RR.

那么ans=RR-LL;


注意数组别开小了。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;char a[5000000];char b[5000000];int main(){    while(~scanf("%s",a))    {        scanf("%s",b);        int output=0;        int lena=strlen(a);        int lenb=strlen(b);        int now=0;        int ans1=-1;        int ans2=-1;        for(int i=0;i<lenb;i++)        {            if(b[i]==a[now])now++;            if(now==lena)            {                ans1=i;                break;            }        }        now=lena-1;        for(int i=lenb-1;i>=0;i--)        {            if(b[i]==a[now])now--;            if(now==-1)            {                ans2=i;                break;            }        }        if(ans1==-1||ans2==-1)printf("0\n");        else if(ans2-ans1<0)printf("0\n");        else        printf("%d\n",ans2-ans1);    }}

D. Statistics of Recompressing Videos
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A social network for dogs called DH (DogHouse) has k special servers to recompress uploaded videos of cute cats. After each video is uploaded, it should be recompressed on one (any) of the servers, and only after that it can be saved in the social network.

We know that each server takes one second to recompress a one minute fragment. Thus, any server takesm seconds to recompress a m minute video.

We know the time when each of the n videos were uploaded to the network (in seconds starting from the moment all servers started working). All videos appear at different moments of time and they are recompressed in the order they appear. If some video appeared at time s, then its recompressing can start at that very moment, immediately. Some videos can await recompressing when all the servers are busy. In this case, as soon as a server is available, it immediately starts recompressing another video. The videos that await recompressing go in a queue. If by the moment the videos started being recompressed some servers are available, then any of them starts recompressing the video.

For each video find the moment it stops being recompressed.

Input

The first line of the input contains integers n andk (1 ≤ n, k ≤ 5·105) — the number of videos and servers, respectively.

Next n lines contain the descriptions of the videos as pairs of integerssi, mi (1 ≤ si, mi ≤ 109), where si is the time in seconds when thei-th video appeared and mi is its duration in minutes. It is guaranteed that all thesi's are distinct and the videos are given in the chronological order of upload, that is in the order of increasingsi.

Output

Print n numbers e1, e2, ..., en, whereei is the time in seconds after the servers start working, when thei-th video will be recompressed.

Examples
Input
3 21 52 53 5
Output
6711
Input
6 11 10000000002 10000000003 10000000004 10000000005 10000000006 3
Output
100000000120000000013000000001400000000150000000015000000004

D题目大意:

给你N个任务单,每个任务单有两个元素,si,mi,分别表示来的时间,以及需要完成这个订单的时间.

问你最优去做的话,每个订单的结束时间是?


思路:


过程模拟一个优先队列即可,优先级为完成时间最早.

那么每一次来一个订单的时候,先把完成了的订单任务从队列中pop出去。

然后判断现在机器是否用满了,如果没用满,那么对应拿出一个空机器给当前任务。

否则如果已经用满了,那么对应取队头元素,等待这份订单结束之后,这台机器供给当前订单使用即可。

优先队列模拟一下,注意数据范围。


Ac代码:

#include<stdio.h>#include<string.h>#include<queue>using namespace std;#define ll __int64struct node{    ll pos;ll end;    friend bool operator < (node a,node b)    {        return a.end>b.end;    }}now,nex;ll ans[3000000];int main(){    ll n,m;    while(~scanf("%I64d%I64d",&n,&m))    {        priority_queue<node>s;        for(ll i=0;i<n;i++)        {            ll a,b;            scanf("%I64d%I64d",&a,&b);            while(s.size()>0&&s.top().end<a)ans[s.top().pos]=s.top().end,s.pop();            if(s.size()<m)            {                now.end=a+b;                now.pos=i;                s.push(now);            }            else            {                now.end=s.top().end+b;                ans[s.top().pos]=s.top().end;                s.pop();                now.pos=i;                s.push(now);            }        }        while(!s.empty())        {            ans[s.top().pos]=s.top().end;            s.pop();        }        for(ll i=0;i<n;i++)        {            printf("%I64d\n",ans[i]);        }    }}







0 0
原创粉丝点击