CSU 1726: 你經歷過絕望嗎?兩次! 1728: 線形逐步共聚合反應 1731: XueXX and P-P String 1733: XueXX and Chessboard
阿新 • • 發佈:2019-01-27
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define EPS 1e-6
#define INF INT_MAX / 10
#define LL long long
#define MOD 100000000
#define PI acos(-1.0)
using namespace std;
struct node
{
int x,y;
int step;
};
bool operator < (node p,node q)
{
return p.step > q.step;
}
const int maxn = 111;
int n,m;
char maze[maxn][maxn];
int dir[4][2] = {{0,1},{-1,0},{0 ,-1},{1,0}};
int sx,sy;
int bfs()
{
priority_queue<node> que;
node p;
p.x = sx,p.y = sy,p.step = 0;
que.push(p);
maze[sx][sy] = '#';
while(que.size()){
node q = que.top();
que.pop();
if(q.x == n - 1 || q.y == m - 1 || q.x == 0 || q.y == 0){
return q.step;
}
for(int i = 0;i < 4;i++){
node r;
r.x = q.x + dir[i][0];
r.y = q.y + dir[i][1];
if(0 <= r.x && r.x < n && 0 <= r.y && r.y < m && maze[r.x][r.y] != '#'){
if(maze[r.x][r.y] == '*')
r.step = q.step + 1;
else
r.step = q.step;
que.push(r);
maze[r.x][r.y] = '#';
}
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
for(int i = 0;i < n;i++)
scanf("%s",maze[i]);
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(maze[i][j] == '@'){
sx = i;
sy = j;
break;
}
}
}
printf("%d\n",bfs());
memset(maze,0,sizeof(maze));
}
return 0;
}
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
#define MAX 200005
#define MAXN 500005
#define maxnode 105
#define sigma_size 2
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt rt<<1
#define rrt rt<<1|1
#define middle int m=(r+l)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define limit 10000
//const int prime = 999983;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const ull mx = 1e9+7;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
/*****************************************************/
int a[MAX];
double dp[MAX],dp1[MAX];
int n;
double get(double x){
dp[0]=dp1[0]=0;
double ret=0;
for(int i=1;i<=n;i++){
dp[i]=max(dp[i-1]+(a[i]-x),(a[i]-x));
dp1[i]=max(dp1[i-1]+(x-a[i]),(x-a[i]));
ret=max(ret,dp[i]);
ret=max(ret,dp1[i]);
}
return ret;
}
int main(){
//freopen("test.txt","r",stdin);
int t;
cin>>t;
while(t--){
cin>>n;
int mini=INF,maxn=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
mini=min(mini,a[i]);
maxn=max(maxn,a[i]);
}
double l=mini,r=maxn;
for(int i=0;i<100;i++){
double ll=(2*l+r)/3;
double rr=(2*r+l)/3;
if(get(ll)<get(rr)) r=rr;
else l=ll;
}
printf("%.6f\n",l);
}
return 0;
}
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cassert>
#include <climits>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
string a;
int save[205];
int judge(int in1,int in2)
{
int len=in2-in1+1;
int fl=0;
if (len&1)
{
int mid=(in1+in2)/2;
int s=mid-1,e=mid+1;
while(a[s]==a[e]&&s>=in1&&e<=in2)
{
s--,e++;
}
if (s==in1-1&&e==in2+1)
return 1;
else return 0;
}
else
{
int mid=(in1+in2)/2;
int s=mid,e=mid+1;
while(a[s]==a[e]&&s>=in1&&e<=in2)
{
s--,e++;
}
if (s==in1-1&&e==in2+1)
return 1;
else return 0;
}
}
int main()
{
int o;
cin>>o;
while (o--)
{
cle(save,0);
cin>>a;
int len=a.size();
int ans=0;
for (int i=1; i<=len-2; i++)
{
for (int j=1; j<=len-3; j++)
{
if (i-j<0||i+2*j-1>=len)
break;
else
{
int a=i-j,b=i+j-1,c=i+2*j-1;
int f1=judge(a,b);
int f2=judge(i,c);
if (f1&&f2)
{
ans=max(3*j,ans);
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cassert>
#include <climits>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=-1;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
int n,m,k;
int dp[1005][1005];
const int mod=1000000007;
int main()
{
int o;
cin>>o;
while(o--)
{
cle(dp,0);
cin>>n>>m>>k;
int x,y;
rep(i,k)
{
cin>>x>>y;
dp[x][y]=INF;
}
dp[0][1]=1;
Rep(i,1,n)
{
Rep(j,1,m)
{
if(dp[i][j]==INF)continue;
if (dp[i-1][j]>=0&&dp[i][j-1]>=0)
{
dp[i][j]+=(dp[i-1][j]+dp[i][j-1]);
dp[i][j]%=mod;
}
if (dp[i-1][j]==INF&&dp[i][j-1]==INF)
{
dp[i][j]+=0;
}
if (dp[i-1][j]>=0&&dp[i][j-1]==INF)
{
dp[i][j]+=dp[i-1][j];
dp[i][j]%=mod;
}
if (dp[i-1][j]==INF&&dp[i][j-1]>=0)
{
dp[i][j]+=dp[i][j-1];
dp[i][j]%=mod;
}
}
}
if (dp[n][m]==INF)
puts("0");
else
printf("%d\n",dp[n][m]);
}
return 0;
}