首页  »   编程

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

【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

#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!

Tutorial中的的解法很简单：

#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;
}


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.

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;
}