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




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章