B. Hamming Distance Sum
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Genos needs your help. He was asked to solve the following programming problem by Saitama:

The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance between string "0011" and string "0110" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.

Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|.

Input

The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000).

The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000).

Both strings are guaranteed to consist of characters '0' and '1' only.

Output

Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.

Examples
Input
0100111
Output
3
Input
00110110
Output
2
Note

For the first sample case, there are four contiguous substrings of b of length |a|: "00", "01", "11", and "11". The distance between "01" and "00" is |0 - 0| + |1 - 0| = 1. The distance between "01" and "01" is |0 - 0| + |1 - 1| = 0. The distance between "01" and "11" is |0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string "11". The sum of these edit distances is 1 + 0 + 1 + 1 = 3.

The second sample case is described in the statement.

 1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<map>
7 #include<queue>
8 #include<stack>
9 #include<vector>
10 #include<bitset>
11 #include<set>
12 #define ll __int64
13 #define mod 100000000
14 using namespace std;
15 char a[200005];
16 char b[200005];
17 int sum1[200005];
18 int sum0[200005];
19 int main()
20 {
21     scanf("%s",a+1);
22     scanf("%s",b+1);
23     int len1=strlen(b+1);
24     int len2=strlen(a+1);
25     sum0[0]=0;
26     sum1[0]=0;
27    for(int i=1;i<=len1;i++)
28    {
29        sum1[i]=sum1[i-1];
30        sum0[i]=sum0[i-1];
31        if(b[i]=='1')
32         sum1[i]++;
33         else
34         sum0[i]++;
35    }
36    ll ans=0;
37     for(int i=1;i<=len2;i++){
38         if(a[i]=='0')
39            ans=ans+sum1[len1-(len2-i)]-sum1[i-1];
40         else
41         ans=ans+sum0[len1-(len2-i)]-sum0[i-1];
42     }
43     printf("%I64d\n",ans);
44     return 0;
45 }

C. Chain Reaction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.

Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.

The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.

Output

Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.

Examples
Input
41 93 16 17 4
Output
1
Input
71 12 13 14 15 16 17 1
Output
3
Note

For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.

For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.

 1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<map>
7 #include<queue>
8 #include<stack>
9 #include<vector>
10 #include<bitset>
11 #include<set>
12 #define ll __int64
13 #define mod 100000000
14 using namespace std;
15 int n;
16 int mp[1000006];
17 int dp[1000006];
18 int sum[1000006];
19 int a,b;
20 int main()
21 {
22     scanf("%d",&n);
23     for(int i=1;i<=n;i++){
24         scanf("%d %d",&a,&b);
25         mp[a]=b;
26         }
27     if(mp[0]==0){
28         sum[0]=0;
29         dp[0]=0;
30     }
31     else
32     {
33         sum[0]=1;
34         dp[0]=0;
35     }
36     int ans=1e9;;
37     for(int i=1;i<=1000001;i++){
38         sum[i]=sum[i-1];
39         if(mp[i]!=0)
40           sum[i]++;
41         }
42     for(int i=1;i<=1000000;i++)
43     {
44        if(mp[i]==0)
45            dp[i]=dp[i-1];
46        else
47        {
48            int now=max(0,i-mp[i]);
49            if(now==0)
50            dp[i]=sum[i-1];
51            else
52            dp[i]=dp[now-1]+(sum[i-1]-sum[now-1]);
53        }
54        ans=min(ans,dp[i]+sum[1000000]-sum[i]);
55     }
56      printf("%d\n",ans);
57     return 0;
58 }
D. Zuma
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.

The second line contains n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.

Output

Print a single integer — the minimum number of seconds needed to destroy the entire line.

Examples
Input
31 2 1
Output
1
Input
31 2 3
Output
3
Input
71 4 4 2 3 2 1
Output
2
Note

In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.

题意：消除回文 问最少几次消除全部

题解：简单区间dp

 1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<map>
7 #include<queue>
8 #include<stack>
9 #include<vector>
10 #include<bitset>
11 #include<set>
12 #define ll __int64
13 #define mod 100000000
14 using namespace std;
15 int n;
16 int a[505];
17 int dp[505][505];
18 int main()
19 {
20     scanf("%d",&n);
21     for(int i=1;i<=n;i++)
22         scanf("%d",&a[i]);
23     for(int i=1;i<=n;i++)
24         for(int j=i;j<=n;j++)
25         dp[i][j]=1e9;
26     for(int i=1;i<=n;i++)
27         dp[i][i]=1;
28     for(int i=n;i>=1;i--)
29     {
30         for(int j=i+1;j<=n;j++)
31         {
32             for(int k=i;k<=j;k++)
33                 dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
34              if(a[i]==a[j]){
35                 if(i+1==j)
36                     dp[i][j]=1;
37                 else
38                 dp[i][j]=min(dp[i][j],dp[i+1][j-1]);
39                 }
40         }
41     }
42     printf("%d\n",dp[1][n]);
43     return 0;
44 }