【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 rectangular w mm ?×? h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how.

In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments.

After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process.

Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree?

Input

The first line contains three integers w,?h,?n (2?≤?w,?h?≤?200?000, 1?≤?n?≤?200?000).

Next n lines contain the descriptions of the cuts. Each description has the form H y or V x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1?≤?y?≤?h?-?1) from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1?≤?x?≤?w?-?1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won’t make two identical cuts.

Output

After each cut print on a single line the area of the maximum available glass fragment in mm2.

Sample test(s)

input

4 3 4

H 2

V 2

V 3

V 1

output

8

4

4

2

input

7 6 5

H 4

V 3

V 5

H 2

V 1

output

28

16

12

6

4

最大那一块一定是选择长与宽都最大的,所以我们对于长和宽s分别用set来存当前有哪些切割点,以及每一段的长度。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <set>
using namespace std;
multiset<int> s,h;
set<int> s1,s2,h1,h2;
char c[5];
int n,m,q,k;
int main()
{
    scanf("%d%d%d",&m,&n,&q);
    s.insert(m),s1.insert(0),s1.insert(m),s2.insert(0),s2.insert(-m);
    h.insert(n),h1.insert(0),h1.insert(n),h2.insert(0),h2.insert(-n);
    while (q--)
    {
        scanf("%s%d",c,&k);
        int a,b;
        if (c[0]==‘V‘)
        {
            b=*s1.lower_bound(k),a=-*s2.lower_bound(-k);
            s1.insert(k),s2.insert(-k);
            s.erase(s.find(b-a));
            s.insert(k-a),s.insert(b-k);
        }
        else
        {
            b=*h1.lower_bound(k),a=-*h2.lower_bound(-k);
            h1.insert(k),h2.insert(-k);
            h.erase(h.find(b-a));
            h.insert(k-a),h.insert(b-k);
        }
        cout<<(1LL*(*s.rbegin())*(*h.rbegin()))<<endl;
    }
    return 0;
}

B. Clique Problem

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn’t it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.

Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let’s form graph G, whose vertices are these points and edges connect exactly the pairs of points (i,?j), such that the distance between them is not less than the sum of their weights, or more formally: |xi?-?xj|?≥?wi?+?wj.

Find the size of the maximum clique in such graph.

Input

The first line contains the integer n (1?≤?n?≤?200?000) — the number of points.

Each of the next n lines contains two numbers xi, wi (0?≤?xi?≤?109,?1?≤?wi?≤?109) — the coordinate and the weight of a point. All xi are different.

Output

Print a single number — the number of vertexes in the maximum clique of the given graph.

Sample test(s)

input

4

2 3

3 1

6 1

0 2

output

3

Note

If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!

思路题+贪心

假设i>j,把式子变形一下:xi?wi≥xj+wj。

Tutorial中的的解法很简单:

我的做法和上面本质差不多:

记录m[i]表示答案取到i的时候x与w和的最小值,每次求答案都是二分找第一个比差小的和。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define inf 1000000005
#define M 200000+5
using namespace std;
int m[M],f[M],n,ans;
struct data
{
    int x,w,c,h;
}a[M];
bool cmp(data a,data b)
{
    return a.x<b.x;
}
int Find(int x)
{
    int l=0,r=ans,re=0;
    while (l<=r)
    {
        int mid=(l+r)>>1;
        if (m[mid]<=x) re=mid,l=mid+1;
        else r=mid-1;
    }
    return re;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
        scanf("%d%d",&a[i].x,&a[i].w),a[i].h=a[i].x+a[i].w,a[i].c=a[i].x-a[i].w;
    sort(a+1,a+1+n,cmp);
    for (int i=1;i<=n;i++)
        m[i]=inf;
    f[1]=1,m[1]=a[1].h;
    ans=1;
    for (int i=2;i<=n;i++)
    {
        int k=Find(a[i].c);
        f[i]=k+1;
        ans=max(ans,f[i]);
        if (a[i].h<m[f[i]])
            m[f[i]]=a[i].h;
    }
    cout<<ans<<endl;
    return 0;
}

D. Fuzzy Search

time limit per test3 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters ‘A’, ‘T’, ‘G’ and ‘C’.

Let’s consider the following scenario. There is a fragment of a human DNA chain, recorded as a string S. To analyze the fragment, you need to find all occurrences of string T in a string S. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem.

Let’s write down integer k?≥?0 — the error threshold. We will say that string T occurs in string S on position i (1?≤?i?≤?|S|?-?|T|?+?1), if after putting string T along with this position, each character of string T corresponds to the some character of the same value in string S at the distance of at most k. More formally, for any j (1?≤?j?≤?|T|) there must exist such p (1?≤?p?≤?|S|), that |(i?+?j?-?1)?-?p|?≤?k and S[p]?=?T[j].

For example, corresponding to the given definition, string “ACAT” occurs in string “AGCAATTCAT” in positions 2, 3 and 6.

Note that at k?=?0 the given definition transforms to a simple definition of the occurrence of a string in a string.

Help Leonid by calculating in how many positions the given string T occurs in the given string S with the given error threshold.

Input

The first line contains three integers |S|,?|T|,?k (1?≤?|T|?≤?|S|?≤?200?000, 0?≤?k?≤?200?000) — the lengths of strings S and T and the error threshold.

The second line contains string S.

The third line contains string T.

Both strings consist only of uppercase letters ‘A’, ‘T’, ‘G’ and ‘C’.

Output

Print a single number — the number of occurrences of T in S with the error threshold k by the given definition.

