8/23
T639126 落落的去的数学问题2
思路: 由题目数据范围可知,A[i]<1e8 所以A[i]+A[j]<2e8 所以只要记录所有A[i]+A[j]的和,再记录A[i]+A[j]>1e8的数量,最后用总和减去A[i]+A[j]>1e8的数量乘1e8
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 3e5 + 5;
ll n, a[N], sum, cnt;
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin>>a[i];
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++){
sum += a[i];
int pos = lower_bound(a + i + 1, a + 1 + n, 100000000 - a[i]) - a;
int now = n - pos + 1;
cnt += now;
}
cout << sum * (n - 1) - cnt * 100000000;
return 0;
}
T638485 第K小
设一个优先队列,用来排序 再用双重map,去重 一直将两个数字相加的数字加入优先队列 每次将队首出队,直至达到求的第k小数为止。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n, m, ans, a[15];
map<ll, bool> mp;
ll bfs() {
priority_queue<ll, vector<ll>, greater<ll> > pq;
for (int i = 1; i <= n; i++) {
if (mp[a[i]]) continue;
pq.push(a[i]);
mp[a[i]] = 1;
}
while (!pq.empty()) {
ans++;
ll t = pq.top();
if (ans == m) return t;
pq.pop();
for (int i = 1; i <= n; i++) {
if (mp[t + a[i]]) continue;
mp[t + a[i]] = 1;
pq.push(t + a[i]);
}
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
cout << bfs();
return 0;
}
U591734 磁铁迷宫
这是一个普通的深搜的题目,重点就是处理好磁铁边上的格子,因为两个互不可达的活动区域中,有可能有部分在此帖周围的区块互相重叠,所以磁铁边的格子标记过后在最后需要取消标记
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e3 + 10;
int n, m, a[N][N];
int ans = 1;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int vis[N][N];
int cnt, color;
void dfs(int x, int y) {
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy] == color || a[xx][yy] == 1) continue;
vis[xx][yy] = color;
cnt++;
if (a[xx][yy] == 2) continue;
dfs(xx, yy);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
if (c == '#') a[i][j] = 1;
else a[i][j] = 0;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == 1) continue;
if (a[i-1][j] == 1 || a[i+1][j] == 1 || a[i][j-1] == 1 || a[i][j+1] == 1) {
a[i][j] = 2;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (vis[i][j] == 0 && a[i][j] == 0) {
cnt = 1;
color++;
vis[i][j] = color;
dfs(i, j);
ans = max(ans, cnt);
}
}
}
cout << ans;
return 0;
}