P7912 [CSP-J 2021] 小熊的果篮 (纪念洛谷第一次一遍过)

[CSP-J 2021] 小熊的果篮

题目描述

小熊的水果店里摆放着一排 n n n 个水果。每个水果只可能是苹果或桔子,从左到右依次用正整数 1 , 2 , … , n 1, 2, \ldots, n 1,2,,n 编号。连续排在一起的同一种水果称为一个“块”。小熊要把这一排水果挑到若干个果篮里,具体方法是:每次都把每一个“块”中最左边的水果同时挑出,组成一个果篮。重复这一操作,直至水果用完。注意,每次挑完一个果篮后,“块”可能会发生变化。比如两个苹果“块”之间的唯一桔子被挑走后,两个苹果“块”就变成了一个“块”。请帮小熊计算每个果篮里包含的水果。

输入格式

第一行,包含一个正整数 n n n,表示水果的数量。

第二行,包含 n n n 个空格分隔的整数,其中第 i i i 个数表示编号为 i i i 的水果的种类, 1 1 1 代表苹果, 0 0 0 代表桔子。

输出格式

输出若干行。

i i i 行表示第 i i i 次挑出的水果组成的果篮。从小到大排序输出该果篮中所有水果的编号,每两个编号之间用一个空格分隔。

样例 #1

样例输入 #1

12
1 1 0 0 1 1 1 0 1 1 0 0

样例输出 #1

1 3 5 8 9 11
2 4 6 12
7
10

样例 #2

样例输入 #2

20
1 1 1 1 0 0 0 1 1 1 0 0 1 0 1 1 0 0 0 0

样例输出 #2

1 5 8 11 13 14 15 17
2 6 9 12 16 18
3 7 10 19
4 20

样例 #3

样例输入 #3

见附件中的 fruit/fruit3.in。

样例输出 #3

见附件中的 fruit/fruit3.ans。

提示

【样例解释 #1】

这是第一组数据的样例说明。

所有水果一开始的情况是 [ 1 , 1 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 0 ] [1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0] [1,1,0,0,1,1,1,0,1,1,0,0],一共有 6 6 6 个块。

在第一次挑水果组成果篮的过程中,编号为 1 , 3 , 5 , 8 , 9 , 11 1, 3, 5, 8, 9, 11 1,3,5,8,9,11 的水果被挑了出来。

之后剩下的水果是 [ 1 , 0 , 1 , 1 , 1 , 0 ] [1, 0, 1, 1, 1, 0] [1,0,1,1,1,0],一共 4 4 4 个块。

在第二次挑水果组成果篮的过程中,编号为 2 , 4 , 6 , 12 2, 4, 6, 12 2,4,6,12 的水果被挑了出来。

之后剩下的水果是 [ 1 , 1 ] [1, 1] [1,1],只有 1 1 1 个块。

在第三次挑水果组成果篮的过程中,编号为 7 7 7 的水果被挑了出来。

最后剩下的水果是 [ 1 ] [1] [1],只有 1 1 1 个块。

在第四次挑水果组成果篮的过程中,编号为 10 10 10 的水果被挑了出来。

【数据范围】

对于 10 % 10 \% 10% 的数据, n ≤ 5 n \le 5 n5
对于 30 % 30 \% 30% 的数据, n ≤ 1000 n \le 1000 n1000
对于 70 % 70 \% 70% 的数据, n ≤ 50000 n \le 50000 n50000
对于 100 % 100 \% 100% 的数据, 1 ≤ n ≤ 2 × 10 5 1 \le n \le 2 \times {10}^5 1n2×105

【提示】

由于数据规模较大,建议 C/C++ 选手使用 scanfprintf 语句输入、输出。

解题思路

题目要求很简单,就是每次从不同的01段中,取出每段的第一个0或者1.
然后取完可能引起段的合并。看了一下题目难度普及/提高,所以直接数组模拟肯定是超限的。
我们采用链表形式,每个节点就是一个段,节点里用一个队列来存储里面包含节点的下标。然后模拟取水果过程即可。
对了数据很大,题目建议用c语言的输入输出写法,但是我习惯c++;
所以关闭c++的流同步也是有概率不超时的。不要用endl换行,endl换行会刷新流,很耗时,改为"\n"即可。

ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

代码如下:

#include
#include
using namespace std;
class node {
public:
	int type = 0;
	int size = 0;
	queue<int> que;
	node* next = NULL;

	void  npush(int i) {
		size++;
		que.push(i);

	}
	pair<int,int> npop() {
		int re;
		size--;
		if (size >= 0) {
			re = que.front();
			que.pop();
		}
		else {
			re = -1;
		}
		return pair<int, int>(re, size);
			
	}
};

int main() {
	ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n{};
	cin >> n;
	node* h = new node();
	node* it = h;
	h->type = 2;
	
	node* s = new node();
	it->next = s;

	int tmp{};
	cin >> tmp;

	int last{ tmp };

	s->type = tmp;
	s->npush(1);

	for (int i = 2; i <= n; i++) {

		cin >> tmp;
		if (tmp == last) {
			s->npush(i);
			
		}
		else {
			
			it = s;
			s = new node();
			s->type = tmp;
			s->npush(i);
			it->next = s;
		}
		last = tmp;
	}
	

	
		
	while (n) {

		it = h;
		node* itpre0 = NULL;
		while (it->next) {
			
			pair<int,int> re = it->next->npop();
			cout << re.first << " ";
			n--;

			if (re.second == 0) {
				if(itpre0==NULL)
					itpre0 = it;
				it = it->next;
			}
			else {
				int nextflag = 0;
				if (itpre0 ) {
					//cout << " pre0 and size"<next->size << " ";

					while (itpre0->next and itpre0->next->size == 0) {
						node* del = itpre0->next;
						itpre0->next = itpre0->next->next;
						delete del;
						
						
						it = itpre0;
					}
					
						


					if (itpre0->next and (itpre0->type == itpre0->next->type)) {

						while (itpre0->next->size > 0){
							int tmp = itpre0->next->npop().first;
							itpre0->npush(tmp);
						}
						node* del = itpre0->next;
						itpre0->next = itpre0->next->next;
						delete del;
						nextflag = 1;
						
					}
				
					itpre0 = NULL;
					
				}
				if (nextflag == 0) it = it->next;
				
			}
			
		}
		if (itpre0) {
			node* del = itpre0->next;
			itpre0->next = NULL;
			delete del;
			itpre0 = NULL;
		}

		cout << "\n";
	}



}

在这里插入图片描述

写在最后

  • 好久不写链表了,真的生疏了好多。
  • 尽量先思考完整代码写法再动手,尤其是所需要的数据结构和类的成员之类的不要有大的改动。

你可能感兴趣的:(算法,开发语言,c++)