D: 遥 控 器
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef unsigned long long LL;
#define met(a,b) (memset(a,b,sizeof(a)))
const int INF = 1e9+;
const int N = ;
const int MOD = ; int a[], s, e; int Slove()
{
int i, Mintime = , shi=e/, ge=e%; if(s>e)
{
if(a[])
Mintime = min(Mintime, -(s-e));
if(a[])
Mintime = min(Mintime, s-e);
}
else if(s<e)
{
if(a[])
Mintime = min(Mintime, e-s);
if(a[])
Mintime = min(Mintime, -(e-s));
}
else
Mintime = ; if(s!=e && !a[] && !a[])
{
if(shi==)
{
if(!a[ge]) Mintime = -;
}
else
{
if(!a[]) Mintime = -;
if(!a[shi]) Mintime = -;
if(!a[ge]) Mintime = -;
}
} for(i=; i<=; i++)
{
if(i==s) continue; int sh = i/, g = i%;
if(sh== && a[g])
{
if(i>e)
{
if(a[]) ///变大
Mintime = min(Mintime, -(i-e)+);
if(a[]) ///变小
Mintime = min(Mintime, (i-e)+);
}
else if(i<e)
{
if(a[])
Mintime = min(Mintime, (e-i)+);
if(a[])
Mintime = min(Mintime, -(e-i)+);
}
else Mintime = min(Mintime, );
}
else if(a[] && a[sh] && a[g])
{
if(i>=e)
{
if(a[])
Mintime = min(Mintime, -(i-e)+);
if(a[])
Mintime = min(Mintime, (i-e)+);
}
else if(i<e)
{
if(a[])
Mintime = min(Mintime, (e-i)+);
if(a[])
Mintime = min(Mintime, -(e-i)+);
}
else Mintime = min(Mintime, );
}
} if(Mintime==)
Mintime = -;
return Mintime;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
met(a, ); scanf("%d%d%d%d", &a[], &a[], &a[], &a[]);
scanf("%d%d%d%d", &a[], &a[], &a[], &a[]);
scanf("%d%d%d", &a[], &a[], &a[]);
scanf("%d%d", &a[], &a[]); scanf("%d%d", &s, &e); int ans = Slove(); printf("%d\n", ans);
}
return ;
}
/** */
G: Divideing Jewels (01背包) 代码写的有点丑
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef unsigned long long LL;
#define met(a,b) (memset(a,b,sizeof(a)))
const int INF = 1e9+;
const int N = ;
const int MOD = ; int a[], dp[N], v[N], k; void Slove() ///重点就在于怎样的把它转化为01背包
{
int i, j;
k=; for(i=; i<=; i++)
for(j=; j<=a[i]; j++)
v[k++] = i;
k--;
} int main()
{ int iCase=; while(scanf("%d%d%d%d%d%d%d%d%d%d", &a[], &a[], &a[], &a[], &a[], &a[], &a[], &a[], &a[], &a[])!=EOF)
{
int sum=, i, j; for(i=; i<=; i++)
sum += a[i]*i; if(sum==) break; met(dp, );
met(v, ); Slove(); for(i=; i<=k; i++)
for(j=sum/; j>=v[i]; j--)
{
if(j>=v[i])
dp[j] = max(dp[j], dp[j-v[i]]+v[i]);
} if(sum% || dp[sum/]!=sum/)
printf("#%d:Can't be divided.\n\n", iCase++);
else
printf("#%d:Can be divided.\n\n", iCase++); } return ;
}