下沙友好群暑假训练二

转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

 

A - Palindromes

Description

Friday is Polycarpus' favourite day of the week. Not because it is followed by the weekend, but because the lessons on Friday are 2 IT lessons, 2 math lessons and 2 literature lessons. Of course, Polycarpus has prepared to all of them, unlike his buddy Innocentius. Innocentius spent all evening playing his favourite game Fur2 and didn't have enough time to do the literature task. As Innocentius didn't want to get an F, he decided to do the task and read the book called "Storm and Calm" during the IT and Math lessons (he never used to have problems with these subjects). When the IT teacher Mr. Watkins saw this, he decided to give Innocentius another task so that the boy concentrated more on the lesson and less — on the staff that has nothing to do with IT.

Mr. Watkins said that a palindrome is a string that can be read the same way in either direction, from the left to the right and from the right to the left. A concatenation of strings ab is a string ab that results from consecutive adding of string b to string a. Of course, Innocentius knew it all but the task was much harder than he could have imagined. Mr. Watkins asked change in the "Storm and Calm" the minimum number of characters so that the text of the book would also be a concatenation of no more than k palindromes. Innocentius can't complete the task and therefore asks you to help him.

Input

The first input line contains a non-empty string s which is the text of "Storm and Calm" (without spaces). The length of the string s does not exceed 500 characters. String s consists of uppercase and lowercase Latin letters. The second line contains a single number k (1 ≤ k ≤ |s|, where |s| represents the length of the string s).

Output

Print on the first line the minimum number of changes that Innocentius will have to make. Print on the second line the string consisting of no more than k palindromes. Each palindrome should be non-empty and consist of uppercase and lowercase Latin letters. Use the character "+" (ASCII-code 43) to separate consecutive palindromes. If there exist several solutions, print any of them.

The letters' case does matter, that is an uppercase letter is not considered equivalent to the corresponding lowercase letter.

Sample Input

Input
abacaba
1
Output
0
abacaba
Input
abdcaba
2
Output
1
abdcdba
Input
abdcaba
5
Output
0
a+b+d+c+aba
Input
abacababababbcbabcd
3
Output
1
abacaba+babab+bcbabcb

水dp

cost[i][j]表示区间[i,j]内变成回文的最小代价

dp[i][j]表示前i个内有j个回文的代价

则递推式易得为dp[i][j]=dp[l][j-1]+cost[l+1][i];

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 //#pragma comment(linker, "/STACK:102400000,102400000")
 6 #include <iostream>
 7 #include <sstream>
 8 #include <ios>
 9 #include <iomanip>
10 #include <functional>
11 #include <algorithm>
12 #include <vector>
13 #include <string>
14 #include <list>
15 #include <queue>
16 #include <deque>
17 #include <stack>
18 #include <set>
19 #include <map>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cmath>
23 #include <cstring>
24 #include <climits>
25 #include <cctype>
26 using namespace std;
27 #define XINF INT_MAX
28 #define INF 0x3FFFFFFF
29 #define MP(X,Y) make_pair(X,Y)
30 #define PB(X) push_back(X)
31 #define REP(X,N) for(int X=0;X<N;X++)
32 #define REP2(X,L,R) for(int X=L;X<=R;X++)
33 #define DEP(X,R,L) for(int X=R;X>=L;X--)
34 #define CLR(A,X) memset(A,X,sizeof(A))
35 #define IT iterator
36 typedef long long ll;
37 typedef pair<int,int> PII;
38 typedef vector<PII> VII;
39 typedef vector<int> VI;
40 int cost[510][510];
41 int dp[510][510];
42 int pre[510][510];
43 int gao[510];
44 int main()
45 {
46     ios::sync_with_stdio(false);
47     string str;
48     int k;
49     cin>>str;
50     cin>>k;
51     int len = str.length();
52     for(int i=0;i<len;i++){
53         for(int j=i;j<len;j++){
54             for(int l=i,r=j;l<r;l++,r--){
55                 if(str[l]!=str[r])cost[i][j]++;
56             }
57         }
58     }
59     for(int i=0;i<len;i++)dp[i][1]=cost[0][i];
60     for(int i = 1;i<len;i++){
61         for(int j = 2;j<=k;j++){
62             dp[i][j]=INF;
63             for(int l=0;l<i;l++){
64                 if(dp[i][j]>dp[l][j-1]+cost[l+1][i]){
65                     dp[i][j]=dp[l][j-1]+cost[l+1][i];
66                     pre[i][j]=l;
67                 }
68             }
69         }
70     }
71     int minn=INF;
72     int mink=0;
73     for(int i=1;i<=k;i++){
74         if(dp[len-1][i]<minn){
75             minn = dp[len-1][i];
76             mink = i;
77         }
78     }
79     gao[mink]=len-1;
80     for(int i=mink-1;i>0;i--){
81         gao[i] = pre[gao[i+1]][i+1];
82     }
83     gao[0]=-1;
84     cout<<minn<<endl;
85     for(int i =1;i<=mink;i++){
86         for(int l=gao[i-1]+1,r=gao[i];l<r;l++,r--){
87             if(str[l]!=str[r])str[l]=str[r];
88         }
89         for(int l=gao[i-1]+1,r=gao[i];l<=r;l++)cout<<str[l];
90         if(i<mink)cout<<"+";
91     }
92     cout<<endl;
93 
94     return 0;
95 }
代码君

