- 李红强 的博客
7月DAY1
- @ 2025-7-16 17:27:21
-
T1
3.用vector存储数据.
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>> a;
for(int i=0;i<n;i++){
a.push_back({});
for(int j=0;j<m;j++){
int b;
cin>>b;
a[i].push_back(b);
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cout<<a[j][i]<<' ';
}
cout<<endl;
}
return 0;
}
T3
贪心:在时间内做报酬最高的. 用优先队列维护数据.
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<pair<int, int> > a;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
a.push_back({x, y});
}
sort(a.begin(), a.end());
priority_queue<int> q;
int ans = 0,j=0;
for (int i=1;i<=m;i++) {
while (j < n && a[j].first <= i)
{
q.push(a[j].second);
j++;
}
if (q.size() != 0)
{
ans += q.top();
q.pop();
}
}
cout << ans << endl;
return 0;
}
T4
因为数据范围很小,所以可以直接暴力枚举
#include<bits/stdc++.h>
using namespace std;
int main(){
long long a,b,x;
cin>>x;
for(long long i=-200;i<=200;i++){
for(long long j=-200;j<=200;j++){
if(i*i*i*i*i-j*j*j*j*j==x) {
cout<<i<<' '<<j;
return 0;
}
}
}
return 0;
}
T5
质因数分解+二分0--mid--p^e^ 对于每一个质因子可以找到一个最小的N 最后取最大值 N!=p~1~^e1^+p~2~^e2^+……+p~n~^en^ e~i~=n/p~i~
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <cmath>
using namespace std;
unordered_map<long long, int> factorize(long long K) {
unordered_map<long long, int> factors;
while (K % 2 == 0) {
factors[2]++;
K /= 2;
}
for (long long i = 3; i * i <= K; i += 2) {
while (K % i == 0) {
factors[i]++;
K /= i;
}
}
if (K > 1) {
factors[K]++;
}
return factors;
}
long long count_p_in_factorial(long long N, long long p) {
long long count = 0;
while (N > 0) {
N /= p;
count += N;
}
return count;
}
long long find_min_N_for_prime(long long p, long long a) {
long long low = 0;
long long high = p * a;
long long answer = 0;
while (low <= high) {
long long mid = (low + high) / 2;
if (count_p_in_factorial(mid, p) >= a) {
answer = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return answer;
}
long long find_min_N(long long K) {
if (K == 1) return 1;
auto factors = factorize(K);
long long max_N = 0;
for (const auto& entry : factors) {
long long p = entry.first;
long long a = entry.second;
long long N_p = find_min_N_for_prime(p, a);
if (N_p > max_N) {
max_N = N_p;
}
}
return max_N;
}
int main() {
long long K;
cin >> K;
cout << find_min_N(K) << endl;
return 0;
}
T6
BFS搜索所有路径寻找最大差值
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int n , m , a[N] , dp[N] , d[N];
vector<int> g[N];
queue<int> q;
int main(){
cin >> n >> m;
for(int i = 1 ; i <= n ; i ++){
cin >> a[i];
}
for(int i = 1 ; i <= m ; i ++){
int x , y;
cin >> x >> y;
g[x].push_back(y);
d[y] ++;
}
for(int i = 1 ; i <= n ; i ++){
if(d[i] == 0) q.push(i);
dp[i] = INT_MAX;
}
int ans = INT_MIN;
while(!q.empty()){
int u = q.front();
q.pop();
ans = max(ans , a[u] - dp[u]);
for(auto v : g[u]){
dp[v] = min(dp[v] , min(dp[u] , a[u]));
if(-- d[v] == 0) q.push(v);
}
}
cout << ans;
return 0;
}