这场是我拉的第一场比赛 我也好久没写题解了 写的不好请多包涵




前面的题比较基础(A-O) 就不多提啦 那就从p开始咯

P - Best Cow Line

题目

FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.

The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows’ names.

FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.

FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he’s finished, FJ takes his cows for registration in this new order.

Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.

Input

  • Line 1: A single integer: N
  • Lines 2…N+1: Line i+1 contains a single initial (‘A’…‘Z’) of the cow in the ith position in the original line

Output
The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows (‘A’…‘Z’) in the new line.

Sample Input
6
A
C
D
B
C
B
Sample Output
ABCBCD

题意

给你n个字符 每次可以从前后选择最小的,选择所有的字符组成一个字典序最小的字符串注意每80个字符换一行 这里很坑

思路

既然每次可以从新的剩下的字符里面选择小的,那么就设两个指针,每次从两头往里面找,如果找到小的就输出小的,如果两个相等就继续往里面找 找到相同的两个继续往里面找 一直到不相同的,如果上指针里面的小,那么这两个一样的就输出上面的 反之就输出下面的 这样可以保证最后找出来的是最小的
eg: abaebc 找到ac不相同 输出ac 两个b相同先输出左边的,下一个小的a就可以找出来 反之下一次找的最小的就是左边的b

代码

