蒟蒻的 Educational Codeforces Round 118 题解A-C(可能持续更新)
A题
题意
给你两个数,一个数是x1后面跟着p1个0,另一个是x2后面跟着p2个0,让你比较两个数的大小
思路
先比较两个数最终结果的位数,位数大小不相等的话结果很显然。相等的时候让x部分小的那个数结尾多添几个0,使两个数x部分位数相同,然后对两个x进行比较,得出大小。
代码
#include<iostream>
#include<algorithm>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int x1, p1, x2, p2;
int qmi(int n, int w)
{
int nn = 1;
while (n)
{
if (n & 1)
{
nn *= w;
}
w *= w;
n >>= 1;
}
return nn;
}
int func(int x)
{
int ans = 0;
while (x)
{
ans++;
x /= 10;
}
return ans;
}
int main()
{
IOS;
int t;
cin >> t;
while (t--)
{
cin >> x1 >> p1 >> x2 >> p2;
int a = func(x1);
int b = func(x2);
int aa = a, bb = b;
a += p1; b += p2;
if (a < b)cout << "<" << endl;
else if (a > b)cout << ">" << endl;
else
{
if (aa < bb)
{
x1 = x1 * qmi(bb-aa,10);
}
else if (aa > bb)x2 =x2* qmi(aa - bb,10);
if (x1 > x2)cout << ">" << endl;
else if (x1 < x2)cout << "<" << endl;
else cout << "=" << endl;
}
}
}
B题
题意
给你n个乱序的数,要你找出n/2对x和y,使x!=y,且x mod y不在那些乱序的数之中。
思路
很简单,假设c=a mod b,那c一定小于b。利用这一结论,我们找出整个数组中最小的元素,然后把它作为y值,和别的数匹配输出n/2对对组就好了。
代码
#include<iostream>
#include<algorithm>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int a[200010];
int main()
{
IOS;
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort(a + 1, a+1 + n);
int bz = a[1];
int idx = 2;
for (int i = 1; i <= n/2; i++)
{
cout <<a[idx]<< " " << bz << endl;
idx++;
}
}
}
C题
题意
给一个数组,人会在每一个数组元素所指的时刻给目标刷新一个毒的效果,目标有h点血,让你给出一个每次效果的最短持续时间,以让目标足够被毒死。
思路
很显然可以对分枚举一遍来做
代码
#include<iostream>
#include<algorithm>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
ll a[110];
ll cf[110];
int n;
ll h;
bool judge(ll x)
{
ll ans = 0;
for (int i = 1; i <= n-1; i++)
{
if (cf[i + 1] > x)
{
ans += x;
}
else
{
ans += cf[i + 1];
}
if (ans >= h)return 1;
}
ans += x;
if (ans >= h)return 1;
return 0;
}
int main()
{
IOS;
int t;
cin >> t;
while (t--)
{
cin >> n >> h;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
cf[i] = a[i] - a[i-1];
}
ll aa=0;
ll l = 0; ll r = h + a[1]+100;
while (l <= r)
{
ll m = (l + r) / 2;
if (judge(m))
{
r = m - 1;
aa = m;
}
else
{
l = m + 1;
}
}
cout << aa << endl;
}
}

浙公网安备 33010602011771号