分析
还是求A+B 只不过这个带了指数
先给一个数用k个次方数表示 n表示指数 a表示底数
把A和B指数相同的部分底数相加 最后先输出有多少个不同的指数 再按从大到小的顺序输出
code
只能过3个测试点的set计数
不能用set直接计数的原因是:
假如A是1的三次 B是-1的三次
最终结果是0的三次 也就是这一项直接可以被舍去 但是set会把这个统计上
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
double a[N], b[N];
int n[N], m[N];
double res[N];
set<int> s;
void solve() {
int k;
cin >> k;
int maxn = 1010;
for (int i = 0; i < k; i ++ ) {
cin >> n[i] >> a[i];
res[n[i]] += a[i];
s.insert(n[i]);
}
// cout << res[0] << " " << res[1] << "\n";
int K;
cin >> K;
for (int i = 0; i < K; i ++ ) {
cin >> m[i] >> b[i];
res[m[i]] += b[i];
s.insert(m[i]);
}
cout << s.size();
for (int i = 1000; i >= 0; i -- ) {
if (res[i] != 0) {
cout << " " << i << " ";
printf("%.1lf", res[i]);
// cout << "res[] = " << res[i] << "\n";
}
}
}
int main() {
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
int _ = 1;
// cin >> _;
while (_ -- ) {
solve();
}
return 0;
}
正解:遍历一遍看sum的个数
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
double a[N], b[N];
int n[N], m[N];
double res[N];
void solve() {
int k;
cin >> k;
int maxn = 1010;
for (int i = 0; i < k; i ++ ) {
cin >> n[i] >> a[i];
res[n[i]] += a[i];
}
// cout << res[0] << " " << res[1] << "\n";
cin >> k;
for (int i = 0; i < k; i ++ ) {
cin >> m[i] >> b[i];
res[m[i]] += b[i];
}
int sum = 0;
for (int i = 0; i < maxn; i ++ ) {
if (res[i] != 0) {
sum ++ ;
}
}
// cout << sum << "\n";
// for (int i = 0; i < 4; i ++ ) {
// cout << res[i] << " " << i << "\n";
// }
cout << sum;
for (int i = maxn; i >= 0; i -- ) {
if (res[i] != 0) {
cout << " " << i << " ";
printf("%.1lf", res[i]);
}
}
}
int main() {
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
int _ = 1;
// cin >> _;
while (_ -- ) {
solve();
}
return 0;
}