贪心
A - Express Mail Taking
Besides on the traditional classes,Baby Volcano also needs to learn how to take the express mails.
Usually express mails are stored in cabinets. In Baby Volcano’s school,there are n cabinets in a row,numbered by 1 to n. The distance between two adjacent cabinets is 1, and the entrance is at the cabinet 1. Among all n cabinets,the one numbered k is special and it is used to enter the code and open the cabinet door. n cabinets in a row,numbered by 1 to n. The distance between two adjacent cabinets is 1, and the entrance is at the cabinet 1. Among all n cabinets,the one numbered k is special and it is used to enter the code and open the cabinet door.
Baby Volcano has m express mails to take,the i-th is in the cabinet ai. m express mails to take,the i-th is in the cabinet ai.
Two express mails will not be stored in the same cabinet. Also there is no express mail in the cabinet k. k.
To prevent expresses from being stolen, Baby Volcano have to take these express mails one by one, starting at the entrance. Generally, if he wants to take the express mail i, he have to walk to cabinet k first to enter the code, and then walks to cabinet ai. After taking the last one,he walks to the entrance. i, he have to walk to cabinet k first to enter the code, and then walks to cabinet ai. After taking the last one,he walks to the entrance.
There are so many express mails to take, so Baby Volcano wants to find a taking order which minimize the distance he walks.
Input
The first line contains one integer T(1≤T≤100),the number of testcases. T(1≤T≤100),the number of testcases.
For each test cases,the first line contains three integer n,m,k(1≤k≤n≤109,1≤m<min(n,106)) n,m,k(1≤k≤n≤109,1≤m<min(n,106))
The next line contains m integer,the i-th stand for ai(1≤ai≤n,ai≠k). m integer,the i-th stand for ai(1≤ai≤n,ai≠k).
The input guarantees that ∑m≤2×106 ∑m≤2×106
Note:Because of the large input,it is prefered to use scanf instead of cin.
Output
For each test case,Output a single line contains one integer,representing for the minimal walking distance.
Sample Input
2
10 2 5
6 7
10 2 5
3 4
Sample Output
14
10
题意
取东西,先从1出发去输密码的地方(k)然后去取东西再去k,再去取东西。。。。。最后回1
题解
离1的距离从小到大排序 离一最近的最后走
#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=1e6+10;
int n, m, k, a[maxn], T;
int main(){
scanf("%d",&T);
while(T--){
ll sum=0;
scanf("%d%d%d",&n, &m, &k);
for(int i=0;i<m;i++){
scanf("%d",&a[i]);
}
sort(a,a+m);
sum+=(k-1);
for(int i=1;i<m;i++){
sum+=(2*abs(k-a[i]));
}
if(a[0]<k) sum+=(k-1);
else sum += (2*a[0] - k - 1);
printf("%lld\n",sum);
}
return 0;
}
B - Length of Bundle Rope
Problem Description
Due to the development of online shopping, the logistics industry which is highly connectedwith goods shipping has been so prosperous that the great amount of employees is needed.Therefore, Alex, a truck driver in this growing industry, was supposed to transport severalparcels scattering in the warehouse to other cities in his daily routine.
According to the official safety requirements to the trucks running in the highway, Alex hadto tie up all the packages tightly so that he could settle the goods safely on his truck. Alexknew that the length of the cords needed for bundling the packages on the truck was based on the size of the packages themselves. Also,
n
packages can be tied up well after
n
−
1
bundles.Moreover, when bundling goods, Alex could only bundle two packages at one time to avoidscattering. Since the daily consumption of the cord was great and Alex was supposed to payfor it, he hopes to bundle all the goods with the shortest cord.
For example, there are 4 parcels in the size of 8, 5, 14, and 26 respectively. If Alex binds thefirst two together, the needed rope will be in the length of 13 (8+5 = 13) while the needed ropefor the latter two packages will be 40 (14 + 26 = 40). If Alex keeps bundling these two items,the rope length he needs will be 53 (13 + 40 = 53). So the total length of the 4 packages willbe 106 (13 + 40 + 53 = 106). If Alex tries another way by bundling the first two (8 + 5 = 13),adding up the third one (13 + 14 = 27), and then bundling the last item (27 + 14 = 53), he willonly need the cord in the length of 93 (13 + 27 + 53 = 93). Now your mission is to help Alexfinding the minimum length of the needed cord.
Input Format
The first line contains an integer
T
indicating the number of test cases. Each test case contains two lines. The first one contains a positive integer
n
indicating the number of packages. The second one contains n positive integers separated by a space to indicate the size of each parcel.
Output Format
For each test case, output the minimum length of the bundle rope required to tie all parcelstogether in one line.
Technical Specification
1
≤
T
≤
10
1
≤
n
≤
1000
The size of each parcel is at most
1000
.
Sample Input
4
6
2 3 4 4 5 7
5
5 15 40 30 10
10
3 1 5 4 8 2 6 1 1 2
9
3 2 1 6 5 2 6 4 3
Sample Output
63
205
100
98
题意
每次捆2个,捆需要的绳子是a+b 求最少需要的绳子
题解
捆最小的, 捆了之后再排序, 再捆最小的
#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
int n, a[1010], T;
int main(){
scanf("%d",&T);
while(T--){
ll ans=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
while(n > 1){
sort(a,a+n);//每次都把现在最小的两个打包
a[0] += a[1];
ans += a[0];
for(int i=1 ;i<n;i++) a[i] = a[i+1];
n--;
}
printf("%lld\n",ans);
}
return 0;
}
F - Boxers
There are n boxers, the weight of the i-th boxer is ai. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.
It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers’ weights in the team are different (i.e. unique).
Write a program that for given current values ai will find the maximum possible number of boxers in a team.
It is possible that after some change the weight of some boxer is 150001 (but no more).
Input
The first line contains an integer n (1≤n≤150000) — the number of boxers. The next line contains n integers a1,a2,…,an, where ai (1≤ai≤150000) is the weight of the i-th boxer.
Output
Print a single integer — the maximum possible number of people in a team.
Examples
Input
4
3 2 4 1
Output
4
Input
6
1 1 1 4 4 4
Output
5
Note
In the first example, boxers should not change their weights — you can just make a team out of all of them.
In the second example, one boxer with a weight of 1 can be increased by one (get the weight of 2), one boxer with a weight of 4 can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of 3 and 5, respectively). Thus, you can get a team consisting of boxers with weights of 5,4,3,2,1.
题意
一组人,可以给他加重1或减重1,求最多可以找出多少质量不一样的
题解
先从小到大排序,从前往后,如果前面没人就-1,让他更小,更方便后面的排同理
#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
int n, k, a[150010], sum, b[150010]; //b数组用来储存数字在不在
int main(){
sum=0;
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
int tmp = 1;
for(int i=0;i<n;i++){
if(a[i]-1>0&&b[a[i]-1]==0) {//如果前面没数就让它变小
sum++;
b[a[i]-1]=1;
}
else if(a[i]>0&&b[a[i]]==0){//如果前面有数就正好这样比前面大一
sum++;
b[a[i]]=1;
}
else if(b[a[i]+1]==0) {//如果前面就是自己那就变大一个
sum++;
b[a[i]+1]=1;
}
}
printf("%d\n",sum);
return 0;
}
Problem E. Bet
Input file: Standard Input
Output file: Standard Ouptut
Time limit: 1 second
The Codejamon game is on fire! Fans across the world are predicting and betting on which team
will win the game.
A gambling company is providing betting odds for all teams; the odds for the i
th team is Ai
:Bi
.
For each team, you can bet any positive amount of money, and you do not have to bet the same
amount on each team. If the i
th team wins, you get your bet on that team back, plus Bi
Ai
times
your bet on that team.
For example, suppose that there are two teams, with odds of 5:3 and 2:7 and you bet $20 on the
first team and $10 on the second team. If the first team wins, you will lose your $10 bet on the
second team, but you will receive your $20 bet back, plus 3
5 × 20 = 12, so you will have a total
of $32 at the end. If the second team wins, you will lose your $20 bet on the first team, but you
will receive your $10 bet back, plus 7
2 × 10 = 35, so you will have a total of $45 at the end. Either
way, you will have more money than you bet ($20+$10=$30).
As a greedy fan, you want to bet on as many teams as possible to make sure that as long as one
of them wins, you will always end up with more money than you bet. Can you figure out how
many teams you can bet on?
Input
The input starts with one line containing exactly one integer T, which is the number of test cases.
Each test case starts with one line containing an integer N: the number of teams in the game.
Then, N more lines follow. Each line is a pair of numbers in the form Ai
:Bi (that is, a number
Ai
, followed by a colon, then a number Bi
, with no spaces in between), indicating the odds for
the i
th team.
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 the maximum number of teams that you can bet on, under the conditions
specified in the problem statement.
Limits
• 1 ≤ T ≤ 100.
• 1 ≤ N ≤ 100.
• 0 < Ai
, Bi < 100.
• Both Ai and Bi have at most 3 digits after the decimal point.
Page 6 of 21
The 2016 ACM-ICPC Asia China-Final Contest
Sample input and output
Sample Input Sample Output
1
3
1:1.1
1:0.2
1.5:1.7
Case #1: 2
Note
In sample case #1, one optimal strategy is to bet 1.5 dollars on the first team and 1.5 dollars on
the third team. If the first team wins, you will get 1.5 + 1.5 × (1.1/1) = 3.15 dollars back, and if
the third team wins, you will get 1.5 + (1.7/1.5) × 1.5 = 3.2 dollars back. Both of these are higher
than the total money that you bet (1.5 + 1.5 = 3 dollars).
However, there is no way to bet on all three teams and be guaranteed a profit.
学会学习:E思路
#include<algorithm>
#include<iostream>
using namespace std;
long double a[1000], x, y;
int T, n, ca = 0;
char c;
int main(){
cin >> T;
while (T--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x >> c >> y;
a[i] = 1.0/(long double)(y/x + 1);
}
sort(a, a + n);
long double sum = 0;
int i;
for (i = 0; i < n; i++) {
sum += a[i];
if (sum >= 1) {
break;
}
}
printf("Case #%d: %d\n", ++ca, i);
}
}