B - Last Chance

Description

Having read half of the book called "Storm and Calm" on the IT lesson, Innocentius was absolutely determined to finish the book on the maths lessons. All was fine until the math teacher Ms. Watkins saw Innocentius reading fiction books instead of solving equations of the fifth degree. As during the last maths class Innocentius suggested the algorithm of solving equations of the fifth degree in the general case, Ms. Watkins had no other choice but to give him a new task.

The teacher asked to write consecutively (without spaces) all words from the "Storm and Calm" in one long string s. She thought that a string is good if the number of vowels in the string is no more than twice more than the number of consonants. That is, the string with vvowels and c consonants is good if and only if v ≤ 2c.

The task Innocentius had to solve turned out to be rather simple: he should find the number of the longest good substrings of the string s.

Input

The only input line contains a non-empty string s consisting of no more than 2·105 uppercase and lowercase Latin letters. We shall regard letters "a", "e", "i", "o", "u" and their uppercase variants as vowels.

Output

Print on a single line two numbers without a space: the maximum length of a good substring and the number of good substrings with this length. If no good substring exists, print "No solution" without the quotes.

Two substrings are considered different if their positions of occurrence are different. So if some string occurs more than once, then it should be counted more than once.

Sample Input

Input
Abo
Output
3 1
Input
OEIS
Output
3 1
Input
auBAAbeelii
Output
9 3
Input
AaaBRAaaCAaaDAaaBRAaa
Output
18 4
Input
EA
Output
No solution

Hint

In the first sample there is only one longest good substring: "Abo" itself. The other good substrings are "b", "Ab", "bo", but these substrings have shorter length.

In the second sample there is only one longest good substring: "EIS". The other good substrings are: "S", "IS".

二分、线段树都可以,想清楚就行

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 //#pragma comment(linker, "/STACK:102400000,102400000")
 6 #include <iostream>
 7 #include <sstream>
 8 #include <ios>
 9 #include <iomanip>
