题目链接:Lisa's Workbook
Lisa just got a new math workbook. A workbook contains exercise problems, grouped into chapters.
- There are chapters in Lisa's workbook, numbered from to .
- The -th chapter has problems, numbered from to .
- Each page can hold up to problems. There are no empty pages or unnecessary spaces, so only the last page of a chapter may contain fewer than problems.
- Each new chapter starts on a new page, so a page will never contain problems from more than one chapter.
- The page number indexing starts at .
Lisa believes a problem to be special if its index (within a chapter) is the same as the page number where it's located. Given the details for Lisa's workbook, can you count its number of special problems?
Note: See the diagram in the Explanation section for more details.
简单说这道题就是让你找到题目号和页码数相等的题目数量。每章含的题目数不同,不同章节的题目不会出现在同一页中,每页能写下的题目数相同且有限制。页码数和每章题目编号都从1开始计数。最开始做这道题弄复杂了,把每章的题目数都先存在一个vector中,把每章占用的页码数也存在vector中,用的时候再循环读取,这样复杂度太高。其实每章题目书并不需要存下来,直接处理就可以了,每章处理完再输入下一章的题目数即可。还有,题目标号和页码数相等并不需要将每一个题目号都写出来和页码比较,因为一页上的题目序号是连续且有限的,只要页码数在这页包含的题目序号范围内即可,这页包含的题目数范围,从problem到problem+k-1,当然还需要判断每页是否都写满了,也就是说problem+k-1是不是小于每章题目数t,即problem<page<(problem+k-1<t ? problem+k-1:t),只要满足这个判断,就认为这页中有我们要找的特殊题目。翻页,题目数也增加k,继续循环,直到题目数大于章节包含的题目总数,就进入下一章节,再输入该章节包含的题目数t,题目编号也重新从1开始记起。直到所有章节题目都判断完,输出记录的特殊题目数即完成题目。源代码如下:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,k;
cin>>n>>k;
int chap=1;
int page=1;
int count=0;
int t;
cin>>t;
int prob=1;
while(chap<=n){
if(prob<=page&&page<=((prob+k-1)<t? (prob+k-1):t)){
count++;
}
page++;
prob+=k;
if(prob>t){
prob=1;
chap++;
cin>>t;
}
}
cout<<count<<endl;
return 0;
}