Codeforces Round #277 (Div. 2) A~E题解

A. Calculating Function
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For a positive integer n let's define a function f:

f(n) =  - 1 + 2 - 3 + .. + ( - 1)nn

Your task is to calculate f(n) for a given integer n.

Input

The single line contains the positive integer n (1 ≤ n ≤ 1015).

Output

Print f(n) in a single line.

Sample test(s)
input
4
output
2
input
5
output
-3
Note

f(4) =  - 1 + 2 - 3 + 4 = 2

f(5) =  - 1 + 2 - 3 + 4 - 5 =  - 3


水题,找规律或推出公式,如果n是偶数答案是n/2,否则答案是-(n+1)/2

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#define mem(x) memset(x, 0, sizeof(x))
using namespace std;
const int N = 1010;
typedef __int64 ll;

int main(){
    ll n;
    cin >> n;
    if(n & 1) cout << -(n+1)/2<<endl;
    else cout << n/2 << endl;
    return 0;
}

B. OR in Matrix
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:

 where  is equal to 1 if some ai = 1, otherwise it is equal to 0.

Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 tom, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:

.

(Bij is OR of all elements in row i and column j of matrix A)

Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.

Input

The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively.

The next m lines each contain n integers separated by spaces describing rows of matrix B(each element of B is either 0 or 1).

Output

In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.

Sample test(s)
input
2 2
1 0
0 0
output
NO
input
2 3
1 1 1
1 1 1
output
YES
1 1 1
1 1 1
input
2 3
0 1 0
1 1 1
output
YES
0 0 0
0 1 0

给出一个矩阵B,问能否构造出矩阵A使得矩阵A 中第i行第j列的值取或后得到Bij的值

如果Bij的值为0,很显然矩阵A中第i行第j列的所有值均要为0,因此我们可以先根据Bij中为0的值构建一个矩阵A,其余值

设为1,再判断这个矩阵A是否满足题意

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#define mem(x) memset(x, 0, sizeof(x))
using namespace std;
const int N = 110;
typedef __int64 ll;

int mp[N][N], res[N][N];
int a[N], b[N];
int n, m;

int main(){
    while(cin >> n >> m){
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= m; ++j) cin >> mp[i][j], a[i] = b[j] = 1;
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= m; ++j){
                if(!mp[i][j]) a[i] = b[j] = 0;
            }
        }

        for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j){
            if(!(a[i] & b[j])) res[i][j] = 0;
            else res[i][j] = 1;
        }
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= m; ++j){
                if(res[i][j]) a[i] = b[j] = 1;
            }
        }
        //for(int i = 1; i <= n; ++i) cout << a[i] << " "; cout << endl;
        //for(int j = 1; j <= m; ++j) cout << b[j] << " "; cout << endl;
        int flag = 1;
        for(int i = 1; i <= n && flag; ++i){
            for(int j = 1; j <= m && flag; ++j){
                if(mp[i][j]){
                    if(!(a[i] | b[j])) flag = 0;
                }
            }
        }
        if(flag){
            puts("YES");
            for(int i = 1; i <= n; ++i){
                for(int j = 1; j <= m; ++j) cout << res[i][j] << " ";
                cout << endl;
            }
        }
        else puts("NO");
    }
    return 0;
}

C. Palindrome Transformation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.

There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).

When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.

Initially, the text cursor is at position p.

Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?

Input

The first line contains two space-separated integers n (1 ≤ n ≤ 105) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.

The next line contains n lowercase characters of Nam's string.

Output

Print the minimum number of presses needed to change string into a palindrome.

Sample test(s)
input
8 3
aeabcaez
output
6
Note

A string is a palindrome if it reads the same forward or reversed.

In the sample test, initial Nam's string is:  (cursor position is shown bold).

In optimal solution, Nam may do 6 following steps:

The result, , is now a palindrome.


题意:给一个长度为n的串和光标的位置p,有四种操作,1.向左移动光标,2.向右移动,步只能移动一位,可将串看做环,3.改变光标所指的字符,每步只能改为相邻字符,也是环形,Hint所示,求最少几步构造出回文串

解题思路:易知回文串是确定的,故只需对一半串进行操作,而光标也只用在考虑一侧的情况,先求出哪些位置需要修改,需要修改几步,再计算移动的步数,移动的最少步数为两端距当前光标点远一端的一倍+近端的二倍

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#define mem(x) memset(x, 0, sizeof(x))
using namespace std;
const int N = 100010;
typedef __int64 ll;

