CodeForces 1072C Cram Time【思维题】

本文介绍了一个算法问题,旨在通过合理分配有限的学习时间来最大化阅读不同耗时书籍的数量。通过找到最接近总可用时间的累积时间点,算法确定了可以阅读的最大书籍数量,并详细解释了如何在两天内分配这些书籍以充分利用时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:http://codeforces.com/problemset/problem/1072/C

C. Cram Time

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

In a galaxy far, far away Lesha the student has just got to know that he has an exam in two days. As always, he hasn't attended any single class during the previous year, so he decided to spend the remaining time wisely.

Lesha knows that today he can study for at most aa hours, and he will have bb hours to study tomorrow. Note that it is possible that on his planet there are more hours in a day than on Earth. Lesha knows that the quality of his knowledge will only depend on the number of lecture notes he will read. He has access to an infinite number of notes that are enumerated with positive integers, but he knows that he can read the first note in one hour, the second note in two hours and so on. In other words, Lesha can read the note with number kk in kkhours. Lesha can read the notes in arbitrary order, however, he can't start reading a note in the first day and finish its reading in the second day.

Thus, the student has to fully read several lecture notes today, spending at most aa hours in total, and fully read several lecture notes tomorrow, spending at most bb hours in total. What is the maximum number of notes Lesha can read in the remaining time? Which notes should he read in the first day, and which — in the second?

Input

The only line of input contains two integers aa and bb (0≤a,b≤1090≤a,b≤109) — the number of hours Lesha has today and the number of hours Lesha has tomorrow.

Output

In the first line print a single integer nn (0≤n≤a0≤n≤a) — the number of lecture notes Lesha has to read in the first day. In the second line print nn distinct integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤a1≤pi≤a), the sum of all pipi should not exceed aa.

In the third line print a single integer mm (0≤m≤b0≤m≤b) — the number of lecture notes Lesha has to read in the second day. In the fourth line print mm distinct integers q1,q2,…,qmq1,q2,…,qm (1≤qi≤b1≤qi≤b), the sum of all qiqi should not exceed bb.

All integers pipi and qiqi should be distinct. The sum n+mn+m should be largest possible.

Examples

input

Copy

3 3

output

Copy

1
3 
2
2 1 

input

Copy

9 12

output

Copy

2
3 6
4
1 2 4 5

Note

In the first example Lesha can read the third note in 33 hours in the first day, and the first and the second notes in one and two hours correspondingly in the second day, spending 33 hours as well. Note that Lesha can make it the other way round, reading the first and the second notes in the first day and the third note in the second day.

In the second example Lesha should read the third and the sixth notes in the first day, spending 99 hours in total. In the second day Lesha should read the first, second fourth and fifth notes, spending 1212 hours in total.

题意:给a和b,分别是两天可以看书的时间,现在你有耗时1、2、3……的书,问你两天可以最多看几本书。

思路:要看的书尽量多,那么肯定是优先选择1、2、3……的书更优。那么找出刚好大于等于a+b的那个sum(1+到n),也就是说可以看n本书。因为你拥有的数字是从1~n,a+b=sum(1~n),也就是说a和b是可以刚好被这些数字耗完的,只要找出组成a或者b的数字,就可以求出另一天的情况了。那么怎么找出耗掉a的那些数字呢,一开始想的是从1开始,根据剩下的格子继续填,不行了就把前面的出队,后来想了想直接从最大的开始比较好,直接从n~1遍历填就能自动填完。

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;

ll a,b,co;
vector<ll>ans,ans2;

int main()
{
	std::ios::sync_with_stdio(false);
	cin>>a>>b;
	ll sum=a+b;
	ll pos=0;
	for(ll i=0;;i++)
	{
		ll now=(1+i)*i/2;
		ll nxt=(2+i)*(i+1)/2;
		if(now<=sum&&sum<nxt)
		{
			pos=i;
			break;
		}
	}
	for(ll i=pos;i>=1;i--)
	{
		if(a>=i)
		{
			ans.push_back(i);
			a-=i;
			co++;
		}
		else
		{
			ans2.push_back(i);
		}
	}
	cout<<co<<endl;
	for(ll i=0;i<ans.size();i++)
	{
		cout<<ans[i];
		if(i<ans.size()-1)
			cout<<" ";
	}
	cout<<endl;
	cout<<pos-co<<endl;
	for(int i=0;i<ans2.size();i++)
	{
		cout<<ans2[i];
		if(i<ans2.size()-1)
			cout<<" ";
	}
	cout<<endl;
	return 0;
}

 

### Codeforces 思维思路和技巧 #### 预处理的重要性 对于许多竞赛编程问而言,预处理能够显著提高效率并简化后续操作。通过提前计算某些固定的数据结构或模式匹配表,可以在实际求解过程中节省大量时间。例如,在字符串处理类目中预先构建哈希表来加速查找过程[^1]。 #### 算法优化策略 针对特定类型的输入数据设计高效的解决方案至关重要。当面对大规模测试案例时,简单的暴力破解往往无法满足时限要求;此时则需考虑更高级别的算法改进措施,比如动态规划、贪心算法或是图论中的最短路径算法等。此外,合理利用空间换取时间也是一种常见的优化手段[^2]。 #### STL库的应用价值 C++标准模板库提供了丰富的容器类型(vector, deque)、关联式容器(set,map)以及各种迭代器支持,极大地便利了程序开发工作。熟练掌握这些工具不仅有助于快速实现功能模块,还能有效减少代码量从而降低出错几率。特别是在涉及频繁插入删除场景下,优先选用双向队列deque而非单向链表list可获得更好的性能表现。 ```cpp #include <iostream> #include <deque> using namespace std; int main(){ deque<int> dq; // 向两端添加元素 dq.push_back(5); dq.push_front(3); cout << "Front element is: " << dq.front() << endl; cout << "Back element is : " << dq.back() << endl; return 0; } ``` #### 实际应用实例分析 以一道具体目为例:给定一系列查询指令,分别表示往左端/右端插入数值或者是询问某个指定位置到边界之间的最小距离。此目的关键在于如何高效地追踪最新状态而无需重复更新整个数组。采用双指针技术配合静态分配的一维数组即可轻松解决上述需求,同时保证O(n)级别的总运行成本[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值