Codeforces1076F. 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≤3105, 1≤k≤106

).

The second line contains n
integers x1,x2,…,xn (1≤xi≤106) — the number of tables on the i

-th page.

The third line contains n
integers y1,y2,…,yn (1≤yi≤106) — 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
Copy

2 2
5 5
2 2

Output
Copy

YES

Input
Copy

2 2
5 6
2 2

Output
Copy

NO

Input
Copy

4 1
4 1 10 1
3 2 10 1

Output
Copy

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.
View Code

题目大意:

  Vova在写n页报告,每页的报告有xi个表格和yi个公式,他只能连续写最多k个表格(公式),然后就要切换到写公式(表格),否则就会感到疲惫。

  Vova只能在写完第i页之后才能开始写第i+1页。问他是否能写完这n页报告而不感到疲惫。

  Vova再翻页的时候疲惫值不会更新。就是说之前那页写了a个表格的话,下一页若要先写表格,只能再连续写k-a个表格了。

思路:

  因为Vova只能在写完前一页之后才能写下一页,所以只能贪心地处理当前页

  对当前页有这样的写法:(因为表格和公式地位相等,不妨只考虑当前页先写表格的情况)

  ①:yi ≤ xi,那么就要尽量多地写表格。最好就是写k个表格之后写1个公式,再写k个表格、1个公式。。。最后还能写k个表格。这样的写法下,只要xi,yi满足yi ≤ xi ≤ k*yi + k,就能写完所有的表格。

  ②:xi < yi,此时要尽量多地写公式。最好就是写1个表格之后写k个公式,再写1个表格、k个公式。。。(这里不要再写1个表格,不然就少写了k个公式)这样的写法下,只要xi,yi满足xi > $\left \lceil y_{i}/k \right \rceil$,就能写完所有的公式。

  先写公式也是类似的。

  然后要考虑当前页写完之后对下一页的影响。前一页最后一步写的是公式(表格),会影响下一页先写公式(表格)的最大长度。此时只要记录前一页的最后一步写公式(表格)时的最短长度,在下一页先写公式(表格)时加上这个长度再进行上面①②的写法就可以了。注意:如果最后一步写的是表格(公式)的话,那么公式(表格)的长度就是0了。

  然后剩余长度不为0的情况:不妨设剩余的为公式(y),那这种情况下只能是yi > k*xi,尽量多地写公式还写不完。。。显然剩余的就是tmp = yi - k*xi了,然后如果tmp > k,那么说明根本写不完公式,那就肯定会“疲劳”了,输出NO。否则如果一直写到了最后一页,那就是YES。

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAX_N = 3e5 + 5;

int N, K;
//-1为不可能,其他为剩余的flag(0x,1y)的值。
bool ans;
ll judge(int flag, ll x, ll y)
{//y开始,剩y
    if(!flag)
        swap(x, y);
    ll l = y/K + (y%K > 0);
    ll r = K*y + K;
    if (l <= x && x <= r)
        return 0;
    ll tmp = y - K*x;
    return tmp;
}

ll f[MAX_N][2];//0:x, 1:y
ll x[MAX_N], y[MAX_N];

int main()
{
    cin >> N >> K;
    ans = true;
    for (int i = 1; i <= N; i++)
        scanf("%lld", x+i);
    for (int i = 1; i <= N; i++)
        scanf("%lld", y+i);

    f[0][0] = f[0][1] = 0;
    for (int i = 1; i <= N; i++) {
        f[i][0] = judge(0, x[i]+f[i-1][0], y[i]);
        f[i][1] = judge(1, x[i], y[i]+f[i-1][1]);
        if (f[i][0] > K || f[i][1] > K) {
            ans = false;
            break;
        }
    }
    if (ans)
        puts("YES");
    else
        puts("NO");
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/Lubixiaosi-Zhaocao/p/9971514.html
今日推荐