Codeforces Round #296 (Div. 2)

本文提供CodeForces比赛527题目的解析与代码实现,涵盖纸张折叠、错误纠正系统、玻璃雕刻及派系问题等算法挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A题:http://codeforces.com/contest/527/problem/A  

A. Playing with Paper
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 rectangular sheet by folding the sheet along the bisector of the right angle, and cutting the excess part.

After making a paper ship from the square piece, Vasya looked on the remaining (a - b) mm  ×  b mm strip of paper. He got the idea to use this strip of paper in the same way to make an origami, and then use the remainder (if it exists) and so on. At the moment when he is left with a square piece of paper, he will make the last ship from it and stop.

Can you determine how many ships Vasya will make during the lesson?

Input

The first line of the input contains two integers ab (1 ≤ b < a ≤ 1012) — the sizes of the original sheet of paper.

Output

Print a single integer — the number of ships that Vasya will make.

Sample test(s)
input
2 1
output
2
input
10 7
output
6
input
1000000000000 1
output
1000000000000
Note

Pictures to the first and second sample test.


题目分析:这个题目一看就有种辗转相除法的感觉,但是肯定不能那么直接的模拟,因为数据量有10的12次方,像最后一个数据,如果一个个减的话不知道何时才能完成,但是如果a是b的好多倍,我们可以发现,可以先制作a/b个b,然后再递归处理(b,a%b)

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<fstream>
using namespace std;
typedef long long ll;
const int M = 10;
int m[30][30];
ll deal(ll a,ll b)
{

    if(a<b)
        swap(a,b);
    if(b==0)
        return 0;
    if(a==b)
        return 1;
    return a/b+deal(b,a%b);
}
int main() {
    ll a,b;
    while(cin>>a>>b)
    {
        cout<<deal(a,b)<<endl;
    }
        
    return 0;
}


B题: http://codeforces.com/contest/527/problem/B

B. Error Correct System
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings S and T of equal length to be "similar". After a brief search on the Internet, he learned about the Hamming distance between two strings S and T of the same length, which is defined as the number of positions in which S and T have different characters. For example, the Hamming distance between words "permanent" and "pergament" is two, as these words differ in the fourth and sixth letters.

Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string S, so that the Hamming distance between a new string S and string T would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings.

Help him do this!

Input

The first line contains integer n (1 ≤ n ≤ 200 000) — the length of strings S and T.

The second line contains string S.

The third line contains string T.

Each of the lines only contains lowercase Latin letters.

Output

In the first line, print number x — the minimum possible Hamming distance between strings S and T if you swap at most one pair of letters in S.

In the second line, either print the indexes i and j (1 ≤ i, j ≤ ni ≠ j), if reaching the minimum possible distance is possible by swapping letters on positions i and j, or print "-1 -1", if it is not necessary to swap characters.

If there are multiple possible answers, print any of them.

Sample test(s)
input
9
pergament
permanent
output
1
4 6
input
6
wookie
cookie
output
1
-1 -1
input
4
petr
egor
output
2
1 2
input
6
double
bundle
output
2
4 1
Note

In the second test it is acceptable to print i = 2j = 3.


