@KirinBill
2017-10-12T22:06:25.000000Z
字数 4562
阅读 1187
题解
套题
目录
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <algorithm>
#include <cstring>
#include <climits>
#include <queue>
#include <vector>
#include <functional>
using std::min;
using std::priority_queue;
using std::vector;
using std::greater;
const int MAXN=105;
int n,t;
int G[MAXN][MAXN];
int dirx[]={0,1,0,-1},diry[]={1,0,-1,0};
struct state{
int x,y,k;
long long d;
state(int x=0,int y=0,int k=0,long long d=0):x(x),y(y),k(k),d(d){}
friend bool operator> (const state &a,const state &b){
return a.d>b.d;
}
};
inline bool inG(state &u){
return 1<=u.x && u.x<=n && 1<=u.y && u.y<=n;
}
inline long long Dijkstra(){
static long long his[MAXN][MAXN][3];
static priority_queue<state,vector<state>,greater<state> > hp;
memset(his,0x7f,sizeof(his));
his[1][1][0]=0;
hp.push(state(1,1,0,0));
state u,v;
long long d;
while(hp.size()){
u=hp.top();
hp.pop();
if(his[u.x][u.y][u.k]<d) continue;
v.k=(u.k+1)%3;
for(int i=0;i<4;++i){
v.x=u.x+dirx[i],v.y=u.y+diry[i];
v.d=u.d+t;
if(v.k==0) v.d+=G[v.x][v.y];
if(inG(v) && his[v.x][v.y][v.k]>v.d){
his[v.x][v.y][v.k]=v.d;
hp.push(v);
}
}
}
long long ret=INT_MAX;
for(int i=0;i<3;++i)
ret=min(ret,his[n][n][i]);
return ret;
}
int main(){
#ifdef DEBUG
setIO("a");
#endif
read(n),read(t);
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j)
read(G[i][j]);
}
write(Dijkstra());
return 0;
}
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <algorithm>
#include <cmath>
using std::max;
using std::abs;
const int MAXN=1005;
int n;
int x[MAXN],y[MAXN];
bool can[MAXN][MAXN];
inline int DP(){
static int dp[MAXN][MAXN][2];
int ret=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
dp[i][j][0]=max(max(dp[i-1][j][0],dp[i][j-1][0]),max(dp[i-1][j][1],dp[i][j-1][1])-1);
dp[i][j][0]=max(dp[i][j][0],max(dp[i-1][j-1][0],dp[i-1][j-1][1]));
if(can[i][j]) dp[i][j][1]=dp[i][j][0]+1;
ret=max(ret,max(dp[i][j][0],dp[i][j][1]));
}
}
return ret;
}
int main(){
#ifdef DEBUG
setIO("b");
#endif
read(n);
for(int i=1;i<=n;++i)
read(x[i]);
for(int i=1;i<=n;++i)
read(y[i]);
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j)
can[i][j]=(abs(x[i]-y[j])<=4);
}
write(DP());
return 0;
}
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
const int MAXN=50005;
int n,m;
int last[MAXN];
class BIT{
private:
long long c[MAXN<<1];
int lowbit(int x){return x&-x;}
long long qry(int r){
long long ret=0;
for(;r;r-=lowbit(r))
ret+=c[r];
return ret;
}
public:
void add(int l,int x){
for(;l<=m;l+=lowbit(l))
c[l]+=x;
}
long long qry(int l,int r){
return qry(r)-qry(l-1);
}
}ta;
int main(){
#ifdef DEBUG
setIO("c");
#endif
read(n);
m=n<<1;
long long ans=0;
for(int i=1,a;i<=m;++i){
read(a);
if(last[a]){
ans+=ta.qry(last[a]+1,i);
ta.add(last[a],-1);
}
else{
last[a]=i;
ta.add(i,1);
}
}
write(ans);
return 0;
}