​D - Time to Run​

​Codeforces Round #619 (Div. 2) Editorial​

特别要考虑到只有一行或者只有一列的情况。

如果由 k 直接正向推路径的话,难免会要考虑到很多种情况,不如先得到全部走完的路径,然后再从末尾开始减,一直到 k。另外,可以对将要输出的向量进行修正,使得其中不会出现 f 为0的情况。

// Created by CAD on 2020/2/14.
#include <bits/stdc++.h>

#define fi first
#define se second
#define pis pair<int,string>
#define ll long long
using namespace std;

vector<pis> v;
void fix(){
vector<pis> cop=v;
v.clear();
for(auto i:cop)
if(i.fi)
v.push_back(i);
}
int main()
{
int n,m,k; cin>>n>>m>>k;
ll all=0;
for(int i=1;i<=n;++i){
v.push_back({m-1,"R"});
all+=v.back().fi*v.back().se.size();
if(i==1) v.push_back({m-1,"L"});
else v.push_back({m-1,"UDL"});
all+=v.back().fi*v.back().se.size();
if(i==n) v.push_back({n-1,"U"});
else v.push_back({1,"D"});
all+=v.back().fi*v.back().se.size();
}
if(all<k) return puts("NO");
else{
puts("YES");
while(all>k){
string t=v.back().se;
int cop=v.back().fi*v.back().se.size();
v.pop_back();
all-=cop;
if(all>=k) continue;
cop=k-all;
v.push_back({cop/t.size(),t});
if(cop%t.size())
v.push_back({1,t.substr(0,cop%t.size())});
all=k;
}
fix();
cout<<v.size()<<'\n';
for(auto i:v)
cout<<i.fi<<" "<<i.se<<'\n';
}
return 0;
}

以及我超级暴力的写法....

// Created by CAD on 2020/2/14.
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
ll n,m,k;cin>>n>>m>>k;
ll all=(4*n*m-2*n-2*m);
if(all<k) return puts("NO");
else {
n--,m--;
puts("YES");
if((!n)||(!m)){
ll t=n?n:m;
if(k>t) cout<<2<<'\n';
else cout<<1<<'\n';
cout<<min(t,k)<<" "<<(t==n?'D':'R')<<"\n",k-=t;
if(k>0) cout<<k<<" "<<(t==n?'U':'L')<<"\n";
return 0;
}
ll x=n*(2*m+1),y=m*(2*n+1),laz=k-x-y,ans=0;
if(k>x) {
ans+=n*3;
int cop=k;
if(cop>x+y) cop-=laz;
cop-=x;
ans+=cop/(2*n+1)*3;
cop%=(2*n+1);
if(cop>0) ans++,cop--;
if(cop>n) ans++;
if(cop>0) ans++;
if(laz>0) ans++;
if(laz>m) ans++;
}
else{
int cop=k;
ans+=k/(2*m+1)*3;
cop%=(2*m+1);
if(cop>0) ans++;
if(cop>m) ans++;
}
cout<<ans<<'\n';
if(k<=x){
ll cnt=k/(2*m+1);
for(int i=1;i<=cnt;++i)
cout<<m<<" R\n"<<m<<" L\n"<<"1 D\n";
k=k%(2*m+1);
int bj=0;
while(k>0){
if(bj==0) cout<<min(k,m)<<" R\n",k-=m;
else if(bj==1) cout<<min(k,m)<<" L\n",k-=m;
else cout<<"1 D\n",k--;
bj++;
}
}
else{
if(k>x+y) k-=laz;
for(int i=1;i<=n;++i)
cout<<m<<" R\n"<<m<<" L\n"<<"1 D\n";
k-=x;
int cnt=k/(2*n+1);
for(int i=1;i<=cnt;++i)
cout<<"1 R\n"<<n<<" U\n"<<n<<" D\n";
k=k%(2*n+1);
int bj=2;
while(k>0){
if(bj==0) cout<<min(k,n)<<" D\n",k-=n;
else if(bj==1) cout<<min(k,n)<<" U\n",k-=n;
else cout<<"1 R\n",k--;
bj--;
}
bj=0;
while(laz>0){
if(bj==0) cout<<min(laz,m)<<" L\n",laz-=m;
if(bj==1) cout<<min(laz,n)<<" U\n",laz-=n;
bj++;
}
}
}
return 0;
}

CAD加油!欢迎跟我一起讨论学习算法,