传送门.
It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.
Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.
There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.
Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).
The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.
Input
The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).
Output
Output a single integer — the minimal possible number of minutes required to dry all clothes.
Sample Input
sample input #1
3
2 3 9
5
sample input #2
3
2 3 6
5
Sample Output
sample output #1
3
sample output #2
2
题意求烘干衣服最少用的时间。二分,mid表示最少时间,若a[i]大于mid,则需要烘干,每分钟烘干水量为k-1,比较烘干总时间与mid,确定二分范围。
#include
#include
#include
#define ll long long
using namespace std;
int n,k,maxx,a[10000000];
int binarysearch(ll mid)
{
ll i,t,sum=0;
for(int i=0; i<n; i++)
{
if(a[i]>mid)
{
t=ceil((a[i]-mid)*1.0/(k-1));
sum+=t;
}
}
if(sum<=mid)
return 1;
else
return 0;
}
int main()
{
scanf("%d",&n);
ll l,r,mid;
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
maxx=max(maxx,a[i]);
}
scanf("%d",&k);
if(k==1)
{
printf("%lld\n",maxx);
return 0;
}
l=0;
r=maxx;
while(l<r)
{
mid=(l+r)/2;
if(binarysearch(mid))
r=mid;
else
l=mid+1;
}
printf("%lld",r);
return 0;
}
传送门.
Master Mind KongMing gave Fei Zhang a secrete master plan stashed in a pocket. The plan instructs how to deploy soldiers on the four corners of the city wall. Unfortunately, when Fei opened the pocket he found there are only four numbers written in dots on a piece of sheet. The numbers form 2×2 matrix, but Fei didn’t know the correct direction to hold the sheet. What a pity!
Given two secrete master plans. The first one is the master’s original plan. The second one is the plan opened by Fei. As KongMing had many pockets to hand out, he might give Fei the wrong pocket. Determine if Fei receives the right pocket.
Input
The first line of the input gives the number of test cases, T(1≤T≤104). T test cases follow. Each test case contains 4 lines. Each line contains two integers ai0 and ai1 (1≤ai0,ai1≤100). The first two lines stands for the original plan, the 3rd and 4th line stands for the plan Fei opened.
Output
For each test case, output one line containing " Case #x: y", where x is the test case number
(starting from 1) and y is either “POSSIBLE” or “IMPOSSIBLE” (quotes for clarity).
Sample Input
4
1 2
3 4
1 2
3 4
1 2
3 4
3 1
4 2
1 2
3 4
3 2
4 1
1 2
3 4
4 3
2 1
Sample Output
Case #1: POSSIBLE
Case #2: POSSIBLE
Case #3: IMPOSSIBLE
Case #4: POSSIBLE
#include
int main(){
int t,cas=1;
scanf("%d",&t);
while(t--){
int a1,b1,c1,d1,a2,b2,c2,d2;
scanf("%d%d%d%d%d%d%d%d",&a1,&b1,&c1,&d1,&a2,&b2,&c2,&d2);
if(a2==a1&&b1==b2&&c1==c2){
printf("Case #%d: POSSIBLE\n",cas++);
continue;
}
if(b2==a1&&a2==c1&&d2==b1){
printf("Case #%d: POSSIBLE\n",cas++);
continue;
}
if(c2==a1&&d2==c1&&a2==b1){
printf("Case #%d: POSSIBLE\n",cas++);
continue;
}
if(d2==a1&&c2==b1&&b2==c1){
printf("Case #%d: POSSIBLE\n",cas++);
continue;
}
printf("Case #%d: IMPOSSIBLE\n",cas++);
}
return 0;
}
传送门
You are given an array a of length n.
You are also given a set of distinct positions p1,p2,…,pm, where 1≤piYour task is to determine if it is possible to sort the initial array in non-decreasing order (a1≤a2≤⋯≤an) using only allowed swaps.
For example, if a=[3,2,1] and p=[1,2], then we can first swap elements a[2] and a[3] (because position 2 is contained in the given set p). We get the array a=[3,1,2]. Then we swap a[1] and a[2] (position 1 is also contained in p). We get the array a=[1,3,2]. Finally, we swap a[2] and a[3] again and get the array a=[1,2,3], sorted in non-decreasing order.
You can see that if a=[4,1,2,3] and p=[3,2] then you cannot sort the array.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases.
Then t test cases follow. The first line of each test case contains two integers n and m (1≤m
Output
For each test case, print the answer — “YES” (without quotes) if you can sort the initial array in non-decreasing order (a1≤a2≤⋯≤an) using only allowed swaps. Otherwise, print “NO”.
Input
6
3 2
3 2 1
1 2
4 2
4 1 2 3
3 2
5 1
1 2 3 4 5
1
4 2
2 1 4 3
1 3
4 2
4 3 2 1
1 3
5 2
2 1 2 3 3
1 4
Output
YES
NO
YES
YES
NO
YES
冒泡排序,只有p[i]和p[i+1]可以交换顺序,如果a数组无法完成排序,则输出NO,否则输出YES。
#include
#include
#include
using namespace std;
int a[105],p[105],q[105];
int main()
{
int t,n,m,flag;
scanf("%d",&t);
while(t--)
{
memset(q,0,sizeof(q));
flag=0;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
for(int i=1; i<=m; i++)
{
scanf("%d",&p[i]);
q[p[i]]=1;
}
for(int j=1; j<n; j++)
{
for(int i=1; i<n; i++)
{
if(a[i]>a[i+1])
{
if(q[i])
{
swap(a[i],a[i+1]);
}
else
{
flag=1;
break;
}
}
}
if(flag)
break;
}
if(flag)
printf("NO\n");
else printf("YES\n");
}
return 0;
}
传送门
Nowadays we use content of PM2.5 to discribe the quality of air. The lower content of PM2.5 one city have, the better quality of air it have. So we sort the cities according to the content of PM2.5 in asending order.
Sometimes one city’s rank may raise, however the content of PM2.5 of this city may raise too. It means that the quality of this city is not promoted. So this way of sort is not rational. In order to make it reasonable, we come up with a new way to sort the cityes. We order this cities through the diffrence between twice measurement of content of PM2.5 (first measurement – second measurement) in descending order, if there are ties, order them by the second measurement in asending order , if also tie, order them according to the input order.
Input
Multi test cases (about 100), every case contains an integer n which represents there are n cities to be sorted in the first line.
Cities are numbered through 0 to n−1.
In the next n lines each line contains two integers which represent the first and second measurement of content of PM2.5
The ith line describes the information of city i−1
Please process to the end of file.
[Technical Specification]
all integers are in the range [1,100]
Output
For each case, output the cities’ id in one line according to their order.
Sample Input
2
100 1
1 2
3
100 50
3 4
1 2
Sample Output
0 1
0 2 1
#include
struct s
{
int first,second,order;
}city[105], tmp;
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
for(i=0;i<n;i++)
{
scanf("%d%d",&city[i].first, &city[i].second);
city[i].order=i;
}
for (i=0;i<n-1;i++)
for (j=0;j<n-1-i;j++)
{
if (city[j].first - city[j].second < city[j+1].first - city[j+1].second)
{
tmp = city[j];
city[j] = city[j+1];
city[j+1] = tmp;
}
else if (city[j].first - city[j].second == city[j+1].first - city[j+1].second)
{
if (city[j].second > city[j+1].second)
{
tmp = city[j];
city[j] = city[j+1];
city[j+1] = tmp;
}
}
}
for(i=0;i<n-1;i++) printf("%d ",city[i].order);
printf("%d\n",city[n-1].order);
}
return 0;
}
传送门
Two planets named Haha and Xixi in the universe and they were created with the universe beginning.
There is 73 days in Xixi a year and 137 days in Haha a year.
Now you know the days N after Big Bang, you need to answer whether it is the first day in a year about the two planets.
Input
There are several test cases(about 5 huge test cases).
For each test, we have a line with an only integer N(0≤N), the length of N is up to 10000000.
Output
For the i-th test case, output Case #i: , then output “YES” or “NO” for the answer.
Sample Input
10001
0
333
Sample Output
Case #1: YES
Case #2: YES
Case #3: NO
传送门
One unknown hacker wants to get the admin’s password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator’s office and stole a piece of paper with a list of n passwords — strings, consists of small Latin letters.
Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords a and b as follows:
two passwords a and b are equivalent if there is a letter, that exists in both a and b;
two passwords a and b are equivalent if there is a password c from the list, which is equivalent to both a and b.
If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.
For example, if the list contain passwords “a”, “b”, “ab”, “d”, then passwords “a”, “b”, “ab” are equivalent to each other, but the password “d” is not equivalent to any other password from list. In other words, if:
admin’s password is “b”, then you can access to system by using any of this passwords: “a”, “b”, “ab”;
admin’s password is “d”, then you can access to system by using only “d”.
Only one password from the list is the admin’s password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
Input
The first line contain integer n (1≤n≤2⋅105) — number of passwords in the list. Next n lines contains passwords from the list – non-empty strings si, with length at most 50 letters. Some of the passwords may be equal.
It is guaranteed that the total length of all passwords does not exceed 106 letters. All of them consist only of lowercase Latin letters.
Output
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
Examples
Input
4
a
b
ab
d
Output
2
Input
3
ab
bc
abc
Output
1
Input
codeforces
Output
1
Note
In the second example hacker need to use any of the passwords to access the system.