分析:可以记录每一位不同的时候,前者后者都是哪些字符,然后相应的下标是啥,如m[c1][c2]=i表示第i为的时候在S里面是c1在T里面是c2,那么如果要有最大程度的改变,最好是m[c1][c2]和m[c2][c1]同时存在,且c1!=c2,这样的话交换一直,可以又有两位相同,这样没找到的话,再去找能使得一位相同的下标。

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<fstream>
using namespace std;
const int M = 10;
int m[30][30];
int main() {
    int n;
    while(cin>>n)
    {
        memset(m,0,sizeof(m));
        string s,t;
        cin>>s>>t;
        int ret = 0;
        for(int i=1;i<=s.length();i++)
        {
            char c1 = s[i-1];
            char c2 = t[i-1];
            m[c1-'a'][c2-'a'] = i;
            if(c1==c2)
                ret++;
        }
        int ret1 = -1;
        int ret2 = -1;
        for(char c1 = 'a';c1<='z';c1++)
        {
            for(char c2='a';c2<='z';c2++)
            {
                if(c1!=c2&&m[c1-'a'][c2-'a']>0&&m[c2-'a'][c1-'a']>0)
                {
                    ret1 = m[c1-'a'][c2-'a'];
                    ret2 = m[c2-'a'][c1-'a'];
                }
            }
        }
        if(ret1>0)
        {
            cout<<n-(ret+2)<<endl;
            cout<<ret1<<" "<<ret2<<endl;
            continue;
        }
        for(char c1 = 'a';c1<='z';c1++)
        {
            for(char c2='a';c2<='z';c2++)
            {
                if(c1!=c2&&m[c1-'a'][c2-'a']>0)
                {
                    for(char c3='a';c3<='z';c3++)
                    {
                        if(m[c2-'a'][c3-'a']>0&&c2!=c3)
                        {
                            ret1 = m[c1-'a'][c2-'a'];
                            ret2 = m[c2-'a'][c3-'a'];
                        }
                    }
                }
            }
        }
        if(ret1>0)
        {
            cout<<n-(ret+1)<<endl;
            cout<<ret1<<" "<<ret2<<endl;
            continue;
        }
        cout<<n-ret<<endl;
        cout<<ret1<<" "<<ret2<<endl;


    }
        
    return 0;
}


C题: http://codeforces.com/contest/527/problem/C

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 rectangular wmm  ×  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 0001 ≤ 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
Note

Picture for the first sample test:

Picture for the second sample test:

分析:这题其实感觉蛮难想的,当然也是我水平太挫啦。。。。。这道题其实是在考set这个数据结构,因为stl里set是由红黑树实现的,所以set内部其实是有序的,然后描述一个长方体只需要两个变量,长和宽,就算有很多长方体,我们也可以保存一个长的set,一个宽的set,就能说清一共有多少种了,要拿最大面积的话,直接去set的最末元素即可,同时我们需要用set来保存已有的划线,假设新来了一根划线,根据set.find()函数能很快查找到它的前后,把前后两根线对应的长或宽去掉,补充进新的长或宽。

代码:

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;
typedef long long ll;
set<int> H, W;
multiset<int> MH, MW;
int main()
{
    int w, h, n;
    cin >> w >> h >> n;
    W.insert(0);
    W.insert(w);
    H.insert(0);
    H.insert(h);
    MH.insert(h);
    MW.insert(w);
    while (n--)
    {
        char x;
        int a, b, c;
        cin >> x >> c;
        if (x != 'V')
            swap(W, H), swap(MW, MH);
        W.insert(c);
        a = *(--W.find(c));
        b = *(++W.find(c));
        MW.erase(MW.find(b - a));
        MW.insert(b - c);
        MW.insert(c - a);
        if(x!='V')
            swap(W, H), swap(MW, MH);
        cout << ((ll)(*(--MW.end())))**(--MH.end()) << endl;
    }
}

D题: http://codeforces.com/contest/527/problem/D

D. Clique Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 xiwi (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!

The picture for the sample test.


分析:其实就是题目中的式子比较难以理解,但是你在纸上画画这种情况的时候,你会发现,当满足这种边的时候,是两条线段不相交的时候,每个点(xi,wi)可以表示成(xi-wi,xi+wi),如果两条线段不想交,也就是说xj的左点要大于xi的右点,那么他们之间必有一条边,而假设我们已经一部分的最大集合,那么新加的线段只要大于之前的最右点,就可以认为可以把这个点加进去,然后更新最右点,其实这也是这种性质的传递性,所以使得再排序之后,我们线性处理数据就能得出结果。

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<fstream>
using namespace std;
typedef long long ll;
const int M = 10;
int m[30][30];

int main() {
    int n;
    while(cin>>n)
    {
        vector<pair<ll,ll> >all;
        for(int i=0;i<n;i++)
        {
            ll x,w;
            cin>>x>>w;
            all.push_back(make_pair(x-w,x+w));

        }
        sort(all.begin(),all.end());
        ll last = -9999999999999;
        ll ans = 0;
        for(int i=0;i<n;i++)
        {
            pair<ll,ll> p = all[i];
            if(last<=p.first)
                last = p.second,ans++;
            else if(last>p.second)
                last = p.second;
        }
        cout<<ans<<endl;

    }
        
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值