A - KIDx's Pagination
Problem Description
One Day, KIDx developed a beautiful pagination for ACdream. Now, KIDx wants you to make another one.
The are n pages in total.
The current page is cur.
The max distance to current page you can display is d.
Here are some rules:
- The cur page button is disabled.
- If cur page is the first page, the button "<<" should be disabled.
- If cur page is the last page, the button ">>" should be disabled.
- If the button "x" is disabled, print "[x]"; else print "(x)".
- You should not display the "..." button when there is no hidden page.
You can assume that the button "..." is always disabled.
Input
Ease case contains three integers n, cur, d.
1 ≤ n ≤ 100.
1 ≤ cur ≤ n.
0 ≤ d ≤ n.
Output
Sample Input
10 5 2 10 1 2
Sample Output
Case #1: (<<)[...](3)(4)[5](6)(7)[...](>>) Case #2: [<<][1](2)(3)[...](>>)
Hint
Case 1:
Case 2:
这题最恶心了,非常简单了模拟,wa了好多发。。。主要是题目理解错了。。。只有一种情况下才会出现[<<],[>>],那就是cur=1或者n的时候,我把cur理解成每次当前位置了。。。QAQ。。。
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
#ifdef xxz
freopen("in.txt","r",stdin);
#endif // xxz
int n,cur,d;
int Case = 1;
while(cin>>n>>cur>>d)
{
cout<<"Case #"<<Case++<<": ";
if(cur - d > 1)
{
cout<<"(<<)[...]";
for(int i = cur-d; i < cur; i++)
{
cout<<"("<<i<<")";
}
cout<<"["<<cur<<"]";
if(cur + d < n)
{
for(int i = cur+1; i <= cur+d; i++)
{
cout<<"("<<i<<")";
}
cout<<"[...](>>)";
}
else {
for(int i = cur+1; i <= n; i++)
{
cout<<"("<<i<<")";
}
if(cur == n)
cout<<"[>>]";
else cout<<"(>>)";
}
}
else if(cur - d <= 1)
{
if(cur == 1)
cout<<"[<<]";
else cout<<"(<<)";
for(int i = 1; i < cur; i++)
{
cout<<"("<<i<<")";
}
cout<<"["<<cur<<"]";
if(cur + d < n)
{
for(int i = cur+1; i <= cur+d; i++)
{
cout<<"("<<i<<")";
}
cout<<"[...](>>)";
}
else {
for(int i = cur+1; i <= n; i++)
{
cout<<"("<<i<<")";
}
if(cur == n)
cout<<"[>>]";
else cout<<"(>>)";
}
}
cout<<endl;
}
return 0;
}
Problem Description
There are n swords of different weights Wi and n heros of power Pi.
Your task is to find out how many ways the heros can carry the swords so that each hero carries exactly one sword.
Here are some rules:
(1) Every sword is carried by one hero and a hero cannot carry a sword whose weight is larger than his power.
(2) Two ways will be considered different if at least one hero carries a different sword.
Input
The first line of the input gives the number of test cases T(1 ≤ T ≤ 50).
Each case starts with a line containing an integer n (1 ≤ n ≤ 105) denoting the number of heros and swords.
The next line contains n space separated distinct integers denoting the weight of swords.
The next line contains n space separated distinct integers denoting the power for the heros.
The weights and the powers lie in the range [1, 109].
Output
For each case, output one line containing "Case #x: " followed by the number of ways those heros can carry the swords.
This number can be very big. So print the result modulo 1000 000 007.
Sample Input
3 5 1 2 3 4 5 1 2 3 4 5 2 1 3 2 2 3 2 3 4 6 3 5
Sample Output
Case #1: 1 Case #2: 0 Case #3: 4
时间复杂度分析(大牛就不用看了~):
首先同时将两个数组power,weight从小到大排序,然后从扫描power数组,遇到power[i] < weight[j]就i++,否则j++,那么此时j-i就是该power可以拿起weight种类的个数,然后因为power是从大到小排列的,所以不需要回溯(如果不明白的可以仔细想想),所以每次j不需要初始化为0,所以总共扫描n+m次,故时间复杂度是o(n+m)约等于o(n),所以咱就可以ac了~~
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long LL;
const int Mod = 1000000000+7;
const int Max = 100000+5;
int weight[Max],power[Max];
int main()
{
int T;
scanf("%d",&T);
for(int k = 1; k <= T; k++)
{
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
scanf("%d",&weight[i]);
}
for(int i = 0; i < n; i++)
{
scanf("%d",&power[i]);
}
sort(power,power+n);
sort(weight,weight+n);
LL ans = 1;
for (int i = 0, j = 0; i < n; i++)
{
for ( ; j < n; j++)
if (weight[j] > power[i])
break;
ans = ans * (j-i) % Mod;
if (ans == 0) break;
}
cout << "Case #" << k << ": " << ans << endl;
}
return 0;
}
G - Integer in C++
Problem Description
KIDx: I like Java much more than C++, because I can use BigInteger in Java. :)
However, KIDx has to use C++ language to do a project...
KIDx can only use three integer types in C++:
1) short occupies 2 bytes and allows you to store numbers from -32768 to 32767
2) int occupies 4 bytes and allows you to store numbers from -2147483648 to 2147483647
3) long long occupies 8 bytes and allows you to store numbers from -9223372036854775808 to 9223372036854775807
For all the types given above the boundary values are included in the value range.
From this list, KIDx wants you to choose the smallest type that can store a positive integer n.
Input
There are multiple cases.
Each case contains a positive integer n.
It consists of at least one digit and at most 30 digits.
In addition, it doesn't contain any leading zeros.
Output
For each line, print the first type from the list "short, int, long long", that can store the natural number n.
If no one can store the number, just print "It is too big!".
Sample Input
102 50000
Sample Output
short int
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
using namespace std;
string str[3][2] ={"-32768" ,"32767",
"-2147483648","2147483647",
"-9223372036854775808","9223372036854775807"
};
int main()
{
string n;
while(cin>>n)
{
for(int i = 0; i < 3; i++)
{
if(n[0] == '-')
{
if(n.length() < str[i][0].length())
{
if(i == 0) t = "short";
else i == 1 ? t = "int" : t = "long long";
break;
}
else if(n.length() == str[i][0].length())
{
if(n <= str[i][0])
{
if(i == 0) t = "short";
else i == 1 ? t = "int" : t = "long long";
break;
}
else if(i == 2) t = "It is too big!";
}
else
{
if(i == 2) t = "It is too big!";
}
}
else {
if(n.length() < str[i][1].length())
{
if(i == 0) t = "short";
else i == 1 ? t = "int" : t = "long long";
break;
}
else if(n.length() == str[i][1].length())
{
if(n <= str[i][1])
{
if(i == 0) t = "short";
else i == 1 ? t = "int" : t = "long long";
break;
}
else if(i == 2) t = "It is too big!";
}
else
{
if(i == 2) t = "It is too big!";
}
}
}
cout<<t<<endl;
}
return 0;
}
I - Integration of Polynomial
Problem Description
Suppose there are a polynomial which has n nonzero terms, please print the integration polynomial of the given polynomial.
The polynomial will be given in the following way, and you should print the result in the same way:
k[1] e[1] k[2] e[2] ... k[n] e[n]
where k[i] and e[i] respectively represent the coefficients and exponents of nonzero terms, and satisfies e[1] < e[2] < ... < e[n].
Note:
- Suppose that the constant term of the integration polynomial is 0.
- If one coefficient of the integration polynomial is an integer, print it directly.
- If one coefficient of the integration polynomial is not an integer, please print it by using fraction a/b which satisfies that a is coprime to b.
Input
There are multiple cases.
For each case, the first line contains one integer n, representing the number of nonzero terms.
The second line contains 2*n integers, representing k[1], e[1], k[2], e[2], ..., k[n], e[n]。
1 ≤ n ≤ 1000
-1000 ≤ k[i] ≤ 1000, k[i] != 0, 1 ≤ i ≤ n
0 ≤ e[i] ≤ 1000, 1 ≤ i ≤ n
Output
Print the integration polynomial in one line with the same format as the input.
Notice that no extra space is allowed at the end of each line.
Sample Input
3 1 0 3 2 2 4
Sample Output
1 1 1 3 2/5 5
Hint
f(x) = 1 + 3x2 + 2x4
After integrating we get: ∫f(x)dx = x + x3 + (2/5)x5
求积分方法(大牛跳过~):利用gcd求系数,比如9x^5,那么系数应该是9/6,但是你不能直接除啊!如果它不整出的话,就逗比了。。题目要求如果不能整出用a/b约分后的形式表示,那么怎么约分呢?当然除gcd 就可以了~~
#include <iostream>
#include <cstdio>
using namespace std;
int gcd(int n ,int m)
{
return m == 0 ? n:gcd(m,n%m);
}
int main()
{
#ifdef xxz
freopen("in","r",stdin);
#endif // xxz
int n;
while(cin>>n)
{
int k,e;
for(int i = 1; i < n; i++)
{
cin>>k>>e;
if(k % (e+1) == 0) cout<<k/(e+1)<<" "<<e+1<<" ";
else {
int g = gcd(k,e+1);
if(g > 0) cout<<k/g<<"/"<<(e+1)/g<<" "<<e+1<<" ";
else cout<<-k/g<<"/"<<-(e+1)/g<<" "<<e+1<<" ";//主要如果最大公约数小于0的话特殊处理一下
}
}
cin>>k>>e;
if(k % (e+1) == 0) cout<<k/(e+1)<<" "<<e+1;
else {
int g = gcd(k,e+1);
if(g > 0) cout<<k/g<<"/"<<(e+1)/g<<" "<<e+1;
else cout<<-k/g<<"/"<<-(e+1)/g<<" "<<e+1;
}
cout<<endl;
}
return 0;
}
J - Disappeared Block
Problem Description
There are n columns of blocks on the ground, the ith columns has hi blocks.
One day, we observed a curious phenomenon that the bottom blocks will disappear in one second.
We need to predict what will happen in the future.
You job is to tell us that how many parts the blocks will be separated into at the ith second.
Input
The input contains several test cases.
The first line contains an integer T(T ≤ 15) denoting the number of test cases. Then T test cases follow.
Each of them begins with a line containing two numbers n and m(1 ≤ n, m ≤ 106), n is the number of columns and m is the number of seconds which we wants to query.
The next line contains n integers h1,h2,..., hn where 1 ≤ hi ≤ 109 is the height of column i.
The third line of a single test case contains m numbers sj(0 ≤ s1 < s2 < ... < sm-1 < sm ≤ 109).
Output
Sample Input
2 4 3 3 2 6 4 0 1 2 5 5 1 3 5 1 3 0 1 3 4 10
Sample Output
Case #1: 1 1 2 Case #2: 1 2 1 1 0
具体详解:
首先我们将记录高度的数组h,进行由大到小排序,时间的话题目已经由小到大排序好了,那么我们开一个标记数组flag,我们时间从大到小算,此时我们可以把这个题目想像成俄罗斯方块游戏,一开始时候n块区域都没有方块,2s时候来了一个方块,那么现在总共有1个区域,2s的时候又来了另外一个方块,如果这个方块落在已经有的方块旁边那么区域不变,如果这个方块的左右两边都没有方块,那么区域加1,如果这个方块拆在两个方块中间,那么原来的2个区域合成了一个区域,那么区域就要-1了,以此倒者推,那么就不用像正者推还需要回溯,时间复杂度瞬间降到o(n+m)~~
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 1000000+5;
int s[MAX],flag[MAX],ans[MAX];
struct H
{
int i,val;
}h[MAX];
bool cmp(H a, H b)
{
return a.val > b.val;
}
int main()
{
#ifdef xxz
freopen("out","r",stdin);
#endif // xxz
int t;
cin>>t;
for(int u = 1; u <= t; u++)
{
int n,m;
cin>>n>>m;
for(int i = 0; i < n ; i++)
{
cin>>h[i].val;
h[i].i = i+1;
}
sort(h,h+n,cmp);
for(int i = 0; i < m; i++)
{
cin>>s[i];
}
memset(flag,0,sizeof(flag));
int res = 0;
for(int i = 0 , j = m-1; j >= 0; j --)
{
for(; i < n; i++)
{
if(h[i].val <= s[j]) break;//当然如果不满足的话跳出
int id = h[i].i;
flag[id] = 1;
if(!flag[id -1] && !flag[id+1]) ++res;//如果两边都没有方块填充的话,那么区域+1
else if(flag[id-1] && flag[id+1]) --res;//如果两边都有方块的话,你插在中间不就区域要-1了~~
}
ans[j] = res;
}
cout<<"Case #"<<u<<": ";
for(int i = 0; i < m; i++)
{
if(i > 0) cout<<" ";
cout<<ans[i];
}
cout<<endl;
}
return 0;
}
至于其它几道题目等我慢慢更新把~~~太弱了我。。。如果你也是弱菜,我们一起加油吧~~~希望有一天通过努力也能够达到kuangbin大神的高度~~~~加油!!!!acdream我们一起成长~~~~