Sample test(s)

input

10 4 1

AGCAATTCAT

ACAT

output

3

Note

If you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid’s original approach, do not take everything described above seriously.

思路题+bitset

首先预处理出在S的每一位可以对应的数存在bitset中,什么意思呢?

就是在k步之内,有哪些字母可以到达。

接下来,我们用bitset整体判断S的每一位是否可以当做匹配的第一位。

for (int i=0;i<m;i++)
        b&=a[t[i]]>>i;

完整代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <bitset>
#define M 200000+5
#define inf 0x3f3f3f3f
using namespace std;
char s[M],t[M];
int n,m,k,pre[5],ne[5];
bitset<M> a[5],b;
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    scanf("%s%s",s,t);
    for (int i=0;i<n;i++)
        s[i]=s[i]==‘A‘?0:s[i]==‘T‘?1:s[i]==‘C‘?2:3;
    for (int i=0;i<m;i++)
        t[i]=t[i]==‘A‘?0:t[i]==‘T‘?1:t[i]==‘C‘?2:3;
    pre[0]=pre[1]=pre[2]=pre[3]=-inf;
    for (int i=0;i<n;i++)
    {
        pre[s[i]]=i;
        for (int j=0;j<4;j++)
            if (i-pre[j]<=k)
                a[j][i]=1;
    }
    ne[0]=ne[1]=ne[2]=ne[3]=inf;
    for (int i=n-1;i>=0;i--)
    {
        ne[s[i]]=i;
        for (int j=0;j<4;j++)
            if (ne[j]-i<=k)
                a[j][i]=1;
    }
    for (int i=0;i<n;i++)
        b[i]=1;
    for (int i=0;i<m;i++)
        b&=a[t[i]]>>i;
    int ans=0;
    for (int i=0;i<n;i++)
        ans+=b[i];
    cout<<ans<<endl;
    return 0;
}
时间: 2024-12-14 16:02:02

【codeforces #296(div 1)】ABD题解的相关文章

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 Round #529(Div.3)个人题解

Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Repeating Cipher 传送门 题意:第一个字母写一次,第二个字母写两次,依次递推,求原字符串是什么 题解:1.2.3.4,非常明显的d=1的等差数列,所以预处理一个等差数列直接取等差数列的每一项即可 代码: #include<bits/stdc++.h> using namespace s

Codeforces #258 Div.2 E Devu and Flowers

大致题意: 从n个盒子里面取出s多花,每个盒子里面的花都相同,并且每个盒子里面花的多数为f[i],求取法总数. 解题思路: 我们知道如果n个盒子里面花的数量无限,那么取法总数为:C(s+n-1, n-1) = C(s+n-1, s). 可以将问题抽象成:x1+x2+...+xn = s, 其中0<=xi <= f[i],求满足条件的解的个数. 两种方法可以解决这个问题: 方法一:这个问题的解可以等价于:mul = (1+x+x^2+...+x^f[1])*(1+x+x^2+...+x^f[2]

CF#247(Div. 2)部分题解

引言: 在软件项目中,Maven提供了一体化的类库管理系统,非常实用.但是,如果新增的类库jar在网络上无法获取到,如何在本地按照Maven的规则添加进来呢?本文将通过一个小例子展示新增过程. 背景介绍: 一个Maven管理的Java项目,提供一个系统级别的POM.xml,其中定义了整个项目使用的类库. 需求: 需要添加一个自定义的类库到当前项目中.假定当前的类库文件名为:abc.jar.. 如何将类库添加进来? 1.  找到当前Maven的Repository类库位置 一般默认情况下,在win

Codeforces A. Valera and X 题解

判断二维字符串是否满足下面条件: on both diagonals of the square paper all letters are the same; all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals. Help Valera, write the progra

codeforces A. Shaass and Oskols 题解

Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delici

Codeforces #259 Div.2

A. Little Pony and Crystal Mine 模拟题. 用矩阵直接构造或者直接根据关系输出 B. Little Pony and Sort by Shift 模拟题. 通过提供的操作得到的序列只能是两段递增或者整个序列递增. 那么可以求得第一段递增序列长度为0-p 如果整个序列是递增,即 p= n-1 那么操作次数就是0. 否则,假设是两段递增,把原始的序列恢复出来,设当前序列是AB,那么A就是0-p BA = (A'B')', '表示对序列进行翻转, 如果BA是有序的,那么需

Codeforces #250 (Div. 2) C.The Child and Toy

之前一直想着建图...遍历 可是推例子都不正确 后来看数据好像看出了点规律 就抱着试一试的心态水了一下 就....过了..... 后来想想我的思路还是对的 先抽象当前仅仅有两个点相连 想要拆分耗费最小,肯定拆相应权值较小的 在这个基础上考虑问题就能够了 代码例如以下: #include <cstdio> #include <iostream> #include <algorithm> #define MAXN 10010 #define ll long long usi

Codeforces D. Giving Awards 412 题解

就是按照一定顺序输出排序. 比如a欠b的钱就不能先输出a然后输出b. 本题的技巧就是,要求的是不能先输出a然后输出b,但是可以先输出b然后输出a. 故此可以按照a欠b的钱的关系,建立图,然后DFS深度优先搜索,然后逆向记录点,输出这些逆向点,也就是a欠b的钱,就先输出b然后输出a,那么这个顺序就满足要求了. 很狡猾的题意.要细心.不然就搞半天都白搞了. 题目连接:http://codeforces.com/problemset/problem/412/D #include <stdio.h>