https://ac.nowcoder.com/acm/contest/93268
A
# include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int T;
cin>>T;
while(T--)
{
string s;
cin>>s;
cout<<"Welcome to GDUT ";
cout<<s<<endl;
}
}
B
贪心即可
# include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e3+10;
int a[N],n,x;
signed main()
{
cin>>n>>x;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
int k=x;
int ans=0;
for(int i=n;i>=1;i--)
{
if(x<=0)break;
if(x>0)
{
x=x-a[i];
ans++;
}
}
cout<<ans<<" ";
if(k<=n)
{
cout<<k;
return 0;
}
else cout<<n;
}
C bfs
# include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e3+10;
int a[N],n,x;
signed main()
{
cin>>n>>x;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
int k=x;
int ans=0;
for(int i=n;i>=1;i--)
{
if(x<=0)break;
if(x>0)
{
x=x-a[i];
ans++;
}
}
cout<<ans<<" ";
if(k<=n)
{
cout<<k;
return 0;
}
else cout<<n;
}
E
二分答案 注意bool函数中一旦a[i]>mid就返回
# include <bits/stdc++.h>
using namespace std;
#define int long long
int n,k;
const int N=1e6+10;
int a[N];
bool check(int mid)
{
int ans=0,sum=0;
for(int i=1;i<=n;i++)
{
if(a[i]>mid)return false;
if(sum+a[i]>mid)
{
sum=0;
ans++;
}
sum+=a[i];
}
if(sum)
ans++;
if(ans<=k)
return true;
return false;
}
signed main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
int l=1,r=1e18;
while(l<r)
{
int mid=l+r>>1;
if(check(mid))r=mid;
else l=mid+1;
}
cout<<l<<endl;
}
}
F
简单dp
f[i]代表血量为i时所需的最小气力 可从三个方向转移
注意血量可能从负数转移过来
# include <bits/stdc++.h>
using namespace std;
#define int long long
int hp,a,b,c,d;
const int N=1e6+01;
int k[N],f[N];
signed main()
{
int T;
cin>>T;
while(T--)
{
cin>>hp>>a>>b>>c>>d;
for(int i=1;i<=hp;i++)
cin>>k[i];
//f[0]=1;
for(int i=1;i<=hp;i++)
f[i]=1e18;
f[0]=0;
for(int i=1;i<=hp;i++)
{
f[i]=min(f[i],f[max(i-b,0ll)]+a);
f[i]=min(f[i],f[max(i-d,0ll)]+c);
if(i-4*b>=0)
f[i]=min(f[i],f[max(0ll,i-4*b-k[i-4*b]-d)]+4*a+c);
}
cout<<f[hp];
cout<<endl;
}
}
I
建图之后, 该更新k点,如果k此时不是最小值的话,那么无需看是否需要传送 ,是最小值的话(有多条最小值路线,就找传送次数最少得那条路线)多维护一个cnt即可
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define fi first
#define se second
#define i128 __int128
const int N = 2e5 + 10;
typedef pair<int, int> PII;
const int mod = 998244353;
const int inf = 1e13;
const int hash_mod = 1e9 + 7;
const double pi = 3.1415926535;
const double eps = 1e-9;
struct node{
int x,dis;
bool operator<(const node&b)const{
return b.dis<dis;
}
};
void solve() {
int n,m,k,w;cin>>n>>m>>k>>w;
vector<array<int,3>>g[n+1];
for(int i=1;i<=m;i++){
int x,y,z;cin>>x>>y>>z;
g[x].push_back({y,z,0});
g[y].push_back({x,z,0});
}
for(int i=1;i+k<=n;i++){
g[i].push_back({i+k,w,1});
g[i+k].push_back({i,w,1});
}
vector<int>dp(n+1,inf);
vector<int>cnt(n+1,0);
dp[1]=0;
priority_queue<node>q;
q.push({1,0});
vector<int>vis(n+1,0);
while(!q.empty()){
auto [x,len]=q.top();
q.pop();
if(vis[x])continue;
vis[x]=1;
for(auto [v,w,st]:g[x])
{
if(dp[v]>dp[x]+w&&!vis[v])
{
dp[v]=dp[x]+w;
cnt[v]=cnt[x]+st;
q.push({v,dp[v]});
}
else if(dp[v]==dp[x]+w)
{
if(cnt[v]>cnt[x]+st)
{
cnt[v]=cnt[x]+st;
}
}
}
}
if(dp[n]==inf)cout<<"-1 -1\n";
else cout<<dp[n]<<" "<<cnt[n]<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int _ = 1;
cin >> _;
cout << setprecision(12);
while (_--)solve();
return 0 ^ 0;
}