10 #include <functional>
11 #include <algorithm>
12 #include <vector>
13 #include <string>
14 #include <list>
15 #include <queue>
16 #include <deque>
17 #include <stack>
18 #include <set>
19 #include <map>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cmath>
23 #include <cstring>
24 #include <climits>
25 #include <cctype>
26 using namespace std;
27 #define XINF INT_MAX
28 #define INF 0x3FFFFFFF
29 #define MP(X,Y) make_pair(X,Y)
30 #define PB(X) push_back(X)
31 #define REP(X,N) for(int X=0;X<N;X++)
32 #define REP2(X,L,R) for(int X=L;X<=R;X++)
33 #define DEP(X,R,L) for(int X=R;X>=L;X--)
34 #define CLR(A,X) memset(A,X,sizeof(A))
35 #define IT iterator
36 typedef long long ll;
37 typedef pair<int,int> PII;
38 typedef vector<PII> VII;
39 typedef vector<int> VI;
40 bool check(char c){
41     if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')return 1;
42     return 0;
43 }
44 int a[200010];
45 int b[200010];
46 int main()
47 {
48     ios::sync_with_stdio(false);
49     string str;
50     cin>>str;
51     int len = str.length();
52     for(int i=0;i<len;i++){
53         if(str[i]>='A'&&str[i]<='Z')str[i]=str[i]-'A'+'a';
54     }
55     a[0]=0;
56     for(int i=0;i<len;i++){
57         a[i+1]=a[i];
58         if(check(str[i]))a[i+1]++;
59         else a[i+1]-=2;
60     }
61     b[len+1]=INF;
62     for(int i=len;i>=0;i--)b[i]=min(b[i+1],a[i]);
63     int ans = 0;
64     int sum = 0;
65     for(int i=0;i<len;i++){
66         int r=len+1,l=i;
67         while(l+1<r){
68             int mid = (l+r)>>1;
69             if(b[mid]<=a[i]){
70                 l=mid;
71             }else{
72                 r=mid;
73             }
74         }
75         if(ans<l-i){
76             ans=l-i;
77             sum=1;
78         }else if(ans==l-i){
79             sum++;
80         }
81     }
82     if(!ans)cout<<"No solution"<<endl;
83     else cout<<ans<<" "<<sum<<endl;
84 
85 
86 
87     return 0;
88 }
代码君

C - Aggressive cows

Description

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).

His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?

Input

t – the number of test cases, then t test cases follows. 
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains an integer stall location, xi

Output

For each test case output one integer: the largest minimum distance.

Example

Input:

1
5 3
1
2
8
4
9

Output:

3

Output details:

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, 
resulting in a minimum distance of 3.

 

直接二分答案,贪心判断。

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 //#pragma comment(linker, "/STACK:102400000,102400000")
 6 #include <iostream>
 7 #include <sstream>
 8 #include <ios>
 9 #include <iomanip>
10 #include <functional>
11 #include <algorithm>
12 #include <vector>
13 #include <string>
14 #include <list>
15 #include <queue>
16 #include <deque>
17 #include <stack>
18 #include <set>
19 #include <map>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cmath>
23 #include <cstring>
24 #include <climits>
25 #include <cctype>
26 using namespace std;
27 #define XINF INT_MAX
28 #define INF 0x3FFFFFFF
29 #define MP(X,Y) make_pair(X,Y)
30 #define PB(X) push_back(X)
31 #define REP(X,N) for(int X=0;X<N;X++)
32 #define REP2(X,L,R) for(int X=L;X<=R;X++)
33 #define DEP(X,R,L) for(int X=R;X>=L;X--)
34 #define CLR(A,X) memset(A,X,sizeof(A))
35 #define IT iterator
36 typedef long long ll;
37 typedef pair<int,int> PII;
38 typedef vector<PII> VII;
39 typedef vector<int> VI;
40 const int maxn=100010;
41 int a[maxn];
42 int main()
43 {
44     ios::sync_with_stdio(false);
45     int n,c;
46     int t;
47     scanf("%d",&t);
48     while(t--)
49     {
50         scanf("%d%d",&n,&c);
51         for(int i=0;i<n;i++)
52         {
53             scanf("%d",&a[i]);
54         }
55         sort(a,a+n);
56         int l=0,r=1e9;
57         int ans=0;
58         while(l<=r)
59         {
60             int last=0;
61             int cnt=1;
62             int mid=(l+r)/2;
63             for(int i=0;i<n;i++)
64             {
65                 if(a[last]+mid<=a[i])
66                 {
67                     cnt++;
68                     last=i;
69                 }
70             }
71             if(cnt>=c)
72             {
73                 ans=max(ans,mid);
74                 l=mid+1;
75             }
76             else
77             {
78                 r=mid-1;
79             }
80         }
81         printf("%d\n",ans);
82     }
83 
84 
85     return 0;
86 }
代码君

D - Mixtures

Description

Harry Potter has n mixtures in front of him, arranged in a row. Each mixture has one of 100 different colors (colors have numbers from 0 to 99).

He wants to mix all these mixtures together. At each step, he is going to take two mixtures that stand next to each other and mix them together, and put the resulting mixture in their place.

When mixing two mixtures of colors a and b, the resulting mixture will have the color (a+b) mod 100.

