现在的位置: 首页 > 综合 > 正文

Codeforces Round #197(div 2)

2018年12月25日 ⁄ 综合 ⁄ 共 2882字 ⁄ 字号 评论关闭
A Helpful Maths  x2427
B Xenia and Ringroad  x2150
C Xenia and Weights  x673
D Xenia and Bit Operations  x889
E Three Swaps  x31

A、B: 水题。


C:

我写错了。当时做的时候,我用循环写的挂在第34组测试数据上。

后来看了别人的代码才知道是个深搜啊。

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;

int ans[1100];
int a[11];
int m, n;
int f;

void dfs(int sub, int p, int k)
{
    int i;
    if(k>m) {
        cout<<"YES"<<endl;
        for(i=1; i<m; i++)
            cout<<ans[i]<<" ";
        cout<<ans[i]<<endl;
        f = 1;
        return ;
    }
    for(i=0; i<n; i++)
        if( (a[i]!=p)&&(a[i]>sub)) {
            ans[k] = a[i];
            dfs(a[i]-sub, a[i], k+1);
            if(f) return ;
        }
}

int main()
{
    string s;
    cin>>s;
    n = 0;
    for(int i=0; i<s.size(); i++)
        if(s[i]=='1') a[n++] = i+1;
    cin>>m;
    f = 0;
    dfs(0, 0, 1);
    if(!f)  cout<<"NO"<<endl;
    return 0;
}


D:

The problem could be solved by using a typical data structure (segment tree).

The leafs of the segment tree will store the values of ai.
At the vertices, the distance from which to the leafs is 1, we will store OR of the numbers from the leafs, which are the sons of this node in the segment tree. Similarly, vertices, the distance from which to the leafs is 2, we will store Xor of the numbers
stored in their immediate sons. And so on. Then, the root of the tree will contain the required value v.

There is no need to rebuild all the tree to perform an update operation. To do update, we should find a path from the root to the corresponding leaf and recalculate the values only at the tree vertices that are lying on that path. If everything is done correctly,
then each update query will be executed in O(n) time. Also we need O(2n) memory.

E:

We will call the command l, r a reverse, also we will call the row of horses an array. Suddenly, right?

The problem can be solved with clever bruteforcing all possible ways to reverse an array. To begin with, assume that the reverse with l = r is ok.
Our solution can find an answer with such kind of reverses. It is clear that this thing doesn't affect the solution. Because such reverses can simply be erased from the answer.

The key idea: reverses split an array into no more than seven segments of the original array. In other words, imagine that the array elements was originally glued together, and each reverse cuts a segment from the array. Then the array would be cut into not
more than 7 pieces.

Now you can come up with the wrong solution to the problem, and then come up with optimization that turns it into right. So, bruteforce all ways to cut array into 7 or less pieces. Then bruteforce reverse operations, but each reverse operation should contain
only whole pieces. It is clear that this solution is correct, One thing it does not fit the TL.

How to improve it? Note that the previous solution requires the exact partition of the array only at the very end of the bruteforce. It needed to check whether it is possible to get the given array a.
So, let's assume that the array was originally somehow divided into 7 parts (we don't know the exact partition), the parts can be empty. Now try to bruteforce reverses as in naive solution. One thing, in the very end of bruteforce try to find such a partition
of the array to get (with fixed reverses) the given array a.

The search for such a partition can be done greedily (the reader has an opportunity to come up with it himself). Author's solution does this in time proportional to the number of parts, that is, 7 operations. However, this can be done for O(n) —
this should fit in TL, if you write bruteforce carefully.

【上篇】
【下篇】

抱歉!评论已关闭.