您的位置:首页 > 运维架构

Topcoder SRM 721 Div.2 B. RememberWordsEasy

2017-09-28 18:34 417 查看

B. RememberWordsEasy

Problem Statement

For Fox Ciel it is the beginning of a new school year. Her school year will consist of two semesters. The first semester contains d1 days and the second semester contains d2 days. Surprisingly, there are no breaks during or between the semesters: the entire school year consists of d1+d2 consecutive days of classes. Fox Ciel is taking an English class during both semesters. For the class she needs to learn a lot of new words: exactly w1 words during the first semester and exactly w2 words during the second semester. Ciel can learn arbitrarily many words on any single day. However, she does not like to change her workload too much. Therefore, the number of words she will learn on any two consecutive days must differ by at most one. Formally, suppose the days of the school year are numbered from 1 to d1+d2. Suppose that Ciel will learn x[i] words on day i. Ciel will be happy if the numbers x[i] have the following properties:

x[1] + … + x[d1] is exactly equal to w1

x[d1+1] + … + x[d1+d2] is exactly equal to w2

for each valid i, | x[i+1] - x[i] | is at most 1

You are given the ints d1, d2, w1, and w2. Return “Possible” if there is a schedule that makes Ciel happy, or “Impossible” if there is no such schedule.

Definition

Class:

RememberWordsEasy

Method:

isPossible

Parameters:

int, int, int, int

Returns:

string

Method signature:

string isPossible(int d1, int d2, int w1, int w2)

(be sure your method is public)

Limits

Time limit (s):

2.000

Memory limit (MB):

256

Stack limit (MB):

256

Constraints

d1 will be between 1 and 1,000,000, inclusive.

d2 will be between 1 and 1,000,000, inclusive.

w1 will be between 0 and 1,000,000, inclusive.

w2 will be between 0 and 1,000,000, inclusive.

Examples

0)

2

3

7

18

Returns: “Possible”

The school year has 2+3=5 days.Ciel needs to learn exactly 7 words during the first semester and exactly 18 words during the second semester.The only valid way to do so is to learn 3, 4, 5, 6, and 7 words during the five days of the school year.Note that 3+4=7 and 5+6+7=18.

1)

1

1

3

5

Returns: “Impossible”

Here the school year has just 1+1=2 days.Ciel must learn 3 words on the first day and 5 words on the second day.However, |3−5| is more than 1, so Ciel will not be happy with this schedule.

2)

3

5

300

500

Returns: “Possible”

One possible solution is to learn 100 words every day.

3)

100

1

0

2

Returns: “Impossible”

4)

1000000

1000000

1000000

1000000

Returns: “Possible”

题意

一位叫Ciel的学生要开始一个新学期,这个学期分为两部分,第一部分有d1天,第二部分有d2天,每个部分都有不同的工作量,第一个部分,Ciel需要在d1天里记住w1个单词;在第二个部分,Ciel需要在d2天里记住w2个单词。问能不能找出一种方法使她开心并且相邻两天的每天背单词数的差距不能多于1。P.S.第一部分的最后一天和第二部分的前一天是连在一起的,也需要满足上面的条件。

思路

这题我在比赛的时候fst了…我的思路是枚举第一部分的结尾那一天她背了多少个单词,然后根据最后一项求出前面这一部分背的最多的单词的可能和背的最少的单词的可能。如果d1在这个范围内,那么最后一天背那么多单词是可行的。就记录一下。然后再枚举第二部分的第一天的背单词数cnt,用同样的方法求出是否可行,如果可行看看cnt-1,cnt,cnt+1是否有一个是可以为第一部分最后一天背的单词数,如果有就是Possible,否则如果扫完了还没有就是Impossible。

Code

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

class RememberWordsEasy {
public:
string isPossible( int d1, int d2, int w1, int w2 ) {
bool vis[1000050];
memset(vis,0,sizeof vis);
for(int i=0;i<=1000000;i++) {
ll lst=max(i-d1,0);
ll leastsum=(ll)(i+lst)*(i-lst+1)/2+(d1-(i-lst+1))*lst,mostsum=(ll)(i+i+d1-1)*d1/2LL;
cout<<i<<" "<<leastsum<<" "<<mostsum<<endl;
if(leastsum>w1)
break;
if(mostsum<w1)
continue;
cout<<i<<endl;
vis[i]=1;
}
for(int i=0;i<=1000000;i++) {
ll lst=max(i-d2,0);
ll mostsum=(ll)(i+i+d2-1)*d2/2LL,leastsum=(ll)(i+lst)*(i-lst+1)/2+(d2-(i-lst+1))*lst;
if(leastsum>w2)
break;
if(mostsum<w2)
continue;
if(vis[i]||vis[i-1]||vis[i+1])
return "Possible";
}
return "Impossible";
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  topcoder入门 算法 c++