0702训练题目题解
作者:互联网
[LC2300]
class Solution {
public:
using LL = long long;
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
//sort(spells.begin(), spells.end());
sort(potions.begin(), potions.end());
int n = spells.size(), m = potions.size();
int r = m - 1;
vector<int> ans(n , 0);
for (int i = 0; i < n; i++) {
int t = getVal(spells[i], potions, success); //返回满足第i个咒语组合药水的数目;
ans[i] = t;
}
return ans;
}
int getVal(int val, vector<int>& potions, LL success)
{
int n = potions.size();
int l = 0, r = n - 1;
while (l < r) {
int mid = (l + r) / 2;
if ((LL)val * potions[mid] >= success) r = mid;
else l = mid + 1;
}
//判断边界情况;
if ((LL)val * potions[l] >= success)
return n - l;
return 0;
}
};
[LC2101]
//先建图,然后DFS/BFS;
//图上总共点数不超过100,边数最多100*100;
const int N = 110, M = 10010;
int h[N], e[M], ne[M], idx; //不用带边权;
int cnt[N];
//考虑存在自环的情况;
void add(int a, int b)
{
//cnt[a] = 1, cnt[b] = 1;
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
//因为链式前向星是单个id进行建图,所以需要将二维坐标映射为1维坐标;
class Solution {
public:
using PII = pair<int, int>;
using LL = long long;
void init()
{
memset(h, -1, sizeof h);
idx = 0;
//memset(cnt, 0, sizeof cnt);
}
unordered_map<LL, int> HashMap;
int id = 0;
int ans = 0;
unordered_map<int, int> CntHash;
int maximumDetonation(vector<vector<int>>& bombs) {
init();
int n = bombs.size();
//满足爆炸范围的进行建图;
for (int i = 0; i < n; i++)
{
LL p1 = bombs[i][0] * 1e5 + bombs[i][1];
if (!HashMap.count(p1)) {
HashMap[p1] = ++id;
}
int a = HashMap[p1];
CntHash[a]++;
int r = bombs[i][2]; //半径;
for (int j = 0; j < n; j++) {
if (i != j) {
LL p2 = bombs[j][0]*1e5 + bombs[j][1];
if (!HashMap.count(p2)) {
HashMap[p2] = ++id;
}
int b = HashMap[p2];
LL deltax = abs(bombs[i][0] - bombs[j][0]);
LL deltay = abs(bombs[i][1] - bombs[j][1]);
LL dis = deltax*deltax + deltay*deltay;
if (dis <= (LL)r*r) {
if (a != b)
add(a, b);
else
{
cnt[a]++;
cnt[b]++;
}
}
}
}
}
//因为爆炸传递存在有向性,所以需要枚举所有的爆炸起点;
for (int i = 0; i < n; i++)
{
LL p1 = bombs[i][0] * 1e5 + bombs[i][1];
unordered_map<int, int> temp; //已经爆炸过的节点;
ans = max(ans, dfs(HashMap[p1], temp));
}
return ans;
}
int dfs(int u, unordered_map<int, int>& hashMap0)
{
//当前节点爆炸;
hashMap0[u]++;
int res = 0;
res += CntHash[u];
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (!hashMap0.count(j))
res += dfs(j, hashMap0);
}
return res;
}
};
[LC2115]
const int N = 1e4 + 10;
int h[N], e[N], ne[N], idx;
int din[N], dout[N]; //出度入度;
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
din[b]++, dout[a]++;
}
class Solution {
public:
void init()
{
memset(h, -1, sizeof h);
memset(din, 0, sizeof din);
memset(dout, 0, sizeof dout);
idx = 0;
}
unordered_map<string, int> HashMap; //将字符串映射成整数;
unordered_map<int, string> unHashMap;
int StrIdx = 0;
unordered_map<string, int> recipesMap;
//unordered_map<int, int> recipesInts;
vector<string> ans;
vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {
//构建树;
init();
//反向建图
for (int i = 0; i < recipes.size(); i++)
{
if (!HashMap.count(recipes[i])) {
HashMap[recipes[i]] = ++StrIdx;
unHashMap[StrIdx] = recipes[i];
}
int a = HashMap[recipes[i]];
for (int j = 0; j < ingredients[i].size(); j++) {
if (!HashMap[ingredients[i][j]]) {
HashMap[ingredients[i][j]] = ++StrIdx;
unHashMap[StrIdx] = ingredients[i][j];
}
int b = HashMap[ingredients[i][j]];
add(b, a); //从b->a连一条边;
}
}
for (auto s : recipes) recipesMap[s]++;
// for (auto it = HashMap.begin(); it != HashMap.end(); ++it)
// cout << it->first << endl;
queue<int> qu;
for (auto s : supplies) {
//cout << s << "->" ;
if (HashMap.count(s)) {
int id = HashMap[s];
//cout << id <<" :" << din[id] << endl;
if (!din[id]) qu.push(id); //入度为0的节点入队;
}
}
//cout << qu.size() << endl;
while (qu.size()) {
int u = qu.front();
qu.pop();
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i]; //
if (--din[j] == 0) { //说明入度为0且是道菜,说明可以被做出来;
qu.push(j);
if (recipesMap.count(unHashMap[j]))
ans.push_back(unHashMap[j]);
}
}
}
return ans;
}
};
标签:题目,HashMap,idx,int,题解,++,vector,bombs,0702 来源: https://www.cnblogs.com/zhanghanLeo/p/16439576.html