char str[N];
int n, pos;
int c[N];
int mdf[N];

int main(){
    cin >> n >> pos;
    cin >> str + 1;
    if(pos > (n + 1) / 2) pos = n - pos + 1;
    int num = 0, res = 0;
    for (int i = 1; i <= (n + 1) / 2; ++i){
        char t1 = str[i], t2 = str[n - i + 1];
        if (t1 > t2) swap(t1, t2);
        if (t1 != t2){
            mdf[num++] = i;
            res += min(t2 - t1, t1 - t2 + 26);
        }
    }
    //cout << "res = " << res << endl;
    if (res == 0){
        cout << 0 << endl;
        return 0;
    }
    int mov;
    int fir = mdf[0], las = mdf[num - 1];
    if (fir >= pos) mov = las - pos;
    else if (las <= pos) mov = pos - fir;
    else {
        int l1 = fabs(pos - fir), l2 = fabs(pos - las);
        if(l1 > l2) swap(l1, l2);
        mov = 2 * l1 + l2;
    }
    cout << res + mov << endl;
    return 0;
}

D. Valid Sets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree consisting of n nodes. Each node i has a value aiassociated with it.

We call a set S of tree nodes valid if following conditions are satisfied:

  1. S is non-empty.
  2. S is connected. In other words, if nodes u and v are in S, then all nodes lying on the simple path between u and v should also be presented in S.
  3. .

Your task is to count the number of valid sets. Since the result can be very large, you must print its remainder modulo 1000000007 (109 + 7).

Input

The first line contains two space-separated integers d (0 ≤ d ≤ 2000) and n (1 ≤ n ≤ 2000).

The second line contains n space-separated positive integers a1, a2, ..., an(1 ≤ ai ≤ 2000).

Then the next n - 1 line each contain pair of integers u and v (1 ≤ u, v ≤ n) denoting that there is an edge between u and v. It is guaranteed that these edges form a tree.

Output

Print the number of valid sets modulo 1000000007.

Sample test(s)
input
1 4
2 1 3 2
1 2
1 3
3 4
output
8
input
0 3
1 2 3
1 2
2 3
output
3
input
4 8
7 8 7 5 4 6 4 10
1 6
1 2
5 8
1 3
3 5
6 7
3 4
output
41
Note

In the first sample, there are exactly 8 valid sets: {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {3, 4}and {1, 3, 4}. Set {1, 2, 3, 4} is not valid, because the third condition isn't satisfied. Set {1, 4} satisfies the third condition, but conflicts with the second condition.


给出一棵树,节点有权值,问有多少 集合S,使得集合中的元素满足 :1.S非空。 2.S中的点构成一条路径。3.S中点的最大权值和最小权值差小于等于d

树形DP

先考虑一种特殊情况,如果d是无穷大,那么权值就没有用了,我们可以假设树根是节点1,做一遍DFS,记Fi表示以i为根节点的子树中集合S的个数,j是节点i的子节点,则,复杂度是O(N)

再对于一般的情况,我们需把计算包含节点i的集合S个数,并且对于集合中的其他元素j有ai ≤ aj ≤ ai + d ,此时便变成了上述的情况, 因此我们可以枚举所有的点按上述方法计算后累加即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
const int N = 2010;
const int mod = 1000000007;
typedef long long ll;

int d, n;
int val[N];
vector<int> G[N];
ll dp[N];

void dfs(int u, int fa, int rt){
    dp[u] = 1;
    for(int i = 0; i < G[u].size(); ++i){
        int v = G[u][i];
        if(v == fa) continue;
        if(val[v] >= val[rt] && val[v] <= val[rt] + d){
            if(val[v] == val[rt] && v > rt) continue;
            dfs(v, u, rt);
            dp[u] = dp[u] * (dp[v] + 1) % mod;
        }
    }
}

int main(){
    while(cin >> d >> n){
        for(int i = 1; i <= n; ++i) cin >> val[i];
        for(int i = 1; i <= n; ++i) G[i].clear();
        int u, v;
        for(int i = 1; i <= n - 1; ++i){
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        ll ans = 0;
        for(int i = 1; i <= n; ++i){
            memset(dp, 0, sizeof(dp));
            dfs(i, -1, i);
            ans = (ans + dp[i]) % mod;
        }
        cout << ans << endl;
    }
    return 0;
}

E. LIS of Sequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.

Nam created a sequence a consisting of n (1 ≤ n ≤ 105) elements a1, a2, ..., an (1 ≤ ai ≤ 105). A subsequence ai1, ai2, ..., aik where 1 ≤ i1 < i2 < ... < ik ≤ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences.

Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 ≤ i ≤ n), into three groups:

  1. group of all i such that ai belongs to no longest increasing subsequences.
  2. group of all i such that ai belongs to at least one but not every longest increasing subsequence.
  3. group of all i such that ai belongs to every longest increasing subsequence.

Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.

Input

The first line contains the single integer n (1 ≤ n ≤ 105) denoting the number of elements of sequence a.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105).