Also, there will be some smoke in the process. The amount of smoke generated when mixing two mixtures of colors a and b is a*b.

Find out what is the minimum amount of smoke that Harry can get when mixing all the mixtures together.

Input

There will be a number of test cases in the input.

The first line of each test case will contain n, the number of mixtures, 1 <= n <= 100.

The second line will contain n integers between 0 and 99 - the initial colors of the mixtures.

Output

For each test case, output the minimum amount of smoke.

Example

Input:
2
18 19
3
40 60 20

Output:
342
2400

In the second test case, there are two possibilities:

  • first mix 40 and 60 (smoke: 2400), getting 0, then mix 0 and 20 (smoke: 0); total amount of smoke is 2400
  • first mix 60 and 20 (smoke: 1200), getting 80, then mix 40 and 80 (smoke: 3200); total amount of smoke is 4400

The first scenario is a much better way to proceed.

 

区间dp

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 //#pragma comment(linker, "/STACK:102400000,102400000")
 6 #include <iostream>
 7 #include <sstream>
 8 #include <ios>
 9 #include <iomanip>
10 #include <functional>
11 #include <algorithm>
12 #include <vector>
13 #include <string>
14 #include <list>
15 #include <queue>
16 #include <deque>
17 #include <stack>
18 #include <set>
19 #include <map>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cmath>
23 #include <cstring>
24 #include <climits>
25 #include <cctype>
26 using namespace std;
27 #define XINF INT_MAX
28 #define INF 0x3FFFFFFF
29 #define MP(X,Y) make_pair(X,Y)
30 #define PB(X) push_back(X)
31 #define REP(X,N) for(int X=0;X<N;X++)
32 #define REP2(X,L,R) for(int X=L;X<=R;X++)
33 #define DEP(X,R,L) for(int X=R;X>=L;X--)
34 #define CLR(A,X) memset(A,X,sizeof(A))
35 #define IT iterator
36 typedef long long ll;
37 typedef pair<int,int> PII;
38 typedef vector<PII> VII;
39 typedef vector<int> VI;
40 int a[110];
41 int b[110][110];
42 int ans[110][110];
43 int main()
44 {
45     ios::sync_with_stdio(false);
46     int n;
47     while(cin>>n){
48         for(int i=0;i<n;i++)cin>>a[i];
49         for(int i=0;i<n;i++){
50             b[1][i]=a[i];
51             ans[1][i]=0;
52         }
53         for(int i=0;i<n-1;i++){
54             b[2][i]=(a[i]+a[i+1])%100;
55             ans[2][i]=a[i]*a[i+1];
56         }
57         for(int len=3;len<=n;len++){
58             for(int i=0;i<=n-len;i++){
59                 ans[len][i]=INF;
60                 for(int j=1;j<len;j++){
61                     int tmp = b[j][i]*b[len-j][i+j]+ans[j][i]+ans[len-j][i+j];
62                     if(tmp<ans[len][i]){
63                         ans[len][i]=tmp;
64                         b[len][i]=(b[j][i]+b[len-j][i+j])%100;
65                     }
66                 }
67             }
68         }
69         cout<<ans[n][0]<<endl;
70     }
71     return 0;
72 }
代码君

E - Postcards and photos

Description

Polycarpus has postcards and photos hung in a row on the wall. He decided to put them away to the closet and hang on the wall a famous painter's picture. Polycarpus does it like that: he goes from the left to the right and removes the objects consecutively. As Polycarpus doesn't want any mix-ups to happen, he will not carry in his hands objects of two different types. In other words, Polycarpus can't carry both postcards and photos simultaneously. Sometimes he goes to the closet and puts the objects there, thus leaving his hands free. Polycarpus must put all the postcards and photos to the closet. He cannot skip objects. What minimum number of times he should visit the closet if he cannot carry more than 5 items?

Input

The only line of the input data contains a non-empty string consisting of letters "С" and "P" whose length does not exceed 100characters. If the i-th character in the string is the letter "С", that means that the i-th object (the numbering goes from the left to the right) on Polycarpus' wall is a postcard. And if the i-th character is the letter "P", than the i-th object on the wall is a photo.

Output

Print the only number — the minimum number of times Polycarpus has to visit the closet.

