大提琴的聲音就像一條河,左岸是我無法忘卻的回憶,右岸是我值得緊握的璀璨年華,中間流淌的,是我年年歲歲淡淡的感傷。
Candy Sharing Game
A number of students sit in a circle facing their teacher in the center. Each student initially has an even number of pieces of candy. When the teacher blows a whistle, each student simultaneously gives half of his or her candy to the neighbor on the right. Any student, who ends up with an odd number of pieces of candy, is given another piece by the teacher. The game ends when all students have the same number of pieces of candy. Write a program which determines the number of times the teacher blows the whistle and the final number of pieces of candy for each student from the amount of candy each child starts with.
Input
For each game, the input begins with the number NN (1≤N≤100)(1≤N≤100) of students, followed by NN (even) candy(the number of candy no more than 1000) counts for the children counter-clockwise around the circle.
Output
For each game, output the number of rounds of the game followed by the amount of candy each child ends up with, both on one line.
Examples
input
Copy
4 2 4 6 8
output
Copy
4 8
input
Copy
2 2 2
output
Copy
0 2
Note
The game ends in a finite number of steps because:
1. The maximum candy count can never increase.
2. The minimum candy count can never decrease.
3. No one with more than the minimum amount will ever decrease to the minimum.
4. If the maximum and minimum candy count are not the same, at least one student with the minimum amount must have their count increase.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int s[1010],n,jg,i;
int main()
{
cin>>n;
for(int i=0; i<n; i++)
cin>>s[i];
while(1)
{
for(i=1; i<n; i++)
if(s[i-1]!=s[i])
break;
if(i>=n)
break;
jg++;
int tmp=s[n-1]/2;
for(i=n-1; i>0; i--)
{
s[i]/=2;
s[i]+=s[i-1]/2;
}
s[0]/=2;
s[0]+=tmp;
for(i=0; i<n; i++)
if(s[i]&1)
s[i]++;
}
cout<<jg<<' '<<s[0]<<endl;
return 0;
}
日期計算
已知 2011 年 11 月 11 日是星期五,問 YYYY 年 MM 月 DD 日是星期幾?注意考慮閏年的情況。尤其是逢百年不閏,逢 400 年閏的情況。
Input
輸入只有一行 YYYY MM DD
Output
輸出只有一行 W
Example
input
Copy
2011 11 11
output
Copy
5
Note
1599≤YYYY≤29991599≤YYYY≤2999
1≤MM≤121≤MM≤12
1≤DD≤311≤DD≤31,且確保測試樣例中 YYYY 年 MM 月 DD 日是一個合理日期
1≤W≤71≤W≤7,分別代表週一到週日
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int rnpd(int year)
{
if(year%4==0&&year%100!=0)
return 1;
if(year%400==0)
return 1;
return 0;
}
int getyed(int year)
{
int sum=0;
int i;
if(year<=2011)
{
for(i=year; i<2011; i++)
{
if(rnpd(i))
sum+=366;
else
sum+=365;
}
}
if(year>2011)
{
for(i=2011; i<year; i++)
{
if(rnpd(i))
sum+=366;
else
sum+=365;
}
}
return sum;
}
int getmon(int year,int mon,int day)
{
int i;
int sum=0;
for(i=1; i<mon; i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
sum+=31;
if(i==4||i==6||i==9||i==11)
sum+=30;
if(i==2&&rnpd(year))
sum+=29;
if(i==2&&!rnpd(year))
sum+=28;
}
return sum+day-1;
}
int main()
{
int year,mon,day,sum;
cin>>year>>mon>>day;
sum=getyed(year);
if(year<2011)
{
sum=sum-getmon(year,mon,day);
cout<<(sum%7==6?7:abs(6-(sum%7)))<<endl;
}
else
{
sum=sum+getmon(year,mon,day);
cout<<((6+(sum%7))>7?((6+(sum%7))%7):(6+(sum%7)))<<endl;
}
return 0;
}
最大子段和
NN 個整數組成的序列 a[1],a[2],a[3],…,a[n]a[1],a[2],a[3],…,a[n],求該序列如 a[i]+a[i+1]+…+a[j]a[i]+a[i+1]+…+a[j] 的連續子段和的最大值。當所給的整數均爲負數時和爲 00。
例如:-2,11,-4,13,-5,-2,和最大的子段爲:11,-4,13。和爲 20。
Input
第 11 行:整數序列的長度 NN (2≤N≤50000)(2≤N≤50000)
第 2−N+12−N+1 行:NN 個整數 (−109≤A[i]≤109)(−109≤A[i]≤109)
Output
輸出最大子段和
Example
input
Copy
6 -2 11 -4 13 -5 -2
output
Copy
20
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
const ll MAXN = 200005;
ll dp[MAXN], a[MAXN], n, maxx = -100000;
int main()
{
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = 1; i <= n; i++)
{
dp[i] = max(a[i], dp[i - 1] + a[i]);
maxx = max(maxx, dp[i]);
}
printf("%lld\n", maxx);
return 0;
}
數塔取數
一個高度爲 NN 的由正整數組成的三角形,從上走到下,求經過的數字和的最大值。
每次只能走到下一層相鄰的數上,例如從第 33 層的 66 向下走,只能走到第 44 層的 22 或 99 上。
5
8 4
3 6 9
7 2 9 5
例子中的最優方案是:5 + 8 + 6 + 9 = 28
Input
第 1 行:NN,NN 爲數塔的高度。(2≤N≤500)(2≤N≤500)
第 2 - N + 1 行:每行包括 1 層數塔的數字,第 2 行 1 個數,第 3 行 2 個數......第 k+1 行 k 個數。數與數之間用空格分隔 (0≤A[i]≤105)(0≤A[i]≤105)。
Output
輸出最大值
Example
input
Copy
4 5 8 4 3 6 9 7 2 9 5
output
Copy
28
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int dp[510][510],n;
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j <= i; j++)
cin >> dp[i][j];
for (int i = n; i > 0; i--)
for (int j = 0; j < i; j++)
dp[i - 1][j] += max(dp[i][j], dp[i][j + 1]);
cout << dp[0][0] << endl;
return 0;
}
最長遞增子序列
給出長度爲 NN 的數組,找出這個數組的最長遞增子序列。(遞增子序列是指,子序列的元素是遞增的)
例如:5 1 6 8 2 4 5 10,最長遞增子序列是 1 2 4 5 10。
Input
第 1 行:1 個數 NN,NN 爲序列的長度 (2≤N≤50000)(2≤N≤50000)
第 2 - N + 1 行:每行 1 個數,對應序列的元素 (−109≤S[i]≤109)(−109≤S[i]≤109)
Output
輸出最長遞增子序列的長度。
Example
input
Copy
8 5 1 6 8 2 4 5 10
output
Copy
5
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int mod=1e9;
int mx[50010];
int main()
{
for (int i = 0; i <50010; i++)
mx[i] = mod + 1;
int n, x, y, jg = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> x;
y = lower_bound(mx, mx + n, x) - mx;
jg = max(y + 1, jg);
mx[y] = x;
}
cout << jg << endl;
return 0;
}
最長公共子序列
給出兩個字符串 A B,求 A 與 B 的最長公共子序列(子序列不要求是連續的)。
比如兩個串爲:
abcicba
abdkscab
ab 是兩個串的子序列,abc 也是,abca 也是,其中 abca 是這兩個字符串最長的子序列。
Input
第1行:字符串 A
第2行:字符串 B
(A,B 的長度 <= 1000)
Output
輸出最長的子序列的長度
Example
input
Copy
abcicba abdkscab
output
Copy
4
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
char S1[2010],S2[2010];
int dp[2010][2010];
int main()
{
int i, j;
scanf("%s%s", S1 + 1, S2 + 1);
int cd1 = strlen(S1 + 1);
int cd2 = strlen(S2 + 1);
for (i = 1; i <= cd1; i++)
{
for (j = 1; j <= cd2; j++)
{
if (S1[i] == S2[j])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
cout << dp[cd1][cd2] << endl;
return 0;
}
揹包問題
在 NN 件物品取出若干件放在容量爲 WW 的揹包裏,每件物品的體積爲 W1,W2……WnW1,W2……Wn(WiWi 爲整數),與之相對應的價值爲 P1,P2……PnP1,P2……Pn(PiPi 爲整數)。求揹包能夠容納的最大價值。
Input
第 1 行,2 個整數,NN 和 WW 中間用空格隔開。NN 爲物品的數量,WW 爲揹包的容量。 (1≤N≤100,1≤W≤10000)(1≤N≤100,1≤W≤10000)
第 2 - N + 1 行,每行 2 個整數,WiWi 和 PiPi,分別是物品的體積和物品的價值。 (1≤Wi,Pi≤10000)(1≤Wi,Pi≤10000)
Output
輸出可以容納的最大價值。
Example
input
Copy
3 6 2 5 3 8 4 9
output
Copy
14
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n, m;
int w[110], p[110], dp[10010];
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> w[i] >> p[i];
for (int i = 1; i <= n; i++)
for (int j = m; j >= w[i]; j--)
dp[j] = max(dp[j], dp[j - w[i]] + p[i]);
cout << dp[m] << endl;
return 0;
}
Monotonic Renumeration
You are given an array aa consisting of nn integers. Let's denote monotonic renumeration of array aa as an array bb consisting of nn integers such that all of the following conditions are met:
- b1=0b1=0;
- for every pair of indices ii and jj such that 1≤i,j≤n1≤i,j≤n, if ai=ajai=aj, then bi=bjbi=bj (note that if ai≠ajai≠aj, it is still possible that bi=bjbi=bj);
- for every index i∈[1,n−1]i∈[1,n−1] either bi=bi+1bi=bi+1 or bi+1=bi+1bi+1=bi+1.
For example, if a=[1,2,1,2,3]a=[1,2,1,2,3], then two possible monotonic renumerations of aa are b=[0,0,0,0,0]b=[0,0,0,0,0] and b=[0,0,0,0,1]b=[0,0,0,0,1].
Your task is to calculate the number of different monotonic renumerations of aa. The answer may be large, so print it modulo 998244353998244353.
Input
The first line contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of elements in aa.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
Output
Print one integer — the number of different monotonic renumerations of aa, taken modulo 998244353998244353.
Examples
input
Copy
5 1 2 1 2 3
output
Copy
2
input
Copy
2 100 1
output
Copy
2
input
Copy
4 1 3 3 7
output
Copy
4
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int mod=998244353;
int n;
ll yx[200010],jg = 1,xx;
map<ll,int> ma;
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>xx;
if(ma[xx] == 0)
ma[xx] = i;
else
{
yx[ma[xx] + 1] ++;
yx[i + 1] --;
}
}
for(int i=2; i<=n; i++)
{
yx[i] += yx[i-1];
if(yx[i] == 0)
jg = (jg * 2 + mod) % mod;
}
cout<<jg<<endl;
return 0;
}