2020百度之星初赛第二场

Poker

Problem Description

小沃沃在玩一个有趣的游戏。

初始他有 n 块钱,每一轮他需要投入至少 m 块钱,系统会拿走其中 p% 的钱,并把剩下的钱还给他。

请问在最优情况下,小沃沃最多可以玩多少轮?

假设当前一轮小沃沃投入了 x 块钱,那么他可以收回 ⌊x×(1−p%)⌋ 块钱,其中 ⌊a⌋ 表示 a 取下整。
小沃沃每一轮投入的钱不能超过他现在拥有的钱。

每一轮投入的钱必须为整数。

Input

第一行一个正整数 test(1≤test≤100000) 表示数据组数。

对于每组数据,一行三个整数 n,m,p(1≤n≤100000,1≤m≤1000,1≤p≤100)

Output

对每组数据输出一行一个整数表示答案。

Sample Input

2
10 2 50
10 2 100

Sample Output

9
5

思路:

模拟

代码:

#include
#include
#include
#include 
#include
using namespace std;
typedef long long ll;
int main() 
{
	int n,m,p,t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m>>p;
		int x=m*p*0.01+(m*p%100!=0);//如果m*p不能整除,就++ 
		int ans=0;
		while(n>=m)
		{
			int cnt=n/m;//每次能玩几轮 
			n-=cnt*x;//每次拿走的钱 
			ans+=cnt;//累加轮数 
		}
		cout<<ans<<endl;
	}
	
    return 0;
}

Distance

Problem Description

小沃沃所在的世界是一个二维平面。他有 n 个朋友,第 i 个朋友距离他的距离为 a[i],小沃沃并不知道这些朋友具体在什么点上。

请问在最优情况下,小沃沃的朋友两两之间的欧几里得距离的和的最小值是几?

假设小沃沃的位置为 P0=(x0,y0),第 i 个朋友的位置为 Pi=(xi,yi),对于所有的 i,需要满足 dist(P0,Pi)=a[i],并且∑n−1i=1∑nj=i+1dist(Pi,Pj) 最小,其中 dist(X,Y) 为连接点 X 和点 Y 的线段的长度。xi,yi 都可以是任意实数。

Input

第一行一个正整数 test(1≤test≤10) 表示数据组数。

对于每组数据,第一行一个正整数 n(1≤n≤100000)。

接下来一行 n 个整数,第 i 个整数 a[i](1≤a[i]1000000000) 表示第 i 个朋友和小沃沃的距离。

Output

对每组数据输出一行一个数,表示 ∑n−1i=1∑nj=i+1dist(Pi,Pj) 的最小值。答案需要四舍五入到整数。

Sample Input

2
2
3 5
5
1 2 3 4 5

Sample Output

2
20

思路:

总的两两之间距离之和最小的时候是所有的点成一条直线的时候,前缀和sum一下

代码:

#include
#include
#include
#include
using namespace std;
typedef long long ll;
ll st[100005],a[100005];//一定要用longlong
int main() 
{
	int n,m,t;
	scanf("%d",&t);
	while(t--)
	{
		cin>>m;
		for(int i=0;i<m;i++)
		scanf("%lld",&st[i]);
		sort(st,st+m);
		ll sum=0;
		for(int i=0;i<m-1;i++)
		{
			a[i]=st[i+1]-st[i];
		}
		for(int i=0;i<m-1;i++)
		{
			sum+=a[i]*(m-i-1)*(i+1);
		}
		printf("%lld\n",sum);
	}
	
    return 0;
}

你可能感兴趣的:(算法)