Output

Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index ibelongs to.

Sample test(s)
input
1
4
output
3
input
4
1 3 2 5
output
3223
input
4
1 5 2 3
output
3133
Note

In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.

In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}.

给出一个序列,有三种分类,第一类:不属于任何一个LIS。第二类:属于LIS但不属于所有的LIS。第三类:属于所有的LIS ,按上述标准将序列所有的数字分成三类并输出。

关于NlogN复杂度求LIS的算法可以参考点击打开链接

开始没有任何思路,看题解,首先求出两个序列F1,F2,和LIS的长度l

  • Let F1i be the length of LIS ending exactly at ai of sequence {a1, a2, ..., ai}.

  • Let F2i be the length of LIS beginning exactly at ai of sequence {ai, ai + 1, ..., an}.

  • l = length of LIS of {a1, a2, ..., an} = max{F1i} = max{F2j}.

     if F1i + F2i - 1 ≠ l than answer for i is 1, otherwise it is 2 or 3.

    Then, assume there are two elements i and j with answer different from 1 such that F1i = F1j. You can prove that in this case you can replace i-th element with j-th one in any LIS containing i-th element. Thus the answer for i is 2 (and for j it is 2 too, of course). It can also be shown that if F1i is unique among all F1 then the answer for i is 3.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#define mem(x) memset(x, 0, sizeof(x))
using namespace std;
const int N = 100010;
typedef __int64 ll;

int a[N], b[N], f1[N], f2[N];
int dp[N];
int ans[N];
int n;
struct Data{
    int id, val;
}d[N];

int fun(int x, int w){
    int l = 1, r = w, mid;
    while(l < r){
        mid = l + (r - l) / 2;
        if(dp[mid] <= b[x]) r = mid;
        else l = mid + 1;
    }
    return r;
}

bool cmp(Data x, Data y){
    if(x.val == y.val) return x.id < y.id;
    else return x.val < y.val;
}

int main(){
    while (cin >> n){
        for (int i = 1; i <= n; ++i) cin >> a[i], b[n - i + 1] = a[i];
        int len = 1;
        dp[1] = a[1];
        f1[1] = 1;
        for(int i = 2; i <= n; ++i){
            if(a[i] > dp[len]){
                dp[++len] = a[i];
                f1[i] = len;
            }
            else{
                int pos = lower_bound(dp, dp + len, a[i]) - dp;
                dp[pos] = a[i];
                f1[i] = pos;
            }
        }
        // cout << "len1 = " <<len << endl;
        // for(int i = 1; i <= n; ++i) cout << f1[i] << " "; cout << endl;
        dp[1] = b[1];
        f2[n] = 1;
        len = 1;
        for(int i = 2; i <= n; ++i){
            if(b[i] < dp[len]){
                dp[++len] = b[i];
                f2[n - i + 1] = len;
            }
            else{
                int pos = fun(i, len);
                dp[pos] = b[i];

                //cout << "len = " << len << endl;
                f2[n - i + 1] = pos;
            }
        }
        // cout << "len2 = " << len << endl;
      // for(int i = 1; i <= n; ++i) cout << f2[i] << " "; cout << endl;
        int t = 0;
        for(int i = 1; i <= n; ++i){
            if(f1[i] + f2[i] - 1 != len) ans[i] = 1;
            else{
                d[t].id = i;
                d[t++].val = f1[i];
            }
        }
        sort(d, d + t, cmp);
        if(t > 1) ans[d[0].id] = d[0].val == d[1].val ? 2 : 3;
        else if(t == 1) ans[d[0].id] = 3;
        for(int i = 1; i < t; ++i){
            if(d[i].val == d[i - 1].val) ans[d[i].id] = ans[d[i - 1].id] = 2;
            else ans[d[i].id] = 3;
        }
        for(int i = 1; i <= n; ++i) cout << ans[i];
        cout << endl;
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值