Codeforces Round 1076 (Educational Codeforces Round 54 Rated for Div. 2) F. Summer Practice Report

F. Summer Practice Report

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vova has taken his summer practice this year and now he should write a report on how it went.

Vova has already drawn all the tables and wrote down all the formulas. Moreover, he has already decided that the report will consist of exactly n pages and the i-th page will include xi tables and yi formulas. The pages are numbered from 1 to n.

Vova fills the pages one after another, he can't go filling page i+1 before finishing page i and he can't skip pages.

However, if he draws strictly more than k tables in a row or writes strictly more than k formulas in a row then he will get bored. Vova wants to rearrange tables and formulas in each page in such a way that he doesn't get bored in the process. Vova can't move some table or some formula to another page.

Note that the count doesn't reset on the start of the new page. For example, if the page ends with 3 tables and the next page starts with 5 tables, then it's counted as 8 tables in a row.

Help Vova to determine if he can rearrange tables and formulas on each page in such a way that there is no more than k tables in a row and no more than k formulas in a row.

Input

The first line contains two integers n and k (1≤n≤3⋅10^5, 1≤k≤10^6).

The second line contains n integers x1,x2,…,xn (1≤xi≤10^6) — the number of tables on the i-th page.

The third line contains n integers y1,y2,…,yn (1≤yi≤10^6) — the number of formulas on the i-th page.

Output

Print "YES" if Vova can rearrange tables and formulas on each page in such a way that there is no more than k tables in a row and no more than k formulas in a row.

Otherwise print "NO".

Examples

input

2 2
5 5
2 2

output

YES

input

2 2
5 6
2 2

output

NO

input

4 1
4 1 10 1
3 2 10 1

output

YES

Note

In the first example the only option to rearrange everything is the following (let table be 'T' and formula be 'F'):

  • page 1: "TTFTTFT"
  • page 2: "TFTTFTT"

That way all blocks of tables have length 2.

In the second example there is no way to fit everything in such a way that there are no more than 2 tables in a row and 2 formulas in a row.

这算一道看起来似乎并不难,但是你就是想不到的类型的题。看过题解后才恍然大悟,但到现在补题人数不太多啊,不太懂。

题意:

一本书有n页,你假设每一页书都有xi个'T'和yi个'F',你可以对每一页的T和F随便排成一个序列,之后把每一页的序列依次拼接成另外一个序列。问是否存在一种情况使得连续的'T'或者'F'的数量最多不会超过k。

思路:

我们首先考虑对于一页书的情况,我们都会贪心用少的那一种平均的去分割多的那一种。那么对于要把所有页的书拼起来的话,只有末尾连续相同的才会对后面的决策产生影响,那么我们就可以考虑使用dp[i][j]表示对于第i页时最后的那一种是j时第i页末尾存在最少连续相同数量'T或者'F'的个数。简而言之就是单独页用贪心思想,而拼起来的用dp去维护状态。

要求 T 结尾的转移分两种情况:

1.对于我们考虑上一页结尾是 T 的又分两种情况:

1-1.当 F 的数量能恰好把连同上一页的 T 分割为xxxTFTTTTF...(xxx指代上一页的若干个 T ,"..."指当前页的若干个 T ,要求保证除"..."外连续的 T 恰好是k个),因为要求结尾是 T 所以"..."这里 T 的个数至少要有1个,"..."的个数更新答案。

1-2.当 F 的数量比1-1多的时候,无论怎么分都能使"..."这里 T 的个数恰好只有1个,另外,还需要考虑 F 大于 T 的情况,如果 T 无法按要求分割 F ,那也是不行的,即 F 的数量大于 T 的数量乘k。

2.对于我们考虑上一页结尾是 F 的又分两种情况:

2-1.因为上一页结尾是 F 那么当 F 的数量能恰好分割 T 的情况为TTTTFTTTTF...,"..."的个数更新答案

2-2.当 F 的数量比2-1多的时候,无论怎么分都能使"..."这里 T 的个数恰好只有1个。与1-2不同的是,因为要求结尾是 T 且 F 是上一页的结尾,所以还需要满足 F 的数量加上一页结尾 F 的数量小于等于 T 的数量乘(k-1)。

考虑 F 结尾时和 T 结尾类似,交换一下值即可。

看dp[n][0]和dp[n][1]的值是否小于等于k即可得出结果。

示例程序

#include <bits/stdc++.h>
#define LDQ 1000000007
#define QAQ 0x3f3f3f3f
#define INF 1000000000000000
#define PI 3.14159265358979323846
#define inv2 ((LDQ+1)>>1)
#define eps 1e-8
using namespace std;
struct jj
{
    int x,y;
}a[300000];
int f(int prex,int prey,int x,int y,int k)
{
    int num,ans=QAQ;                //两个状态都不符合更新要求直接返回最大值
    if(prex<=k)                     //如果上一个状态不符合要求也就没必要去跑了
    {
        num=(prex+x+k-1)/k-1;       //求最少需要多少个才能分割为1-1的形式,需要连上一页一起算进去
        if(y==num)                                   //1-1
        {
            ans=prex+x-num*k;
        }
        else if(y>num&&y<=(long long)x*k)                   //1-2
        {
            ans=1;
        }
    }
    if(prey<=k)                     //同理
    {
        num=(x+k-1)/k-1;                    //求最少需要多少个才能分割为2-1的形式,不需要连上一页一起算进去
        if(y==num)                          //2-1
        {
            ans=min(ans,x-num*k);
        }
        else if(y>num&&y-k+prey<=(long long)(x-1)*k)               //2-2
        {
            ans=1;
        }
    }
    return ans;
}
int main()
{
    int i,n,k,pre0=0,pre1=0,next0,next1;                //这里我们直接用4个变量维护dp即可
    scanf("%d %d",&n,&k);
    for(i=0;n>i;i++)
    {
        scanf("%d",&a[i].x);
    }
    for(i=0;n>i;i++)
    {
        scanf("%d",&a[i].y);
    }
    for(i=0;n>i;i++)
    {
        next0=f(pre0,pre1,a[i].x,a[i].y,k);                 //求T结尾的
        next1=f(pre1,pre0,a[i].y,a[i].x,k);                 //求F结尾的
        pre0=next0;                                         //转移
        pre1=next1;
    }
    if(pre0<=k||pre1<=k)
    {
        puts("YES");
    }
    else
    {
        puts("NO");
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值