↓ Skip to main content
  1. Posts/

codeforces 496 C Removing Columns (构造)

·586 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:给一个n*m的由小写字母组成的table.要求从上往下每一行字典序不严格递增。问最少删除几列才能满足。

思路:一开始想的是用一个left数组维护每次删除后某一列左边是哪一列,目的是为了下次的判断。

再想了下,发现没必要。我们只需要知道,两行之间的关系是否确定就好了。over[i][j]为真表示第i行和第j行的胜负已分,对于胜负已分的行,大小无所谓。

对于胜负未分的行,如果table[i][j]>table[i+1][j],就必须要删掉这一列了。。。。

需要注意的是,某一列最多删一次,记得打上标记。

以及ove标记的时候,要在最后确定这一列没有被删以后再标记。。。用一个set存下可能的胜负已分的行的下标即可。

1A

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年10月04日 星期二 19时10分39秒
 4File Name :code/cf/problem/496C.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <set>
13#include <deque>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <bitset>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27using namespace std;
28const double eps = 1E-8;
29const int dx4[4]={1,0,0,-1};
30const int dy4[4]={0,-1,1,0};
31const int inf = 0x3f3f3f3f;
32const int N=105;
33char table[N][N];
34int n,m;
35bool mark[N];
36bool over[N][N];
37int left[N];
38int main()
39{
40	#ifndef  ONLINE_JUDGE
41	freopen("code/in.txt","r",stdin);
42  #endif
43	cin>>n>>m;
44	ms(mark,false);
45	ms(over,false);
46	for ( int i = 0 ; i < n ; i++) scanf("%s",table[i]);
47	int cnt = 0 ;
48	for ( int j = 0 ; j < m ; j++)
49	{
50	    set< int >se;
51	    bool del = false;
52	    for ( int i = 0 ; i < n-1 ; i++)
53	    {
54		if (table[i][j]<table[i+1][j])
55		{
56		    se.insert(i);
57		}
58		else if (table[i][j]>table[i+1][j]&&!over[i][i+1]&&!mark[j])
59		{
60		        cnt++;
61			del = true;
62			mark[j] = true;
63		//	cout<<"delete:"<<j<<endl;
64		}
65	    }
66	    if (!del)
67	    {
68		for ( auto it = se.begin (); it!=se.end(); it++)
69		{
70		    over[*it][*it+1] = true;
71		}
72	    }
73	}
74	printf("%d\n",cnt);
75  #ifndef ONLINE_JUDGE
76  fclose(stdin);
77  #endif
78    return 0;
79}

Related

codeforces 509 B. Painting Pebbles (构造)

·382 words·1 min
题目链接 题意:n堆石子,每堆a[i]个,k种颜色。给每个石子涂色,要求对于每种颜色,任意两堆中该颜色石子的个数最多差一个。问是否有解,有解输出一组方案。 思路:我们发现有解与否只和最大值最小值有关。

codeforces 468 A. 24 Game (构造)

·618 words·2 mins
题目链接 题意:给出n,有1..n n个数,可以选择两个数进行加,减,乘,三种操作,操做完得到一个数放回。 n-1次操作后只剩下一个数。现在要求剩下的数为24.问方法。 思路:我们发现。。。两个数相减可以为1.。那么只要找到4个数的方案和5个数的方案就好了。。。

codeforces 623 A. Graph and String (构造)

·708 words·2 mins
题目链接:题目链接 题意:给出一个无向图,该图是通过仅包含‘a’ ‘b’ ‘c’三个字母,以规则“i,j之间有边,当且仅当s[i]和s[j]相同,或者s[i]和s[j]在字母表中相邻”(也就是只有’a’和’c’是没有边相连的)得到的,现在问能否还原这个字符串,如果能,输出任意一个解。

codeforces #368 div 2 C. Pythagorean Triples (构造,数学)

·953 words·2 mins
题目链接 题意:给出一个数,问包含这个数三个数组成的勾股数,输出另外两个数。 思路: 所谓勾股数,就是当组成一个直角三角形的三边长都为正整数时,我们就称这一组数为勾股数. 那么,组成一组勾股数的三个正整数之间,是否具有一定的规律可寻呢?下面我们一起来观察几组勾股数: 规律一:在勾股数(3,4,5)、(5,12,13)、(7,24,25)(9,40,41)中,我们发现 由(3,4,5)有:32=9=4+5 由(5,12,13)有:52=25=12+13 由(7,24,25)有:72=49=24+25 由(9,40,41)有:92=81=40+41. 即在一组勾股数中,当最小边为奇数时,它的平方刚好等于另外两个连续的正整数之和.因此,我们把它推广到一般,从而可得出以下公式: ∵(2n+1)²=4n²+4n+1=(2n²+2n)+(2n²+2n+1) ∴(2n+1)²+(2n²+2n)²=(2n²+2n+1)²(n为正整数) 证明(略) 勾股数公式一:(2n+1,2n²+2n,2n²+2n+1)(n为正整数) 规律二:在勾股数(6,8,10)、(8,15,17)、(10,24,26)中,我们发现 由(6,8,10)有:62=36+2×(8+10) 由(8,15,17)有:82=64=2×(15+17) 由(10,24,26)有:102=100=2×(24+26) 即在一组勾股数中,当最小边为偶数时,它的平方刚好等于两个连续整数之和的二倍,推广到一般,从而可得出另一公式: ∵(2n)2=4n2=2[(n2-1)+(n2+1)] ∴(2n)2+(n2-1)2=(n2+1)2(n≥2且n为正整数) 证明(略) 勾股数公式二:(2n,n²-1,n²+1)(n≥2且n为正整数) 利用以上两个公式,我们可以快速写出各组勾股数.