1.吉祥日
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e6+10;
const int p = 1e4;
//ios::sync_with_stdio(0);
ll a,b,c;
ll d,e,f;
int main()
{
cin>>a>>b>>c;
for(ll i=(a-2000)*360+(b*30)+1;;i+=30)
{
if(i%7==0||i%7==6)
{
d=i/360;
e=i/30;
f=i+30;
cout<<d+2000<<" "<<(e+12+1)%12<<" "<<f%30;
break;
}
}
return 0;
}
2.两人三足
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5+10;
const int p = 1e4;
//ios::sync_with_stdio(0);
ll x,y,n,m;
ll a[N],b[N];
ll suma[N],sumb[N],ans=1e18;
ll mina[N],minb[N];
int main()
{
ios::sync_with_stdio(0);
cin>>x>>y>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i],suma[i]=suma[i-1]+a[i];
for(int i=1;i<=m;i++) cin>>b[i],sumb[i]=sumb[i-1]+b[i];
mina[n]=suma[n];
minb[m]=sumb[m];
for(int i=n-1;i>=1;i--) mina[i]=min(mina[i+1],suma[i]);
for(int i=m-1;i>=1;i--) minb[i]=min(minb[i+1],sumb[i]);
ll l1,l2,r1,r2,l,r;
if(x-n>=0) l1=x-n;
else l1=0;
if(y-m>=0) l2=y-m;
else l2=0;
r1=x+n;
r2=y+m;
l=max(l1,l2);
r=min(r1,r2);
for(ll i=l;i<=r;i++)
{
ll k1=abs(x-i);
ll k2=abs(y-i);
// cout<<mina[k1]<<" "<<minb[k2]<<endl;
ans=min(ans,max(mina[k1],minb[k2]));
}
cout<<ans;
}
3.营养计划
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
ll dp[N][3];
int m;
int t[N];
//dp[i][0] t[i] 不选
//dp[i][1] t[i] 选 , t[i-1]不选
//dp[i][2] t[i] 选 , t[i-1]选 , t[i-2] 不选
int main()
{
cin>>m;
for(int i=1;i<=m;i++) cin>>t[i];
for(int i=1;i<=m;i++)
{
dp[i][0]=max(dp[i-1][0],max(dp[i-1][1],dp[i-1][2]));
dp[i][1]=dp[i-1][0]+t[i];
dp[i][2]=dp[i-1][1]+t[i];
}
cout<<max(dp[m][0],max(dp[m][1],dp[m][2]));
return 0;
}
4.能量收集赛
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int p = 1e9+7;
const int N = 5e4+10;
int n,m,t;
ll val[N];
vector<int>ve[N];
ll v[N];
ll C(ll x)
{
int x1=x-1,x2=x+1;
if(x%3==0) x/=3;
if(x1%3==0) x1/=3;
if(x2%3==0) x2/=3;
return x%p*x1%p*x2%p;;
}
int dfs(int x)
{
if(v[x]!=0) return v[x];
else
{
v[x]=val[x];
for(auto k : ve[x]) v[x]=max(v[x],dfs(k)+val[x]);
}
return v[x];
}
int main()
{
cin>>n>>m>>t;
for(int i=1;i<=n;i++) scanf("%lld",&val[i]);
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d %d",&u,&v);
ve[u].push_back(v);
}
while(t--)
{
int a,b;
scanf("%d %d",&a,&b);
ll va=dfs(a),vb=dfs(b);
printf("%lld %lld ",va,vb);
if(va<vb) swap(va,vb);
ll ansa=C(va),ansb=C(vb);
printf("%lld\n",(ansa%p-ansb%p+p)%p);
}
}
最后还有一份大佬的题解,学习一下
反向建边拓扑排序+逆元
好像可以用 dfs+记忆化代替拓扑排序
一定会来好好学习
太牛逼了
等有时间回来学习
#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
#define db long double
#define lowbit(x)((x)&-(x))
#define vl vector<int>
#define ar2 array<int,2>
#define ar3 array<int,3>
#define ar4 array<int,4>
#define int long long
const int N=100005,mod=1e9+7;
using namespace std;
int n,m,t,a[N],vis[N];
vector<int> e[N];
int f[N];
void add(int &x,int y){
x+=y;
if(x>=mod)x-=mod;
}
int kasumi(int x,int y){
x%=mod;
int ans=1;
while(y){
if(y&1)ans=ans*x%mod;
x=x*x%mod;
y/=2;
}
return ans;
}
int six,two;
int pre(int x){
return (x*(x+1)%mod*(2*x+1)%mod*six%mod+x*(x+1)%mod*two%mod)%mod;
}
int cal(int x,int y){
return (pre(y-1)-pre(x-1)+mod)%mod;
}
signed main() {
six=kasumi(6,mod-2),two=kasumi(2,mod-2);
cin>>n>>m>>t;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
for(int i=1;i<=m;i++){
int u,v;scanf("%lld%lld",&u,&v);
e[v].push_back(u);
vis[u]++;
}
queue<int> q;
for(int i=1;i<=n;i++){
if(vis[i]==0){
q.push(i);
f[i]=a[i];
}
}
while(q.size()){
int u=q.front();q.pop();
for(int v:e[u]){
vis[v]--;
f[v]=max(f[v],a[v]+f[u]);
if(vis[v]==0)q.push(v);
}
}
for(int i=1;i<=n;i++) cout<<f[i]<<endl;
while(t--)
{
int u,v;scanf("%lld%lld",&u,&v);
int x=f[u],y=f[v];
printf("%lld %lld ",x,y);
if(x>y)swap(x,y);
int ans=cal(x,y);
printf("%lld\n",ans);
}
return 0;
}