#296 (div.2) C.Glass Carving

1.题目描述:点击打开链接

2.解题思路:本题要求每切一刀,输出目前的最大玻璃的面积。这道题的思路很明显:每次切完后找目前的最大长度,最大宽度,相乘即可。不过问题的关键是如何快速的找到这个最大值。

一开始我没有太好的思路,想用数组,但总感觉数组力不从心,不知道怎么才能很好地更新切完后的长度值,也不知道如何快速的找到这个最大值。接着想到了STL中的set,可以把所有的切割位置存放在set中,然后逐个找最大值。不过这样的结果就是效率过低,导致TLE了。看了别人的代码才恍然大悟:可以直接除掉被切割的线段,同时添加上切割后的线段。

但问题又来了,如何存放这些线段?答案是利用标记数组。如果线段值存在,标记为1,否则标记为0,这样就能很方便地更新线段值了。同时查找最长的线段也很方便,由于每次切割后,最大长度是不会增加的,因此只需要从切割前的最大长度开始,逐一减小,看是否存在即可。

3.代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

typedef long long LL;
#define N 200000+10
set<int>wd, hd;//记录切割的位置
int w, h, n;

set<int>::iterator i, j;
int wi[N], hi[N];//分别标记目前存在的边长值
void cut(set<int>&s, int*a, int p)
{
	s.insert(p), i = j = s.find(p);
	--i, ++j, --a[*j - *i];//除掉被分开的长/宽
	++ a[p - *i], ++a[*j - p];//新产生了两个长/宽
}
int main()
{
	//freopen("t.txt", "r", stdin);
	while (cin >> w >> h >> n)
	{
		int p, maxw, maxh;
		char c;
		memset(wi, 0, sizeof(wi));
		memset(hi, 0, sizeof(hi));
		wd.clear(), hd.clear();
		wd.insert(0), wd.insert(w);
		hd.insert(0), hd.insert(h);
		wi[w] = hi[h] = 1;
		maxw = w, maxh = h;
		while (n--)
		{
			cin >> c >> p;
			if (c == 'V')cut(wd, wi, p);
			else cut(hd, hi, p);
			while (!wi[maxw])--maxw;//找切割后的最大值
			while (!hi[maxh])--maxh;
			cout << (LL)maxh*maxw << endl;
		}
	}
	return 0;
}
时间: 2024-12-21 16:25:33

#296 (div.2) C.Glass Carving的相关文章

Codeforces Round #296 (Div. 2) C. Glass Carving(想法题)

传送门 Description Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular wmm  ×  h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understa

C. Glass Carving (CF Round #296 (Div. 2) STL--set的运用)

C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a re

Codeforces Round #296 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/527 A. Playing with Paper time limit per test:2 seconds memory limit per test:256 megabytes One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm ?×? b mm sheet

Codeforces 527C Glass Carving(Set)

题意  一块w*h的玻璃  对其进行n次切割  每次切割都是垂直或者水平的  输出每次切割后最大单块玻璃的面积 用两个set存储每次切割的位置   就可以比较方便的把每次切割产生和消失的长宽存下来  每次切割后剩下的最大长宽的积就是答案了 #include <bits/stdc++.h> using namespace std; const int N = 200005; typedef long long LL; set<int>::iterator i, j; set<i

Codeforces 527C Glass Carving

vjudge 上题目链接:Glass Carving 题目大意: 一块 w * h 的玻璃,对其进行 n 次切割,每次切割都是垂直或者水平的,输出每次切割后最大单块玻璃的面积: 用两个 set 存储每次切割的位置,就可以比较方便的把每次切割产生和消失的长宽存下来(用个 hash 映射数组记录下对应值的长宽的数量即可,O(1) 时间维护),每次切割后剩下的最大长宽的积就是答案了: 1 #include<cstdio> 2 #include<cstring> 3 #include<

【codeforces #296(div 1)】ABD题解

A. Glass Carving time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectan

Codeforces 527C Glass Carving (最长连续0变形+线段树)

Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm ?×? h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what t

C. Glass Carving 正着做或者倒着做都可以

http://codeforces.com/problemset/problem/527/C 这题总体思路就是,每画一条线,然后就找到x间距的最max值和y间距的最max值,相乘就是当前的ans 那么我需要维护这样的一个数列,每次往里面添加一个元素,然后查询相邻两个元素的差值的最大值. 倒着做比较简单,首先把所有元素插上去,然后最大值直接暴力算一次.然后后来只有删除操作,这个操作只会让最大值变大. 每次删除后,检查上面和下面的新间距,和最大值比较一下就好. #include <bits/stdc

Codeforces Round #296 (Div. 2) A. Playing with Paper

A. Playing with Paper One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm ?×? b mm sheet of paper (a?>?b). Usually the first step in making an origami is making a square piece of paper from the