1001.Anti-AK Problem
1002.Blow up the Enemy
1003.Contest of Rope Pulling
const ll inf=1e16;
const int N=1e3+10;
const int M=1e6+10;
ll n,m;
inline ll rd(){//inline 会更快 register int n;放全局变量更快
ll X=0,w=0; char ch=0;
while(!isdigit(ch)) {
w|=ch=='-';
ch=getchar();
}
while(isdigit(ch))
X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
ll w1[N],v1[N],w2[N],v2[N];
ll f1[M],f2[M];
int main(){
ll T,x,y;
T=rd();
while (T--){
n=rd();m=rd();
ll m1=0,m2=0;
for (int i=1;i<=n;i++){
w1[i]=rd();v1[i]=rd();
m1+=w1[i];
}
for (int i=1;i<=m;i++){
w2[i]=rd();v2[i]=rd();
m2+=w2[i];
}
for (int i=1;i<=min(m1,m2);i++)f1[i]=f2[i]=-inf;
f1[0]=f2[0]=0;
ll sumw=0,bound;
for(int i=1;i<=n;i++){
sumw+=w1[i];
bound=max(1ll,w1[i]);
for(int c=min(m2,sumw);c>=w1[i];c--){
f1[c]=max(f1[c],f1[c-w1[i]]+v1[i]);
}
}
sumw=0;
for(int i=1;i<=m;i++){
sumw+=w2[i];
for(int c=min(m1,sumw);c>=w2[i];c--){
f2[c]=max(f2[c],f2[c-w2[i]]+v2[i]);
}
}
ll mx=0;
for (int i=1;i<=min(m1,m2);i++){
mx=max(f1[i]+f2[i],mx);
}
printf("%lld\n",mx);
}
return 0;
}
1004.Deliver the Cake
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>P;
const int mod=1e9+7;
const ll inf=1e16;
const int N=1e5+10;
ll n,m,k;
char sss[N];
ll ss[N];
ll c[N][3];
vector<P>v[N];
ll dijkstra(ll x,ll y){
priority_queue< P , vector<P> , greater<P> > q;
for (int i=1;i<=n;i++)for (int j=1;j<=2;j++)c[i][j]=inf;
if (ss[x]!=0)q.push({0*1ll,x}),c[x][ss[x]]=0;
else q.push({0*1ll,x}),c[x][2]=0,c[x][1]=0;
while(!q.empty()){
P r=q.top();
q.pop();
int u=r.second;
if (c[u][1]<r.first&&c[u][2]<r.first)continue;
for (auto i:v[u]){
ll to=i.first,d=i.second;
if (ss[to]==1){
if(c[u][1]+d<c[to][1]){
c[to][1]=c[u][1]+d;
q.push({c[to][1],to});
}
if(c[u][2]+d+k<c[to][1]){
c[to][1]=c[u][2]+d+k;
q.push({c[to][1],to});
}
}
else if (ss[to]==2){
if(c[u][1]+d+k<c[to][2]){
c[to][2]=c[u][1]+d+k;
q.push({c[to][2],to});
}
if(c[u][2]+d<c[to][2]){
c[to][2]=c[u][2]+d;
q.push({c[to][2],to});
}
}
else if (ss[to]==0){
if(c[u][1]+d<c[to][1]){
c[to][1]=c[u][1]+d;
q.push({c[to][1],to});
}
if(c[u][2]+d+k<c[to][1]){
c[to][1]=c[u][2]+d+k;
q.push({c[to][1],to});
}
if(c[u][1]+d+k<c[to][2]){
c[to][2]=c[u][1]+d+k;
q.push({c[to][2],to});
}
if(c[u][2]+d<c[to][2]){
c[to][2]=c[u][2]+d;
q.push({c[to][2],to});
}
}
}
}
return min(c[y][1],c[y][2]);
}
void add(int x,int y,int z){
v[x].push_back({y,z});v[y].push_back({x,z});
}
int main(){
int T;
scanf("%d",&T);
ll s,t,x,y,z;
while (T--){
scanf("%lld%lld%lld%lld%lld",&n,&m,&s,&t,&k);
for (int i=1;i<=n;i++)v[i].clear();
scanf("%s",sss+1);
for (int i=1;i<=n;i++){
if (sss[i]=='M')ss[i]=0;
if (sss[i]=='L')ss[i]=1;
if (sss[i]=='R')ss[i]=2;
}
while (m--){
scanf("%lld%lld%lld",&x,&y,&z);
add(x,y,z);
}
printf("%lld\n",dijkstra(s,t));
}
return 0;
}
1005.Equal Sentences
1006.Fake Photo
1007.Go Running
思路:二分匹配+时间戳
const int N=2e5+10;
int n,flag;
struct node{
int x,id;
}a[N],b[N];
vector<int>v[N];
inline int rd(){//inline 会更快 register int n;放全局变量更快
int X=0,w=0; char ch=0;
while(!isdigit(ch)) {
w|=ch=='-';
ch=getchar();
}
while(isdigit(ch))
X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
bool cmp(node a,node b){
return a.x<b.x;
}
bool cmp1(node a,node b){
return a.id<b.id;
}
int f[N],used[N],match[N];
bool dfs(int x){
for (int i:v[x]){
if(used[i]!=flag){
used[i]=flag;
if(!match[i]||dfs(match[i])){
match[i]=x;
return 1;
}
}
}
return 0;
}
int main(){
int T;
T=rd();
while (T--){
n=rd();
int x,y;
for (int i=1;i<=n;i++){
x=rd();y=rd();
a[i].x=x-y;b[i].x=x+y;
a[i].id=b[i].id=i;
}
sort(a+1,a+1+n,cmp);sort(b+1,b+1+n,cmp);
int cnt=0;f[a[1].id]=++cnt;
for (int i=2;i<=n;i++){
if (a[i].x==a[i-1].x)f[a[i].id]=f[a[i-1].id];
else f[a[i].id]=++cnt;
}
int p=cnt;
f[b[1].id+n]=++cnt;
for (int i=2;i<=n;i++){
if (b[i].x==b[i-1].x)f[b[i].id+n]=f[b[i-1].id+n];
else f[b[i].id+n]=++cnt;
}
sort(a+1,a+1+n,cmp1);sort(b+1,b+1+n,cmp1);
for (int i=1;i<=n;i++)v[i].clear();
for (int i=1;i<=n;i++){
v[f[i]].push_back(f[i+n]);v[f[i+n]].push_back(f[i]);
}
for (int i=1;i<=cnt;i++)match[i]=used[i]=0;
n=p;
int ans=0;
flag=0;
for(int i=1;i<=n;i++){
flag++;
if(dfs(i))ans++;
}
printf("%d\n",ans);
}
return 0;
}
1008.Head Maker
1009.Imperative Meeting
1010.Joyful Party
1011.Kindergarten Physics
1012.Last Problem