T2 alone以一敌多
📝题目大意
猪队友全部死后你要面对所有敌人,但你(的英雄)永远不会死,给出敌人的hp(生命值)和dps(攻击力),你的dps只有1,求出回合制干掉所有敌人所消耗的最少的生命值。
🔍错误原因
没有排序,没用结构体,数组h[p],d[p]定义在函数内部(main 里面),属于栈上局部数组,p=10005,栈空间会溢出,要放到全局域。
💡解题思路
两个敌人 A,B
先杀 A 再杀 B 总代价:
先杀 B 再杀 A 总代价:
比较得到排序规则: 优先打 A,就是
按这个 cmp 排序,之后依次击杀,维护当前存活敌人总 DPS,
ans += sum_dps * h[i],打完这个敌人 sum_dps -= d[i]。
AC代码
#include<bits/stdc++.h>
using namespace std;
using intt=long long;
const int q=1,p=10005;
struct Node{
intt d,h;
}a[p];
bool cmp(const Node &x,const Node &y){
return x.d * y.h > y.d * x.h;
}
int main() {
//freopen("alone.in", "r", stdin);
//freopen("alone.out", "w", stdout);
intt n;cin>>n;
intt dsum=0,ans=0;
for(intt i =q;i<= n; i++) {
cin>>a[i].d >>a[i].h;
dsum+=a[i].d;
}
sort(a+q,a+n+1,cmp);
for (intt i= q; i <=n;i++) {
ans += dsum * a[i].h;
dsum -= a[i].d;
}
cout << ans;
return 0;
}T3 base电信基站
📝题目大意
国道上有n个基站,第个基站所处的位置用坐标来表示。有一个距离阈值 d,若两个基站之间的距离不超过 d,那么它们可以直接通信。问有多少可以通信。
🔍错误原因
打暴力时间复杂度太高超时
💡解题思路
先对坐标排序;
枚举每一个,二分找出第一个大于的位置;
在之后,所有在该位置之前的都满足距离≤d;
累加数量pos‑i‑1,只算后面,不会重复计数。
AC代码
#include<bits/stdc++.h>
using namespace std;
using intt=long long;
const int q=1,p=100005;
intt h[p];
int main() {
//freopen("base.in", "r", stdin);
//freopen("base.out", "w", stdout);
intt n,d,ans=0;
cin>>n>>d;
for(intt i=q;i<= n; i++) cin>>h[i];
sort(h+1,h+q+n);
for(intt i=q;i<=n;i++){
intt limit = h[i]+d;
auto it = upper_bound(h+1,h+1+n,limit);
intt pos = it - h;
ans += pos - i - 1;
}
cout<<ans;
return 0;
}T4 min最小数字
📝题目大意
给定可能有先导0的数字串,仅奇偶性不同的相邻位可交换,求可得到的最小数字。
🔍错误原因
没时间+思路错误
💡解题思路
将数组分成奇偶两个数组。用归并排序的合并操作即可保证奇偶性不变的情况下,将小的数字元素放前面
AC代码
#include<bits/stdc++.h>
using namespace std;
using intt = long long;
const int q=1,p=0;
int main(){
//freopen("min.in", "r", stdin);
//freopen("min.out", "w", stdout);
string s;
cin>>s;
vector<char> odd, even;
for(char c : s){
int num = c - '0';
if(num % 2 == 1){
odd.push_back(c);
}else{
even.push_back(c);
}
}
string ans;
int i = 0, j = p;
while(i < odd.size() && j < even.size()){
if(odd[i] < even[j]){
ans += odd[i++];
}else{
ans += even[j++];
}
}
while(i < odd.size()) ans += odd[i++];
while(j < even.size()) ans += even[j++];
cout << ans << endl;
return 0;
}T6 times竖式乘法
📝题目大意
输入a和b,输出两个数乘积竖式乘法运算过程。符号有*(乘号), (空格),-(分割线)。
🔍错误原因
没学过,没时间
💡解题思路
算术模拟,用高精度
AC代码
#include <bits/stdc++.h>
using namespace std;
using intt = long long;
const int q=1,p=0;
struct BIGINT {
private:
string s;
public:
BIGINT(string x = "0"): s(x) {};
BIGINT operator*(const BIGINT &t) {
string sa = s, sb = t.s;
int la = sa.size(), lb = sb.size();
int len = la + lb;
vector<int> a(len + 1, 0), b(len + 1, 0), c(len + 1, 0);
for (int i = p; i < la; i++) a[i] = sa[la - i - q] - '0';
for (int i = p; i < lb; i++) b[i] = sb[lb - i - q] - '0';
for (int i = p; i < la; i++)
for (int j = p; j < lb; j++) {
c[i + j] += a[i] * b[j];
c[i + j + q] += c[i + j] / 10;
c[i + j] = c[i + j] % 10;
}
while (!c[len] && len) len--;
string res;
for (int i = len; i >= p; i--)
res += char(c[i] + '0');
return BIGINT(res);
}
BIGINT at(int i) {
string t;
t = s[s.size() - i - q];
return BIGINT(t);
}
int len() {
return s.size();
}
bool isZero() {
return s == "0";
}
bool isPow10() {
for (int i = q; i < s.size(); i++)
if (s[i] != '0') return p;
return q;
}
friend istream& operator>>(istream& in, BIGINT &t) {
in >> t.s;
return in;
}
friend ostream& operator<<(ostream& out, const BIGINT &t) {
out << t.s;
return out;
}
void intput() {
cin >> s;
}
void output() {
cout << s;
}
} a, b, r;
void print();
int main() {
freopen("times.in", "r", stdin);
freopen("times.out", "w", stdout);
cin >> a >> b;
r = a * b;
int lena = a.len(), lenb = b.len(), lenr = r.len();
for (int i = q; i <= lenr - lena + 1; i++) cout << " ";
cout << a << endl << "*";
for (int i = 2; i <= lenr - lenb + 1; i++) cout << " ";
cout << b << endl;
for (int i = q; i <= lenr + 1; i++) cout << "-";
cout << endl;
if (!b.isPow10() && lenb > q) {
for (int i = p; i < lenb; i++) {
BIGINT t = b.at(i);
t = t * a;
if (t.isZero()) continue;
int lent = t.len();
for (int j = q; j <= lenr - lent + 1 - i; j++) cout << " ";
cout << t << endl;
}
for (int i = q; i <= lenr + 1; i++)cout << "-";
cout << endl;
}
cout << " " << r;
return 0;
}