Sample Input

Input
CPCPCPC
Output
7
Input
CCCCCCPPPPPP
Output
4
Input
CCCCCCPPCPPPPPPPPPP
Output
6
Input
CCCCCCCCCC
Output
2

Hint

In the first sample Polycarpus needs to take one item to the closet 7 times.

In the second sample Polycarpus can first take 3 postcards to the closet; then 3 more. He can take the 6 photos that are left in the similar way, going to the closet twice.

In the third sample Polycarpus can visit the closet twice, both times carrying 3 postcards. Then he can take there 2 photos at once, then one postcard and finally, he can carry the last 10 photos if he visits the closet twice.

In the fourth sample Polycarpus can visit the closet twice and take there all 10 postcards (5 items during each go).

直接从头往后搞

 1 //#####################
 2 //Author:fraud
 3 //Blog: http://www.cnblogs.com/fraud/
 4 //#####################
 5 //#pragma comment(linker, "/STACK:102400000,102400000")
 6 #include <iostream>
 7 #include <sstream>
 8 #include <ios>
 9 #include <iomanip>
10 #include <functional>
11 #include <algorithm>
12 #include <vector>
13 #include <string>
14 #include <list>
15 #include <queue>
16 #include <deque>
17 #include <stack>
18 #include <set>
19 #include <map>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cmath>
23 #include <cstring>
24 #include <climits>
25 #include <cctype>
26 using namespace std;
27 #define XINF INT_MAX
28 #define INF 0x3FFFFFFF
29 #define MP(X,Y) make_pair(X,Y)
30 #define PB(X) push_back(X)
31 #define REP(X,N) for(int X=0;X<N;X++)
32 #define REP2(X,L,R) for(int X=L;X<=R;X++)
33 #define DEP(X,R,L) for(int X=R;X>=L;X--)
34 #define CLR(A,X) memset(A,X,sizeof(A))
35 #define IT iterator
36 typedef long long ll;
37 typedef pair<int,int> PII;
38 typedef vector<PII> VII;
39 typedef vector<int> VI;
40 
41 int main()
42 {
43     ios::sync_with_stdio(false);
44     string str;
45     cin>>str;
46     int len = str.length();
47     int ans = 0;
48     for(int i=0;i<len;i++){
49         int num = 0;
50         for(int j=i+1;j<i+5&&j<len;j++){
51             if(str[j]!=str[i])break;
52             num++;
53         }
54         i+=num;
55         ans++;
56     }
57     cout<<ans<<endl;
58     return 0;
59 }
代码君

F - Permutation

Description

"Hey, it's homework time" — thought Polycarpus and of course he started with his favourite subject, IT. Polycarpus managed to solve all tasks but for the last one in 20 minutes. However, as he failed to solve the last task after some considerable time, the boy asked you to help him.

The sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.

You are given an arbitrary sequence a1, a2, ..., an containing n integers. Each integer is not less than 1 and not greater than 5000. Determine what minimum number of elements Polycarpus needs to change to get a permutation (he should not delete or add numbers). In a single change he can modify any single sequence element (i. e. replace it with another integer).

Input

The first line of the input data contains an integer n (1 ≤ n ≤ 5000) which represents how many numbers are in the sequence. The second line contains a sequence of integers ai (1 ≤ ai ≤ 5000, 1 ≤ i ≤ n).

Output

Print the only number — the minimum number of changes needed to get the permutation.

Sample Input

Input
3
3 1 2
Output
0
Input
2
2 2
Output
1
Input
5
5 3 3 3 1
Output
2

Hint

The first sample contains the permutation, which is why no replacements are required.

In the second sample it is enough to replace the first element with the number 1 and that will make the sequence the needed permutation.

In the third sample we can replace the second element with number 4 and the fourth element with number 2.

大水

//#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int num[100010];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    int a;
    for(int i=0;i<n;i++){
        cin>>a;
        num[a]=1;
    }
    int ans =0;
    for(int i=1;i<=n;i++){
        if(!num[i])ans++;
    }
    cout<<ans<<endl;

    return 0;
}
View Code

 

posted on 2015-07-12 21:52  xyiyy  阅读(377)  评论(0编辑  收藏  举报

导航