1. 程式人生 > >Codeforces1076F. Summer Practice Report(貪心+動態規劃)

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