蓝桥杯寒假训练-----8
作者:互联网
大提琴的声音就像一条河,左岸是我无法忘却的回忆,右岸是我值得紧握的璀璨年华,中间流淌的,是我年年岁岁淡淡的感伤。
A - Crossing River
A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.
Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 seconds to cross.
Output
For each test case, print a line containing the total number of seconds required for all the N people to cross the river.
Sample Input
1 4 1 2 5 10
Sample Output
17
#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 T,N,s[1010],jg,ls;
int main()
{
cin>>T;
while(T--)
{
jg=0;
cin>>N;
for(int i=0; i<N; i++)
cin>>s[i];
sort(s,s+N);
ls=N;
while(ls>=4)
{
jg+=min(2*s[0]+s[ls-2]+s[ls-1],s[0]+2*s[1]+s[ls-1]);
ls-=2;
}
if(ls==3)
jg+=s[0]+s[1]+s[2];
else if(ls==2)
jg+=s[1];
else if(ls==1)
jg+=s[0];
cout<<jg<<endl;
}
return 0;
}
B - FatMouse' Trade
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
Sample Output
13.333 31.500
#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 m,n;
struct node
{
int x;
int y;
double bz;
} p[1010];
bool cmp(node a,node b)
{
return a.bz>b.bz;
}
int main()
{
while(cin>>m>>n&&m!=-1&&n!=-1)
{
double jg=0;
for(int i=0; i<n; i++)
{
cin>>p[i].x>>p[i].y;
p[i].bz=p[i].x*1.0/p[i].y;
}
sort(p,p+n,cmp);
for(int i=0; i<n; i++)
{
if(p[i].y>=m)
{
jg+=p[i].x*m*1.0/p[i].y;
m=0;
}
else
{
jg+=p[i].x;
m-=p[i].y;
}
}
printf("%.3f\n",jg);
}
return 0;
}
C - 字数统计
一天,淘气的Tom不小心将水泼到了他哥哥Jerry刚完成的作文上。原本崭新的作文纸顿时变得皱巴巴的,更糟糕的是由于水的关系,许多字都看不清了。可怜的Tom知道他闯下大祸了,等Jerry回来一定少不了一顿修理。现在Tom只想知道Jerry的作文被“破坏”了多少。
Jerry用方格纸来写作文,每行有L个格子。(图1显示的是L = 10时的一篇作文,’X’表示该格有字,该文有三个段落)。
图1
图2
图2显示的是浸水后的作文 ,‘O’表示这个位置上的文字已经被破坏。可是Tom并不知道原先哪些格子有文字,哪些没有,他唯一知道的是原文章分为M个段落,并且每个段落另起一行,空两格开头,段落内部没有空格(注意:任何一行只要开头的两个格子没有文字就可能是一个新段落的开始,例如图2中可能有4个段落)。
Tom想知道至少有多少个字被破坏了,你能告诉他吗?
Input
测试数据有多组。每组测试数据的第一行有三个整数:N(作文的行数1 ≤ N ≤ 10000),L(作文纸每行的格子数10 ≤ L ≤ 100),M(原文的段落数1 ≤ M ≤ 20),用空格分开。
接下来是一个N × L的位矩阵(A ij)(相邻两个数由空格分开),表示被破坏后的作文。其中Aij取0时表示第i行第j列没有文字(或者是看不清了),取1时表示有文字。你可以假定:每行至少有一个1,并且所有数据都是合法的。
Output
对于每组测试输出一行,一个整数,表示至少有多少文字被破坏。
Sample Input
10 10 3 0 0 0 1 1 1 0 1 1 0 1 1 0 0 0 1 1 1 0 0 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 0 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 0
Sample Output
19
#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,L,M,A[110],p[10010];
int main()
{
while(cin>>N>>L>>M)
{
int ct=0,t=0,jg=0;
for(int i=1; i<=N; i++)
{
for(int j=1; j<=L; j++)
{
cin>>A[j];
if(A[j]==0)//一行中0的总数
jg++;
}
if(A[1]==0&&A[2]==0)
p[++ct]=t;//一段
for(int j=L; j>=1; j--)
{
if(A[j]==1)
{
t=L-j;//行末尾的0的个数
break;
}
}
}
jg=jg-2*M-t;//减去每段开头空格和最后一行0的个数
sort(p+1,p+ct+1);
for(int i=ct; i>=ct-(M-2); i--)
jg-=p[i];//减去可能为断尾0的个数最多的
cout<<jg<<endl;
}
return 0;
}
D - 湫湫系列故事——消灭兔子
湫湫减肥
越减越肥!
最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏。
游戏规则很简单,用箭杀死免子即可。
箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di(1 <= i <= M),每种箭需要一定的QQ币购买。
假设每种箭只能使用一次,每只免子也只能被射一次,请计算要消灭地图上的所有兔子最少需要的QQ币。
Input
输入数据有多组,每组数据有四行;
第一行有两个整数N,M(1 <= N, M <= 100000),分别表示兔子的个数和箭的种类;
第二行有N个正整数,分别表示兔子的血量Bi(1 <= i <= N);
第三行有M个正整数,表示每把箭所能造成的伤害值Di(1 <= i <= M);
第四行有M个正整数,表示每把箭需要花费的QQ币Pi(1 <= i <= M)。
特别说明:
1、当箭的伤害值大于等于兔子的血量时,就能将兔子杀死;
2、血量Bi,箭的伤害值Di,箭的价格Pi,均小于等于100000。
Output
如果不能杀死所有兔子,请输出”No”,否则,请输出最少的QQ币数,每组输出一行。
Sample Input
3 3 1 2 3 2 3 4 1 2 3 3 4 1 2 3 1 2 3 4 1 2 3 1
Sample Output
6 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;
int M,N,B[100010];
struct node
{
int d;
int p;
bool operator <(const node&b) const
{
return p>b.p;
}
} tp[100010];
bool cmp(node a,node b)
{
return a.d<b.d;
}
priority_queue<node> q;
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
while(!q.empty())
q.pop();
for (int i=0; i<N; i++)
scanf("%d",&B[i]);
for (int i=0; i<M; i++)
scanf("%d",&tp[i].d);
for (int i=0; i<M; i++)
scanf("%d",&tp[i].p);
if(M<N)
{
cout<<"No"<<endl;
continue;
}
sort(B,B+N);
sort(tp,tp+M,cmp);
int ct=M-1,fg=0;
ll jg=0;
for(int i=N-1; i>=0; i--)
{
while(ct>=0&&tp[ct].d>=B[i])
{
q.push(tp[ct]);
ct--;
}
if(q.empty())
{
fg=1;
break;
}
node p=q.top();
jg+=q.top().p;
q.pop();
}
if(fg)
cout<<"No"<<endl;
else
cout<<jg<<endl;
}
return 0;
}
E - Triangle
After Xiaoteng took a math class, he learned a lot of different shapes, but Xiaoteng's favorite triangle is because he likes stable shapes, just like his style.
After the Xiaoxun knew it, he wanted to give a triangle as a gift to Xiaoteng. He originally planned to do one, but he was too tired. So he decided to bring some wooden sticks to Xiaoteng and let Xiaoteng make a triangle himself.
One day, Xiaoxun brought nn sticks to Xiaoteng. Xiaoteng wanted to try to use three of them to form a triangle, but the number is too much, Xiaoteng stunned, so he can only ask for your help.
Input
There are mutiple test cases.
Each case starts with a line containing a integer n(1≤n≤5×106) n(1≤n≤5×106) which represent the number of sticks and this is followed by nn positive intergers(smaller than 231−1231−1) separated by spaces.
Output
YES or NO for each case mean Xiaoteng can or can't use three of sticks to form a triangle.
Sample Input
4 1 2 3 4
Sample Output
YES
#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,p[5000010];
int main()
{
while(scanf("%d",&N)!=EOF)
{
int fg=0;
memset(p,0,sizeof(p));
for(int i=0; i<N; i++)
scanf("%d",&p[i]);
for(int i=0; i<N-2; i++)
{
if(p[i]+p[i+1]>p[i+2])
{
// cout<<"YES"<<endl;
printf("YES\n");
fg=1;
break;
}
}
if(fg==0)
printf("NO\n");
// cout<<"NO"<<endl;
}
return 0;
}
F - Apple Catching
It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.
Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).
Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.
Input
* Line 1: Two space separated integers: T and W
* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute.
Output
* Line 1: The maximum number of apples Bessie can catch without walking more than W times.
Sample Input
7 2 2 1 1 2 2 1 1
Sample Output
6
Hint
INPUT DETAILS:
Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice.
OUTPUT DETAILS:
Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.
#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 T,W,p[1010],dp[1010][50],jg;
//dp[i][j]表示第i分钟时移动j步的最大苹果数
int main()
{
int i,j;
cin>>T>>W;
for(i=1; i<=T; i++)
cin>>p[i];
if(p[1]==1)
dp[1][0]=1;
else
dp[1][1]=1;
for(i=2; i<=T; i++)
{
for(j=0; j<=W; j++)
{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
if(j%2+1==p[i])
dp[i][j]++;
jg=max(jg,dp[i][j]);
}
}
cout<<jg<<endl;
return 0;
}
G - 最短管道距离
在一张2D地图上有N座城市,坐标依次是(X1, Y1), (X2, Y2), ... (XN, YN)。
现在H国要修建一条平行于X轴的天然气主管道。这条管道非常长,可以认为是一条平行于X轴的直线。
小Ho想知道如何修建这条管道,可以使N座城市到管道的垂直距离之和最小。请你求出这个最小的距离之和。
Input
第一行包含一个整数N。
以下N行每行包含两个整数Xi, Yi。
1 <= N <= 100000
0 <= Xi, Yi <= 1000000
Output
一个整数,代表最小的距离之和。
Sample Input
4 0 0 0 100 100 0 100 100
Sample Output
200
#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,x,y[100010];
ll jg;
int main()
{
cin>>N;
for(int i=0; i<N; i++)
cin>>x>>y[i];
sort(y,y+N);
for(int i=0; i<N; i++)
jg+=abs(y[i]-y[N/2]);
cout<<jg<<endl;
return 0;
}
H - Doing Homework again
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
Input
The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.
Output
For each test case, you should output the smallest total reduced score, one line per test case.
Sample Input
3 3 3 3 3 10 5 1 3 1 3 1 6 2 3 7 1 4 6 4 2 4 3 3 2 1 7 6 5 4
Sample Output
0 3 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;
struct node
{
int rq;
int score;
} p[1010];
int N,T,vis[2010];
bool cmp(node a,node b)
{
if(a.score==b.score)
return a.rq<b.rq;
return a.score>b.score;
}
int main()
{
cin>>T;
while(T--)
{
memset(vis,0,sizeof(vis));
cin>>N;
for(int i=0; i<N; i++)
cin>>p[i].rq;
for(int i=0; i<N; i++)
cin>>p[i].score;
sort(p,p+N,cmp);
int jg=0,fg;
for(int i=0; i<N; i++)
{
fg=0;
for(int j=p[i].rq; j>0; j--)
{
if(vis[j]==0)
{
vis[j]=1;
fg=1;
break;
}
}
if(fg==0)
jg+=p[i].score;
}
cout<<jg<<endl;
}
return 0;
}
0k-ok 发布了812 篇原创文章 · 获赞 44 · 访问量 4万+ 关注
标签:Sample,jg,训练,int,long,蓝桥,寒假,Input,include 来源: https://blog.csdn.net/weixin_44170305/article/details/104515351