#include <iostream>
#include <algorithm>using namespace std;const int maxn = 2000+10;
char a[maxn];int main()
{int n;char ch;cin>>n;for(int i=1;i<=n;i++){cin>>ch;a[i]=ch;}int l=1;int r=n;int cnt=0;while(l<=r){// cout<<l<<" "<<r<<endl;int indexl = l;int indexr = r;while(a[indexl]==a[indexr]){if(indexl>=indexr)break;indexl++;indexr--;}if(cnt==80){cout<<"\n";cnt=0;continue;}cnt++;if(a[indexl]>=a[indexr]){cout<<a[r];r--;}else if(a[indexr]>=a[indexl]){cout<<a[l];l++;}}
}

Q - Saruman’s Army

题目

Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each palantir has a maximum effective range of R units, and must be carried by some troop in the army (i.e., palantirs are not allowed to “free float” in mid-air). Help Saruman take control of Middle Earth by determining the minimum number of palantirs needed for Saruman to ensure that each of his minions is within R units of some palantir.

Input
The input test file will contain multiple cases. Each test case begins with a single line containing an integer R, the maximum effective range of all palantirs (where 0 ≤ R ≤ 1000), and an integer n, the number of troops in Saruman’s army (where 1 ≤ n ≤ 1000). The next line contains n integers, indicating the positions x1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1.

Output
For each test case, print a single integer indicating the minimum number of palantirs needed.

Sample Input
0 3
10 20 20
10 7
70 30 1 7 15 20 50
-1 -1
Sample Output
2
4
Hint
In the first test case, Saruman may place a palantir at positions 10 and 20. Here, note that a single palantir with range 0 can cover both of the troops at position 20.

In the second test case, Saruman can place palantirs at position 7 (covering troops at 1, 7, and 15), position 20 (covering positions 20 and 30), position 50, and position 70. Here, note that palantirs must be distributed among troops and are not allowed to “free float.” Thus, Saruman cannot place a palantir at position 60 to cover the troops at positions 50 and 70.

题意

有n个石头 现在有哨兵可以看到左右范围为r的区域 问最少需要多少个哨兵可以覆盖所有的石头(我也不知道为什么是石头2333)

思路

每次固定住一个L指针 让R往右扩展 一直扩展到L不能扩展到的地方 即L+r <R 就把L设置成此时的R,ans++ 一直往右贪心找就可以找到最小解

代码

#include <iostream>
#include <algorithm>using namespace std;typedef long long ll;
const ll maxn = 1000+10;
ll a[maxn];int main()
{ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);ll r,n;while(cin>>r>>n){if(r==-1 && n==-1)break;for(int i=1;i<=n;i++)cin>>a[i];ll L = 1;ll R = n+1;ll ans=0;sort(a+1,a+1+n);while(L<R){// cout<<L<<" "<<R<<" "<<ans<<endl;ll now = a[L];L++;while(L<R && a[L]<=now+r)L++;now = a[L-1];while(L<R && a[L]<=now+r)L++;ans++;}cout<<ans<<"\n";}
}

R - Fence Repair

题目

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn’t own a saw with which to cut the wood, so he mosies over to Farmer Don’s Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn’t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input
Line 1: One integer N, the number of planks
Lines 2… N+1: Each line contains a single integer describing the length of a needed plank
Output
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts
Sample Input
3
8
5
8
Sample Output
34
Hint
He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

题意

题意大概和合并果子一样,每次合并两堆的时候需要消耗他俩加一起的权值,问最小的权值为什么

思路

思路和合并果子一样,关于为什么要把大根堆重载成小根堆 是因为:比较大的数我们自然不想让它加上太多次 加上太多次答案就会很大 所以尽可能的让比较小的多加几次,权值比较大的少算几次 这和哈弗曼树非常像:权值越大离根越近,因为这样可以尽可能的让权值大的节点分到尽可能小的深度。

代码

#include <iostream>
#include <queue>using namespace std;const long long maxn = 20000+100;
priority_queue<long long>Q;
long long a[maxn];int main()
{long long n;cin>>n;for(long long i=0;i<n;i++){cin>>a[i];Q.push(-a[i]);}long long sum=0;for(long long i=1;i<n;i++){long long k = Q.top();Q.pop();k+=Q.top();Q.pop();Q.push(k);sum-=k;}cout<<sum;
}

哦还有个地方忘记说了 这个题要开long long

S - Cleaning Shifts

题目

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T.

Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval.

Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.
Input

  • Line 1: Two space-separated integers: N and T

  • Lines 2…N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.
    Output

  • Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.
    Sample Input
    3 10
    1 7
    3 6
    6 10
    Sample Output
    2
    Hint
    This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

INPUT DETAILS:

There are 3 cows and 10 shifts. Cow #1 can work shifts 1…7, cow #2 can work shifts 3…6, and cow #3 can work shifts 6…10.

OUTPUT DETAILS:

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

题意

有n头牛在做清洁工作 输入的是每头牛可以干活的区间,问最少需要多少头牛可以把1-t所有的牛棚打扫完。

思路

先按照L从小到大排序,如果L相同就按照r排序,因为是排完序的,所以如果第一个不是1的话就无法满足覆盖1-t 直接输出-1 continue 否则进入循环 , indexl记录的是第一次的右边界,indexr记录的是每一次选完的右边界 如果满足当前的R大于之前的indexr 那么往后遍历,如果已经超过t 退出循环 这题很奇怪,1-5和6-10是可以连接的,因为当前是按照L排序的 所以如果当前的a[i].r >=indexr+2 (最远的右边界+2) 就不能连上,ans=-1 直接退出的 关键点:indexl先记录第一次选完的右边界 然后往后找看看能不能找到右边比indexr大而且之前的indexl能接上的线段,如果能接上的话,就只扩展indexr(原因是放弃之前那一段,选择新的这一段),如果有接不上的就只能ans++ 再选择一条了

其他内容都在代码里:

代码

#include <iostream>
#include <algorithm>using namespace std;const int maxn = 25000+100;
class node
{public:int l;int r;
}a[maxn];bool cmp(node a,node b)
{return a.l==b.l?a.r>b.r:a.l<b.l;
}int main()
{ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);int n,t;while(cin>>n>>t){for(int i=0;i<n;i++){cin>>a[i].l>>a[i].r;}sort(a,a+n,cmp);int ans=1;int firx = a[0].l;int indexl = 0;int indexr = a[0].r;if(firx != 1){cout<<-1<<"\n";continue;}bool flag = true;for(int i=1;i<n;i++){// cout<<indexl<<" "<<indexr<<endl;if(a[i].r<indexr)continue; // 如果当前的线段右边没有之前的大,就遍历下一个线段if(indexr >=t)break; //如果右边界到达需求值,结束if(a[i].l >=indexr+2) //如果连不上{flag = false;ans = -1;break;}if(a[i].l >=indexl+2)// 如果上一次的右边界不能直接连上此时的l 那么需要再选一次 不然直接不选第二个选第三个{indexl = indexr; //indexl记录的是上一次能扩展前的右边界,用于下次判断此时的l能否用上次的右边界连上ans++;xindexr = a[i].r;// 前面已经判断过了  此时的r比之前的indexr大}else // 如果能直接连上,不选上次选过的 选下一个,所以ans不变{indexr = a[i].r;}// cout<<indexl<<" "<<indexr<<endl;}if(!flag || indexr<t)cout<<-1<<"\n";elsecout<<ans<<"\n";}
}
// 1 3 2 6 3 10

T - Stall Reservations

题目

Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A…B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reservation system to determine which stall each cow can be assigned for her milking time. Of course, no cow will share such a private moment with other cows.

Help FJ by determining:
The minimum number of stalls required in the barn so that each cow can have her private milking period
An assignment of cows to these stalls over time
Many answers are correct for each test dataset; a program will grade your answer.
Input
Line 1: A single integer, N

Lines 2…N+1: Line i+1 describes cow i’s milking interval with two space-separated integers.
Output
Line 1: The minimum number of stalls the barn must have.

Lines 2…N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.
Sample Input
5
1 10
2 4
3 6
5 8
4 7
Sample Output
4
1
2
3
2
4
Hint
Explanation of the sample:

Here’s a graphical schedule for this output:

Time 1 2 3 4 5 6 7 8 9 10

Stall 1 c1>>>>>>>>>>>>>>>>>>>>>>>>>>>

Stall 2 … c2>>>>>> c4>>>>>>>>> … …

Stall 3 … … c3>>>>>>>>> … … … …

Stall 4 … … … c5>>>>>>>>> … … …
Other outputs using the same number of stalls are possible.

题意

农夫要给n头牛挤奶,每一头牛?只有自己和农夫在一个棚子里才能挤奶,两头牛不能同时,而且每头牛挤奶有一个特定的时间段,问最多需要几个棚子和每头牛用的棚子的编号是多少(如果有多种情况,输出其中的一种即可)

思路 by sy

这道题是用哥给我讲的,思路就是先按照开始的时间排序,然后重载优先队列的小于号,把牛放进优先队列里面,优先队列最长的长度就是需要的最多棚子的数量,优先队列按照右边界从小到大排序,如果下一只牛进入的时间比优先队列里面第一头牛靠后,那么就取出优先队列里面的top,给当前要打扫的牛ans赋值为出来的牛的ans(出来的牛正好把它用的棚子空出来)最后把id还原回去输出,最终cnt的值就是最后的最小棚子数量,输出每个牛的ans即可

代码

#include <iostream>
#include <algorithm>
#include <queue>using namespace std;const int maxn = 100000+10;class cow
{public:int l,r,id,ans;bool operator < (const cow &a) const{return r>a.r;}
}a[maxn];
priority_queue<cow>Q;bool cmp(cow a,cow b)
{return a.l<b.l;
}bool cmp2(cow a,cow b)
{return a.id<b.id;
}
int main()
{ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);int n;cin>>n;for(int i=1;i<=n;i++){cin>>a[i].l>>a[i].r;a[i].id = i;}// while(!Q.empty())// {//     cout<<Q.top().l<<" "<<Q.top().r<<endl;//     Q.pop();// }int cnt=1;sort(a+1,a+n+1,cmp);a[1].ans = cnt;Q.push(a[1]);// for(int i=1;i<=n;i++)// {//     cout<<a[i].l<<" "<<a[i].r<<endl;//     // cout<<a[i].ans<<"\n";// }for(int i=2;i<=n;i++){if(Q.top().r<a[i].l){a[i].ans = Q.top().ans;Q.pop();Q.push(a[i]);}else{cnt++;a[i].ans=cnt;Q.push(a[i]);}}sort(a+1,a+n+1,cmp2);cout<<cnt<<"\n";for(int i=1;i<=n;i++){// cout<<a[i].l<<" "<<a[i].r<<endl;cout<<a[i].ans<<"\n";}
}

V - Stripies

题目

Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian - polosatiki, but the scientists had to invent an English name to apply for an international patent). The stripies are transparent amorphous amebiform creatures that live in flat colonies in a jelly-like nutrient medium. Most of the time the stripies are moving. When two of them collide a new stripie appears instead of them. Long observations made by our scientists enabled them to establish that the weight of the new stripie isn’t equal to the sum of weights of two disappeared stripies that collided; nevertheless, they soon learned that when two stripies of weights m1 and m2 collide the weight of resulting stripie equals to 2sqrt(m1m2). Our chemical biologists are very anxious to know to what limits can decrease the total weight of a given colony of stripies.
You are to write a program that will help them to answer this question. You may assume that 3 or more stipies never collide together.
Input
The first line of the input contains one integer N (1 <= N <= 100) - the number of stripies in a colony. Each of next N lines contains one integer ranging from 1 to 10000 - the weight of the corresponding stripie.
Output
The output must contain one line with the minimal possible total weight of colony with the accuracy of three decimal digits after the point.
Sample Input
3
72
30
50
Sample Output
120.000

题意

有n个细胞
a,b两个细胞放在一起的权值为 2∗sqrt(a∗b)2*sqrt(a*b)2∗sqrt(a∗b) 问最后合成的最小细胞的值为多少

思路

由高中学的均值不等式可得 a+b>=2∗sqrt(a∗b)a+b>=2*sqrt(a*b)a+b>=2∗sqrt(a∗b) 所以大的数多开几次平方就可以让答案尽可能的小,而且得到的还是最小值。

代码

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <queue>using namespace std;priority_queue<double>Q;int main()
{int n;cin>>n;for(int i=0;i<n;i++){double x;cin>>x;Q.push(x);}double ans=0,now1=0,now2=0;for(int i=1;i<n;i++){now1 = Q.top();Q.pop();now2 = Q.top();Q.pop();ans = 2*sqrt(now1*now2);Q.push(ans);// cout<<ans<<endl;}ans=Q.top();printf("%.3f",ans);
}

W - Protecting the Flowers

题目

Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cluster of cows was in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport each cow back to its own barn.

Each cow i is at a location that is Ti minutes (1 ≤ Ti ≤ 2,000,000) away from its own barn. Furthermore, while waiting for transport, she destroys Di (1 ≤ Di ≤ 100) flowers per minute. No matter how hard he tries, FJ can only transport one cow at a time back to her barn. Moving cow i to its barn requires 2 × Ti minutes (Ti to get there and Ti to return). FJ starts at the flower patch, transports the cow to its barn, and then walks back to the flowers, taking no extra time to get to the next cow that needs transport.

Write a program to determine the order in which FJ should pick up the cows so that the total number of flowers destroyed is minimized.

Input
Line 1: A single integer N
Lines 2… N+1: Each line contains two space-separated integers, Ti and Di, that describe a single cow’s characteristics
Output
Line 1: A single integer that is the minimum number of destroyed flowers
Sample Input
6
3 1
2 5
2 3
3 2
4 1
1 6
Sample Output
86
Hint
FJ returns the cows in the following order: 6, 2, 3, 4, 1, 5. While he is transporting cow 6 to the barn, the others destroy 24 flowers; next he will take cow 2, losing 28 more of his beautiful flora. For the cows 3, 4, 1 he loses 16, 12, and 6 flowers respectively. When he picks cow 5 there are no more cows damaging the flowers, so the loss for that cow is zero. The total flowers lost this way is 24 + 28 + 16 + 12 + 6 = 86.

题意

FJ去砍树,然后和平时一样留了 N(2≤N≤100,000)N(2 ≤ N ≤ 100,000)N(2≤N≤100,000)头牛吃草。当他回来的时候,他发现奶牛们正在津津有味地吃着FJ种的美丽的花!为了减少后续伤害,FJ决定立即采取行动:运输每头牛回到自己的牛棚。 每只奶牛i在离牛棚Ti(1≤Ti≤2,000,000)Ti(1 ≤ Ti ≤ 2,000,000)Ti(1≤Ti≤2,000,000) 分钟路程的地方,每分钟吃掉Di(1≤Di≤100)Di(1 ≤ Di ≤ 100)Di(1≤Di≤100)朵花。FJ使尽浑身解数,也只能一次带回一头奶牛。弄回一头奶牛i需要2∗Ti2*Ti2∗Ti分钟(来回)。由于怕被怼,从FJ决定带回i号奶牛开始,i号奶牛就不会吃花。请你找出被毁坏的花的最小数量 .

思路

这是一道有两个变量的贪心题,显然不能按照一个规则来贪心,于是分析一下,肯定是让单位时间吃草多的奶牛先运输,于是贪心策略完成。

代码

#include <iostream>
#include <algorithm>using namespace std;typedef long long ll;
const ll maxn = 100000+10;
class cow
{public:ll t,d;double comp;
}c[maxn];bool cmp(cow a,cow b)
{return a.comp > b.comp;//贪心策略是单位时间内吃花多的牛放前面
}int main()
{ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);int n;cin>>n;for(int i=0;i<n;i++){cin>>c[i].t>>c[i].d;c[i].comp = 1.0*c[i].d/c[i].t;}sort(c,c+n,cmp);ll ans=0,cnt=0;for(int i=0;i<n;i++){ans+=cnt*c[i].d;cnt+=c[i].t*2; //来回返程两趟}cout<<ans;
}

X - The Dragon of Loowater

题目

Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem.

The shores of Rellau Creek in central Loowater had always been a prime breeding ground for geese. Due to the lack of predators, the geese population was out of control. The people of Loowater mostly kept clear of the geese. Occasionally, a goose would attack one of the people, and perhaps bite off a finger or two, but in general, the people tolerated the geese as a minor nuisance.

One day, a freak mutation occurred, and one of the geese spawned a multi-headed fire-breathing dragon. When the dragon grew up, he threatened to burn the Kingdom of Loowater to a crisp. Loowater had a major problem. The king was alarmed, and called on his knights to slay the dragon and save the kingdom.

The knights explained: “To slay the dragon, we must chop off all its heads. Each knight can chop off one of the dragon’s heads. The heads of the dragon are of different sizes. In order to chop off a head, a knight must be at least as tall as the diameter of the head. The knights’ union demands that for chopping off a head, a knight must be paid a wage equal to one gold coin for each centimetre of the knight’s height.”

Would there be enough knights to defeat the dragon? The king called on his advisors to help him decide how many and which knights to hire. After having lost a lot of money building Mir Park, the king wanted to minimize the expense of slaying the dragon. As one of the advisors, your job was to help the king. You took it very seriously: if you failed, you and the whole kingdom would be burnt to a crisp!

Input
The input contains several test cases. The first line of each test case contains two integers between 1 and 20000 inclusive, indicating the number n of heads that the dragon has, and the number m of knights in the kingdom. The next n lines each contain an integer, and give the diameters of the dragon’s heads, in centimetres. The following m lines each contain an integer, and specify the heights of the knights of Loowater, also in centimetres.

The last test case is followed by a line containing:

0 0

Output
For each test case, output a line containing the minimum number of gold coins that the king needs to pay to slay the dragon. If it is not possible for the knights of Loowater to slay the dragon, output the line:

Loowater is doomed!

Sample Input
2 3
5
4
7
8
4
2 1
5
5
10
0 0
Sample Output
11
Loowater is doomed!

题意

有n头龙?和m个骑士,需要让这些骑士杀掉所有的龙,骑士能杀死龙的条件是骑士的身高比龙头的直径要高,每个骑士只能被雇用一次,且雇佣的费用是骑士的身高,求杀死所有龙所需要的最少费用 如果不能杀死所有的龙输出 Loowater is doomed!

思路

把龙头的直径和骑士的身高从小到大排序,从第一头龙开始找,然后从所有的骑士里面找能杀死他的,如果找到一个肯定是最小的,然后把他标记上(代表不能再雇佣他了) 如果可以杀死所有的龙?,那么输出答案,否则输出 Loowater is doomed!

代码

#include <iostream>
#include <algorithm>
#include <cstring>using namespace std;const int maxn = 20000+100;
int a[maxn],b[maxn];
bool bk[maxn];int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);int n,m;while(cin>>n>>m){if(m==0 && n==0)break;memset(bk,false,sizeof(bk));for(int i=0;i<n;i++)cin>>a[i];for(int i=0;i<m;i++)cin>>b[i];sort(a,a+n);sort(b,b+m);bool flag=false;int sum=0,cnt=0;for(int i=0;i<m;i++)for(int j=0;j<n;j++){if(!bk[j] && b[i]>=a[j]){flag=true;cnt++;sum+=b[i];bk[j]=true;break;}}// cout<<sum<<"\n";if(cnt==n && flag)cout<<sum<<"\n";elsecout<<"Loowater is doomed! \n";}
}

Y - Commando War

题目

“Waiting for orders we held in the wood, word from the front never came
By evening the sound of the gunfire was miles away
Ah softly we moved through the shadows, slip away through the trees
Crossing their lines in the mists in the fields on our hands and our knees
And all that I ever, was able to see
The fire in the air, glowing red, silhouetting the smoke on the breeze”
There is a war and it doesn’t look very promising for your country. Now it’s time to act. You
have a commando squad at your disposal and planning an ambush on an important enemy camp
located nearby. You have N soldiers in your squad. In your master-plan, every single soldier has a
unique responsibility and you don’t want any of your soldier to know the plan for other soldiers so that
everyone can focus on his task only. In order to enforce this, you brief every individual soldier about
his tasks separately and just before sending him to the battlefield. You know that every single soldier
needs a certain amount of time to execute his job. You also know very clearly how much time you
need to brief every single soldier. Being anxious to finish the total operation as soon as possible, you
need to find an order of briefing your soldiers that will minimize the time necessary for all the soldiers
to complete their tasks. You may assume that, no soldier has a plan that depends on the tasks of his
fellows. In other words, once a soldier begins a task, he can finish it without the necessity of pausing
in between.
Input
There will be multiple test cases in the input file. Every test case starts with an integer N (1 ≤
N ≤ 1000), denoting the number of soldiers. Each of the following N lines describe a soldier with two
integers B (1 ≤ B ≤ 10000) & J (1 ≤ J ≤ 10000). B seconds are needed to brief the soldier while
completing his job needs J seconds. The end of input will be denoted by a case with N = 0. This case
should not be processed.
Output
For each test case, print a line in the format, ‘Case X: Y ’, where X is the case number & Y is the
total number of seconds counted from the start of your first briefing till the completion of all jobs.
Sample Input
3
2 5
3 2
2 1
3
3 3
4 4
5 5
0
Sample Output
Case 1: 8
Case 2: 15

题意

有n个士兵,现在你需要听他们每个人汇报工作,汇报工作完以后他们可以开始工作,在他们工作的时候你可以去听下一个士兵汇报工作,求执行完所有操作需要的总时间的最小值。

思路

在上一个士兵汇报工作后让now记录他工作的时间,如果now可以覆盖下一个人的所有时间就继续往后,如果不行的话就必须更新now的值为下一个人的工作时间(上一次不能覆盖,在听下一个人汇报工作的时候或者工作的时候第一个人执行完工作),最后让所有汇报工作的时间加上多余需要的时间即可。

代码

#include <iostream>
#include <algorithm>using namespace std;const int maxn = 1000+10;struct node
{int speak;int metion;
}a[maxn];bool cmp(node a,node b)
{if(a.metion == b.metion)return a.speak<b.speak;return a.metion >b.metion;
}
int main()
{ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);int n,ca=1;while(cin>>n && n){for(int i=0;i<n;i++){cin>>a[i].speak>>a[i].metion;}sort(a,a+n,cmp);// for(int i=0;i<n;i++)//     cout<<a[i].speak<<" "<<a[i].metion<<endl;int ans=a[0].speak,now=a[0].metion;for(int i=1;i<n;i++){ans+=a[i].speak;now-=a[i].speak;    // 在上一个士兵执行任务的时候可以去听下一个汇报if(now<a[i].metion) // 如果剩余的时间不足以让下一个士兵执行完任务now=a[i].metion; // 就让now=多的时间 最后一次算的是上一个巡逻的和下一次说的差值 如果}// cout<<now<<endl;ans+=now;cout<<"Case "<<ca++<<": "<<ans<<"\n";}
}

Z - Children’s Game

题目

There are lots of number games for children. These games are pretty easy to play but not so easy to
make. We will discuss about an interesting game here. Each player will be given N positive integer.
(S)He can make a big integer by appending those integers after one another. Such as if there are
4 integers as 123, 124, 56, 90 then the following integers can be made — 1231245690, 1241235690,
5612312490, 9012312456, 9056124123, etc. In fact 24 such integers can be made. But one thing is sure
that 9056124123 is the largest possible integer which can be made.
You may think that it’s very easy to find out the answer but will it be easy for a child who has just
got the idea of number?
Input
Each input starts with a positive integer N (≤ 50). In next lines there are N positive integers. Input
is terminated by N = 0, which should not be processed.
Output
For each input set, you have to print the largest possible integer which can be made by appending all
the N integers.
Sample Input
4
123 124 56 90
5
123 124 56 90 9
5
9 9 9 9 9
0
Sample Output
9056124123
99056124123

题意

让你从给定的n个字符串中组成一个字典序最大的字符串

思路

如果是9 和 90 两个字符串,如果按照字典序排序的话是909 但是990比它要大,于是按照x+y>y+x排序,如果x+y>y+x的话返回1 否则返回0 这样排完之后再加上就是字典序最大的字符串了。

代码

#include <iostream>
#include <algorithm>using namespace std;const int maxn = 50+10;
string a[maxn];bool cmp(string a,string b)
{return a+b>b+a; // 返回a>b的情况
}int main()
{int n;while(cin>>n && n){for(int i=0;i<n;i++)cin>>a[i];sort(a,a+n,cmp);for(int i=0;i<n;i++)cout<<a[i];cout<<"\n";}
}

2019-2020新生训练class 1- 熟悉oj的使用方法贪心相关推荐

  1. bistuacm 2019年第⑦场新生训练赛题解

    比赛链接:bistuacm 新生训练赛 第七场 难度:cf 900~1400 A 知识点:枚举 题意:寻找距离数组某个数最接近的a[i]<=k且b[i]=1的数. 解法:按题意模拟即可. #in ...

  2. 郑州军办计算机学校,郑州市国防科技学校2019级新生开启军训模式

    郑州市国防科技学校举行2019年新训动员大会 举行升旗仪式 新生列队整齐 班主任代表发言 教官代表发言 教官队列展示 学生队列整齐 副校长段峰同志的宣布开训 8月11日上午10点钟,郑州市国防科技学校 ...

  3. 卡耐基梅隆大学计算机工程录取率,卡内基梅隆大学2020新生数据,计算机学院录取率堪比藤校!...

    原标题:卡内基梅隆大学2020新生数据,计算机学院录取率堪比藤校! 卡内基梅隆大学(Carnegie Mellon University),简称CMU,坐落在美国宾夕法尼亚州的匹兹堡,美国25所新常春 ...

  4. 美国计算机科学本科录取人数,卡内基梅隆大学2020新生数据出炉!计算机学院录取率堪比藤校...

    原标题:卡内基梅隆大学2020新生数据出炉!计算机学院录取率堪比藤校 提起CMU,想必各位小伙伴肯定都不陌生,坐落在美国宾夕法尼亚州匹兹堡的它,可是美国25所新常春藤盟校之一.最近卡内基梅隆大学202 ...

  5. 西航职院计算机工程学院,西航职院 | 计算机工程学院组织召开2019级新生见面会...

    原标题:西航职院 | 计算机工程学院组织召开2019级新生见面会 2019年9月2日下午,计算机工程学院在新区体育馆举行2019级新生见面会.计算机工程学院院长史小英.党总支副书记张永红.各专业带头人 ...

  6. 济南电子机械工程学校计算机专业班主任,济南电子机械工程学校2019级新生报到、军训工作安排通知...

    济南电子机械工程学校2019级新生报到.军训工作安排通知 各位家长朋友.2019级新同学: 为妥善做好2019级新生报到及军训工作,现将学校2019级新生报到.军训工作具体安排如下,请您知悉并遵照执行 ...

  7. 动态改变_【学校动态】在坚守与改变中追求卓越——礼县二中召开2019—2020学年度秋季学期第二次全体教职工大会...

    点击上方"礼县第二中学"关注我们 礼县二中召开2019-2020学年度秋季学期第二次全体教职工大会 9月22日晚,礼县二中召开2019-2020学年度秋季学期第二次全体教职工大会. ...

  8. 《2019~2020网络安全态势观察报告》重磅发布!

    [导读]过去一年多,各种 APT 攻击事件.勒索挖矿事件,数据泄露事件,漏洞攻击事件仍然不绝于耳.从 ATT&CK 模型框架的兴起到实战化攻防环境的建立,从反序列化漏洞的攻防博弈到 VPN 漏 ...

  9. BNUZ-ACM 2019国庆新生欢乐赛 E.如风般奔跑(题目详解+代码)

    原题链接:BNUZ-ACM 2019 E.如风般奔跑 BNUZ-ACM 2019国庆新生欢乐赛 E.如风般奔跑 题目 题目大意 这题题目的大意是:给你一个800米的环形跑道以及小陈和小张的速度,问你什 ...

最新文章

  1. 一文读懂简化的图卷积网络GCN(SGC)| ICML 2019
  2. 通过脚本案例学习shell(五) 通过创建DNS脚本一步一步教你将一个普通脚本规范到一个生产环境脚本...
  3. 让AI个性化而且功耗更低 IBM研发新型神经网络芯片
  4. 亲测好用!机器学习环境搭建及基础
  5. (0011) iOS 开发之模拟HTTP请求与响应,返回自己想要的报文。
  6. 解决微信小程序配置https不成功问题
  7. dp_Pku1887
  8. python数据驱动ddt_python_数据驱动_ddt
  9. C语言实现List实现(附完整源码)
  10. css背景图片、圆角、盒子阴影、浮动
  11. 速度一半永远追不上_您将永远不会知道自己应该怎么做的一半-没关系。
  12. Maven的pom报错的解决方法
  13. 布客·ApacheCN 编程/后端/大数据/人工智能学习资源 2020.9
  14. 第十一节:Springboot整合log4j2日志
  15. JAVA类加载的委托模型
  16. 《黑客帝国》观后感之我所理解的地球矩阵
  17. 安川服务器报b33怎么维修,驻马店安川伺服报警B33故障维修
  18. 阿里云商标驳回复审申请收费价格、结果时间及常见问题解答
  19. 魔术表演的核心秘密(一)——开篇简介
  20. Sublime 安装与中文配置

热门文章

  1. 伴着代码,那个女孩儿慢慢长大
  2. 190108每日一句
  3. ForgivingExceptionHandler: An unexpected connection driver error occured (Exception message: Socket
  4. Docker基础、进阶笔记,为k8s的学习预预热
  5. Tslib移植与分析
  6. 2023年春招热门笔试算法题(C++)
  7. md 生成目录 码云_DuangDuangDuang!码云项目的 Readme.md 特殊技能
  8. 在c语言中把x和y互换怎么编译,已知int x=10,y=12;编程将x和y的值相互交换
  9. Coding 码市:携手开发者与创业者,重新定义软件外包
  10. python labelImg xml 格式的数据集解析及可视化