commit
1bdb3c4e35
|
@ -0,0 +1,39 @@
|
|||
/** SPFA 单源最短路径算法 不支持负环*/
|
||||
namespace SPFA
|
||||
{
|
||||
const int MAXN = 1005;
|
||||
int d[MAXN];/// distance [ From S to ... ]
|
||||
int v[MAXN];/// visit
|
||||
int q[MAXN];/// 基于数组的队列(也可用queue等...)
|
||||
int mp[MAXN][MAXN]; /// mp[i][j] i<--> j is connected.
|
||||
int n;/// n is the number of max Point .
|
||||
|
||||
void spfa(int StartPoint) /// d[i] is the min distance from StartPoint to i ( Both >=1 )
|
||||
{
|
||||
memset(d,0x3f,sizeof(d));
|
||||
memset(v,0,sizeof(v));
|
||||
/*
|
||||
for(int i=1;i<MAXN;i++)
|
||||
d[i]=INF,v[i]=0;*/
|
||||
int cnt=0;
|
||||
q[cnt++]=StartPoint;
|
||||
v[StartPoint]=1;
|
||||
d[StartPoint]=0;
|
||||
while(cnt>0)
|
||||
{
|
||||
int c=q[--cnt];
|
||||
v[c]=0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
/// Here : if your mp[i][j] use INF as infinite, then use mp[c][i]!=INF.
|
||||
/// Or you may use mp[i][j]!=-1 && d[i] > d[c] + mp[c][i]
|
||||
if( mp[c][i]!=INF && d[i]>d[c]+mp[c][i] )
|
||||
{
|
||||
d[i]=d[c]+mp[c][i];
|
||||
if(!v[i]) v[i]=1,q[cnt++]=i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}/// End of NameSpace SPFA
|
|
@ -0,0 +1,83 @@
|
|||
#include <windows.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
|
||||
char clipbuffer[1024];
|
||||
char uname[1024];
|
||||
char cmessage[1024];
|
||||
char fname[1024];
|
||||
int tid;
|
||||
void GetClipData()
|
||||
{
|
||||
OpenClipboard(NULL);
|
||||
HANDLE hClipboardData=GetClipboardData(CF_TEXT);
|
||||
char* pchData=(char*)GlobalLock(hClipboardData);
|
||||
strcpy(clipbuffer,pchData);
|
||||
GlobalUnlock(hClipboardData);
|
||||
CloseClipboard();
|
||||
}
|
||||
|
||||
void SplitData()
|
||||
{
|
||||
char* p=strstr(clipbuffer,"://")+3;
|
||||
p=strstr(p,"/");
|
||||
char* q=strstr(p+1,"/");
|
||||
strncpy(uname,p+1,q-p-1);
|
||||
sprintf(cmessage,"From %s",clipbuffer);
|
||||
}
|
||||
|
||||
void GetFileNum()
|
||||
{
|
||||
printf("Please input Problem ID:\n");
|
||||
scanf("%d%*c",&tid);
|
||||
sprintf(fname,"%d_%s.cpp",tid,uname);
|
||||
}
|
||||
|
||||
void SetClip(const char* inc)
|
||||
{
|
||||
OpenClipboard(NULL);
|
||||
EmptyClipboard();
|
||||
HGLOBAL hClipboardData;
|
||||
hClipboardData=GlobalAlloc(GMEM_DDESHARE,strlen(inc)+1);
|
||||
char* pchData;
|
||||
pchData=(char*)GlobalLock(hClipboardData);
|
||||
strcpy(pchData,inc);
|
||||
GlobalUnlock(hClipboardData);
|
||||
SetClipboardData(CF_TEXT,hClipboardData);
|
||||
CloseClipboard();
|
||||
}
|
||||
|
||||
void PrintFileName()
|
||||
{
|
||||
SetClip(fname);
|
||||
printf("File Name Set.\n");
|
||||
}
|
||||
void PrintCommentMessage()
|
||||
{
|
||||
SetClip(cmessage);
|
||||
printf("Comment Message Set.\n");
|
||||
}
|
||||
void ShowClip()
|
||||
{
|
||||
printf("Data In Clipboard...\n");
|
||||
printf("%s\n",clipbuffer);
|
||||
}
|
||||
void GPause()
|
||||
{
|
||||
getchar();
|
||||
}
|
||||
int main()
|
||||
{
|
||||
printf("Link Start!\n");
|
||||
GetClipData();
|
||||
ShowClip();
|
||||
SplitData();
|
||||
GetFileNum();
|
||||
PrintFileName();
|
||||
GPause();
|
||||
PrintCommentMessage();
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
int f[5009];
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
int n,i;
|
||||
|
||||
while(cin>>t)
|
||||
{
|
||||
while(t--)
|
||||
{
|
||||
cin>>n;
|
||||
for(i=1;i<=n;i++)
|
||||
f[i]=i;
|
||||
int t;
|
||||
if(n<=3)
|
||||
{
|
||||
printf("1");
|
||||
for(i=2;i<=n;i++)
|
||||
printf(" %d",i);
|
||||
printf("\n");
|
||||
continue;
|
||||
}
|
||||
while(1)
|
||||
{
|
||||
int leap=0;
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
if(f[i]!=-1)
|
||||
{
|
||||
leap++;
|
||||
}
|
||||
if(leap==2)
|
||||
{
|
||||
leap=0;
|
||||
f[i]=-1;
|
||||
}
|
||||
}
|
||||
t=0;
|
||||
for(i=1;i<=n;i++)
|
||||
if(f[i]!=-1)
|
||||
t++;
|
||||
|
||||
if(t<=3)
|
||||
break;
|
||||
|
||||
leap=0;
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
if(f[i]!=-1)
|
||||
leap++;
|
||||
if(leap==3)
|
||||
{
|
||||
f[i]=-1;
|
||||
leap=0;
|
||||
}
|
||||
}
|
||||
t=0;
|
||||
for(i=1;i<=n;i++)
|
||||
if(f[i]!=-1)
|
||||
t++;
|
||||
if(t<=3)
|
||||
break;
|
||||
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
if(f[i]!=-1)
|
||||
{
|
||||
printf("%d",f[i]);
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
for(;i<=n;i++)
|
||||
if(f[i]!=-1)
|
||||
printf(" %d",f[i]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
|
||||
char buffer[1024];
|
||||
int main()
|
||||
{
|
||||
while(scanf("%s",buffer)==1)
|
||||
{
|
||||
int idt=strstr(buffer,"B")-buffer;
|
||||
int cnt=0;
|
||||
for(int i=idt-1;i>=0;i--)
|
||||
{
|
||||
if(buffer[i]=='(') cnt++;
|
||||
else cnt--;
|
||||
}
|
||||
printf("%d\n",cnt);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
#define INF 0x3f3f3f3f
|
||||
|
||||
namespace SPFA
|
||||
{
|
||||
const int MAXN = 256;
|
||||
int d[MAXN];/// distance [ From S to ... ]
|
||||
int v[MAXN];/// visit
|
||||
int q[MAXN];///
|
||||
int mp[MAXN][MAXN]; /// mp[i][j] i<--> j is connected.
|
||||
int n;
|
||||
|
||||
int spfa(int s,int t)
|
||||
{
|
||||
memset(d,0x3f,sizeof(d));
|
||||
memset(v,0,sizeof(v));
|
||||
/*
|
||||
for(int i=1;i<MAXN;i++)
|
||||
d[i]=INF,v[i]=0;*/
|
||||
int cnt=0;
|
||||
q[cnt++]=s;
|
||||
v[s]=1;
|
||||
d[s]=0;
|
||||
while(cnt>0)
|
||||
{
|
||||
int c=q[--cnt];
|
||||
v[c]=0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
if(mp[c][i]!=INF&&d[i]>d[c]+mp[c][i])
|
||||
{
|
||||
d[i]=d[c]+mp[c][i];
|
||||
if(!v[i]) v[i]=1,q[cnt++]=i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return d[t];
|
||||
}
|
||||
|
||||
}/// End of NameSpace SPFA
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace SPFA;
|
||||
int N,M;
|
||||
while(scanf("%d %d",&N,&M)==2)
|
||||
{
|
||||
memset(mp,0x3f,sizeof(mp));
|
||||
for(int i=0;i<M;i++)
|
||||
{
|
||||
int a,b,cost;
|
||||
scanf("%d %d %d",&a,&b,&cost);
|
||||
++a;++b;
|
||||
if(mp[a][b]>cost)
|
||||
{
|
||||
mp[b][a]=mp[a][b]=cost;
|
||||
}
|
||||
}
|
||||
int s,t;
|
||||
scanf("%d %d",&s,&t);
|
||||
if(s==t)
|
||||
{
|
||||
printf("0\n");continue;
|
||||
}
|
||||
++s;++t;
|
||||
SPFA::n=N;
|
||||
int ans=spfa(s,t);
|
||||
printf("%d\n",ans>=INF ? -1 : ans );
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
#define INF 0x3f3f3f3f
|
||||
|
||||
namespace SPFA
|
||||
{
|
||||
const int MAXN = 1005;
|
||||
int d[MAXN];/// distance [ From S to ... ]
|
||||
int v[MAXN];/// visit
|
||||
int q[MAXN];/// ???????(???queue?...)
|
||||
int mp[MAXN][MAXN]; /// mp[i][j] ???<--> j ?? ?1??
|
||||
int n;
|
||||
|
||||
int spfa(int s,int t)
|
||||
{
|
||||
memset(d,0x3f,sizeof(d));
|
||||
memset(v,0,sizeof(v));
|
||||
/*
|
||||
for(int i=1;i<MAXN;i++)
|
||||
d[i]=INF,v[i]=0;*/
|
||||
int cnt=0;
|
||||
q[cnt++]=1;
|
||||
v[1]=1;
|
||||
d[1]=0;
|
||||
while(cnt>0)
|
||||
{
|
||||
int c=q[--cnt];
|
||||
v[c]=0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
if(mp[c][i]!=-1&&d[i]>d[c]+mp[c][i])
|
||||
{
|
||||
d[i]=d[c]+mp[c][i];
|
||||
if(!v[i]) v[i]=1,q[cnt++]=i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return d[t];
|
||||
}
|
||||
|
||||
}/// End of NameSpace SPFA
|
||||
|
||||
bool labled[SPFA::MAXN];
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace SPFA;
|
||||
int T,S,D;
|
||||
while(scanf("%d %d %d",&T,&S,&D)==3)
|
||||
{
|
||||
memset(mp,0x3f,sizeof(mp));
|
||||
memset(labled,false,sizeof(labled));
|
||||
n=0;
|
||||
for(int i=0;i<T;i++)
|
||||
{
|
||||
int a,b,t;
|
||||
scanf("%d %d %d",&a,&b,&t);
|
||||
mp[a+1][b+1]=mp[b+1][a+1]=min(mp[a+1][b+1],t);
|
||||
n=max(n,max(a+1,b+1));
|
||||
}
|
||||
for(int i=0;i<S;i++)
|
||||
{
|
||||
int x;
|
||||
scanf("%d",&x);
|
||||
mp[1][x+1]=mp[x+1][1]=0;
|
||||
}
|
||||
spfa(1,1);
|
||||
int mintime=INF;
|
||||
for(int i=0;i<D;i++)
|
||||
{
|
||||
int x;
|
||||
scanf("%d",&x);
|
||||
mintime=min(mintime,d[x+1]);
|
||||
}
|
||||
printf("%d\n",mintime);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#define INF 0x3f3f3f3f
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace SPFA
|
||||
{
|
||||
const int MAXN = 110;
|
||||
int d[MAXN];/// distance
|
||||
int v[MAXN];/// visit
|
||||
int q[MAXN];/// 基于数组的队列(也可用queue等...)
|
||||
int n;///点总数
|
||||
int m;///边总数
|
||||
int mp[MAXN][MAXN]; /// mp[i][j] 表示i<--> j 连通 从1开始
|
||||
|
||||
int spfa(int s,int t)
|
||||
{
|
||||
for(int i=1;i<=n;i++)
|
||||
d[i]=INF,v[i]=0;
|
||||
int cnt=0;
|
||||
q[cnt++]=1;
|
||||
v[1]=1;
|
||||
d[1]=0;
|
||||
while(cnt>0)
|
||||
{
|
||||
int c=q[--cnt];
|
||||
v[c]=0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
if(mp[c][i]!=-1&&d[i]>d[c]+mp[c][i])
|
||||
{
|
||||
d[i]=d[c]+mp[c][i];
|
||||
if(!v[i]) v[i]=1,q[cnt++]=i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return d[t];
|
||||
}
|
||||
|
||||
}/// End of NameSpace SPFA
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace SPFA;
|
||||
while(~scanf("%d%d",&n,&m)&&(n||m))
|
||||
{
|
||||
memset(mp,-1,sizeof(mp));
|
||||
for(int i=0;i<m;i++)
|
||||
{
|
||||
int a,b,c;
|
||||
scanf("%d%d%d",&a,&b,&c);
|
||||
mp[a][b]=mp[b][a]=c;/// 这里并没有判断a b的关系..
|
||||
}
|
||||
printf("%d\n",spfa(1,n));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
# 东北大学 NEUOJ
|
||||
[Goto Home Page](http://acm.neu.edu.cn "东北大学ACM-ICPC官网")
|
||||
[Online Judge Home Page](http://acm.neu.edu.cn/hustoj/ "Welcome to Online Judge")
|
|
@ -0,0 +1,20 @@
|
|||
import java.io.*;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Scanner;
|
||||
public class Main
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
|
||||
BigInteger a=BigInteger.ZERO;
|
||||
BigInteger b=new BigInteger("1");
|
||||
int t;
|
||||
t=sc.nextInt();
|
||||
for(int s=0; s<t; s++)
|
||||
{
|
||||
a=sc.nextBigInteger();
|
||||
a=a.subtract(b);
|
||||
System.out.println(a);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
#include<iostream>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
|
||||
int d[100][100]; //d[i][j]表示输入字符串从下标i到下标j至少需要加的括号数
|
||||
int c[100][100]={-1}; //c[i][j]表示从下标i到下标j的子串分割的下标,-1表示不分割
|
||||
int len; //输入括号串的长度
|
||||
string s; //输入的括号串
|
||||
|
||||
void dp()
|
||||
{
|
||||
int i,j,k,l;
|
||||
int min;
|
||||
|
||||
for(i=0; i<len; i++) d[i][i]=1;
|
||||
|
||||
for(l=1; l<len; ++l)
|
||||
for(i=0; i+l<len; ++i)
|
||||
{
|
||||
j=i+l;
|
||||
min=d[i][i]+d[i+1][j];
|
||||
c[i][j]=i;
|
||||
for(k=i+1; k<j; k++)
|
||||
{
|
||||
if(d[i][k]+d[k+1][j]<min)
|
||||
{
|
||||
min=d[i][k]+d[k+1][j];
|
||||
c[i][j]=k;
|
||||
}
|
||||
}
|
||||
d[i][j]=min;
|
||||
|
||||
if(s[i]=='(' && s[j]==')' || s[i]=='[' && s[j]==']')
|
||||
{
|
||||
if(d[i+1][j-1] < min)
|
||||
{
|
||||
d[i][j]=d[i+1][j-1];
|
||||
c[i][j]=-1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print(int i, int j) //打印结果
|
||||
{
|
||||
if(i>j) return;
|
||||
if(i==j)
|
||||
{
|
||||
if(s[i]=='(' || s[i]==')') cout<<"()";
|
||||
else cout<<"[]";
|
||||
}
|
||||
else
|
||||
{
|
||||
if(c[i][j]>=0) //从i到j从c[i][j]处分割
|
||||
{
|
||||
print(i, c[i][j]);
|
||||
print(c[i][j]+1, j);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(s[i]=='(')
|
||||
{
|
||||
cout<<"(";
|
||||
print(i+1, j-1);
|
||||
cout<<")";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"[";
|
||||
print(i+1, j-1);
|
||||
cout<<"]";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
cin>>s; //输入括号序列
|
||||
|
||||
len=s.size();
|
||||
dp();
|
||||
print(0, len-1);
|
||||
cout<<endl;
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
struct node
|
||||
{
|
||||
int n,v;
|
||||
} a[20];
|
||||
|
||||
int dp[100010];
|
||||
|
||||
int main()
|
||||
{
|
||||
int sum,n,i,j,k;
|
||||
while(~scanf("%d%d",&sum,&n))
|
||||
{
|
||||
for(i = 1; i<=n; i++)
|
||||
scanf("%d%d",&a[i].n,&a[i].v);
|
||||
if(!sum)
|
||||
{
|
||||
printf("0\n");
|
||||
continue;
|
||||
}
|
||||
if(!n)
|
||||
{
|
||||
printf("0\n");
|
||||
continue;
|
||||
}
|
||||
memset(dp,0,sizeof(dp));
|
||||
dp[0] = 1;
|
||||
int MAX = 0,tem;
|
||||
for(i = 1; i<=n; i++)
|
||||
{
|
||||
for(j = MAX;j>=0;j--)
|
||||
{
|
||||
if(dp[j])
|
||||
{
|
||||
for(k = 1;k<=a[i].n;k++)
|
||||
{
|
||||
tem = j+k*a[i].v;
|
||||
if(tem>sum)
|
||||
continue;
|
||||
dp[tem] = 1;
|
||||
if(tem>MAX)
|
||||
MAX = tem;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d\n",MAX);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
#include<cstdio>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
int a[30010], u[30010];
|
||||
int main()
|
||||
{
|
||||
int n, m, i, j, k, x, ans;
|
||||
while(~scanf("%d%d",&m,&n))
|
||||
{
|
||||
priority_queue<int, vector<int>, less<int> > que1; //队列中的元素从大到小排序
|
||||
priority_queue<int, vector<int>, greater<int> > que2; //队列中的元素从小到大排序
|
||||
for(i = 1; i <= m; i++)
|
||||
scanf("%d",&a[i]);
|
||||
for(j = 1; j <= n; j++)
|
||||
scanf("%d",&u[j]);
|
||||
i = 0;
|
||||
j = k = 1;
|
||||
while(j <= n)
|
||||
{
|
||||
if(i == u[j]) //弹出第k小的数
|
||||
{
|
||||
j++;
|
||||
if(que1.size() < k) //que1里的元素不够k个
|
||||
{
|
||||
x = que2.top();
|
||||
que1.push(x);
|
||||
que2.pop();
|
||||
}
|
||||
ans = que1.top();
|
||||
printf("%d\n",ans);
|
||||
k++; //每次弹出一个数后,k的值都要加1
|
||||
}
|
||||
else
|
||||
{
|
||||
i++;
|
||||
//que1里的元素不够k个
|
||||
if(que1.size() < k)
|
||||
{
|
||||
que2.push(a[i]);
|
||||
x = que2.top();
|
||||
que2.pop();
|
||||
que1.push(x); //先把a[i]压入que2,再从que2里取出最小值,压入que1
|
||||
}
|
||||
|
||||
//如果que1的元素达到k个,且要压入队列的值比que1中的当前最大值大,说明que1中当前的最大值并不是第k小
|
||||
else if(que1.top() > a[i])
|
||||
{
|
||||
x = que1.top();
|
||||
que1.pop();
|
||||
que2.push(x);
|
||||
que1.push(a[i]);
|
||||
}
|
||||
//que1中的元素个数达到k个,且要压入队列的值比que1中的当前最大值小,说明que1中当前的最大值就是是第k小,则把a[i]直接压入que2中
|
||||
else
|
||||
{
|
||||
que2.push(a[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAXN 1010
|
||||
#define INF 9000000
|
||||
|
||||
typedef struct platform{
|
||||
int x1;
|
||||
int x2;
|
||||
int high;
|
||||
}Platform;
|
||||
|
||||
int compare (const void * p, const void * q){
|
||||
Platform * p1 = (Platform *)p;
|
||||
Platform * q1 = (Platform *)q;
|
||||
return p1->high - q1->high;
|
||||
}
|
||||
|
||||
int Min (int a, int b){
|
||||
return (a < b) ? a : b;
|
||||
}
|
||||
|
||||
int N, X, Y, MAX;
|
||||
Platform plat[MAXN];
|
||||
int dp[MAXN][2]; //dp[i][0]、dp[i][1]分别表示从第i个平台左、右边到地面的最短时间
|
||||
|
||||
void LeftMinTime (int i){ //计算从平台i左边到地面的最短时间
|
||||
int k = i - 1;
|
||||
while (k > 0 && plat[i].high - plat[k].high <= MAX){
|
||||
//如果平台i左边下面有平台,且两者相距不超过MAX
|
||||
if (plat[i].x1 >= plat[k].x1 && plat[i].x1 <= plat[k].x2){
|
||||
dp[i][0] = plat[i].high - plat[k].high +
|
||||
Min (plat[i].x1 - plat[k].x1 + dp[k][0], plat[k].x2 - plat[i].x1 + dp[k][1]);
|
||||
return;
|
||||
}
|
||||
else
|
||||
--k;
|
||||
}
|
||||
//如果平台i左边下面没有平台,或者两者相距超过了MAX
|
||||
if (plat[i].high - plat[k].high > MAX)
|
||||
dp[i][0] = INF;
|
||||
else
|
||||
dp[i][0] = plat[i].high;
|
||||
}
|
||||
|
||||
void RightMinTime (int i){ //计算从平台i右边到地面的最短时间
|
||||
int k = i - 1;
|
||||
while (k > 0 && plat[i].high - plat[k].high <= MAX){
|
||||
//如果平台i右边下面有平台,且两者相距不超过MAX
|
||||
if (plat[i].x2 >= plat[k].x1 && plat[i].x2 <= plat[k].x2){
|
||||
dp[i][1] = plat[i].high - plat[k].high +
|
||||
Min (plat[i].x2 - plat[k].x1 + dp[k][0], plat[k].x2 - plat[i].x2 + dp[k][1]);
|
||||
return;
|
||||
}
|
||||
else
|
||||
--k;
|
||||
}
|
||||
//如果平台i右边下面没有平台,或者两者相距超过了MAX
|
||||
if (plat[i].high - plat[k].high > MAX)
|
||||
dp[i][1] = INF;
|
||||
else
|
||||
dp[i][1] = plat[i].high;
|
||||
}
|
||||
|
||||
int ShortestTime (){
|
||||
int i, j;
|
||||
|
||||
for (i=1; i<=N+1; ++i){
|
||||
LeftMinTime (i);
|
||||
RightMinTime (i);
|
||||
}
|
||||
return Min (dp[N+1][0], dp[N+1][1]);
|
||||
}
|
||||
|
||||
int main(void){
|
||||
int t;
|
||||
int i;
|
||||
while (scanf ("%d", &t) != EOF){
|
||||
while (t-- != 0){
|
||||
scanf ("%d%d%d%d", &N, &X, &Y, &MAX);
|
||||
for (i=1; i<=N; ++i){
|
||||
scanf ("%d%d%d", &plat[i].x1, &plat[i].x2, &plat[i].high);
|
||||
}
|
||||
plat[0].high = 0;
|
||||
plat[0].x1 = -20000;
|
||||
plat[0].x2 = 20000;
|
||||
plat[N+1].high = Y;
|
||||
plat[N+1].x1 = X;
|
||||
plat[N+1].x2 = X;
|
||||
//根据平台高度按从低到高排序
|
||||
qsort (plat, N+2, sizeof(Platform), compare);
|
||||
printf ("%d\n", ShortestTime());
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,210 @@
|
|||
/*************************************************************************
|
||||
> File Name: 1729.cpp
|
||||
> Author: gwq
|
||||
> Mail: gwq5210@qq.com
|
||||
> Created Time: 2015年08月11日 星期二 15时42分37秒
|
||||
************************************************************************/
|
||||
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <cctype>
|
||||
#include <climits>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
#define INF (INT_MAX / 10)
|
||||
#define clr(arr, val) memset(arr, val, sizeof(arr))
|
||||
#define pb push_back
|
||||
#define sz(a) ((int)(a).size())
|
||||
|
||||
using namespace std;
|
||||
typedef set<int> si;
|
||||
typedef vector<int> vi;
|
||||
typedef map<int, int> mii;
|
||||
typedef pair<int, int> pii;
|
||||
typedef long long ll;
|
||||
|
||||
const double esp = 1e-5;
|
||||
|
||||
#define N 35
|
||||
|
||||
struct Node;
|
||||
|
||||
int n, vis[N][N][N][N];
|
||||
int sx1, sy1, sx2, sy2, ex1, ey1, ex2, ey2, cnt;
|
||||
int dx[] = {0, -1, 0, 1};
|
||||
int dy[] = {1, 0, -1, 0};
|
||||
char direct[] = "ENWS";
|
||||
char mp[N][N];
|
||||
|
||||
int dist(int x1, int y1, int x2, int y2)
|
||||
{
|
||||
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
|
||||
}
|
||||
|
||||
struct Node {
|
||||
int x1, y1, x2, y2, dis, d1, d2, id, pre;
|
||||
Node() {}
|
||||
Node(int a, int b, int c, int d, int dd) :x1(a), y1(b), x2(c), y2(d), dis(dd) {}
|
||||
void output(void)
|
||||
{
|
||||
printf("dis = %d\n", dis);
|
||||
printf("x1 = %d, y1 = %d.\n", x1, y1);
|
||||
printf("x2 = %d, y2 = %d.\n", x2, y2);
|
||||
printf("-----------------\n");
|
||||
}
|
||||
bool check(void)
|
||||
{
|
||||
if (check1() && check2()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool check1(void)
|
||||
{
|
||||
if (x1 == ex1 && y1 == ey1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool check2(void)
|
||||
{
|
||||
if (x2 == ex2 && y2 == ey2) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool operator <(const Node v) const
|
||||
{
|
||||
return this->dis < v.dis;
|
||||
}
|
||||
}way[1000010];
|
||||
|
||||
void bfs(void)
|
||||
{
|
||||
priority_queue<Node> q;
|
||||
clr(vis, -1);
|
||||
Node v = Node(sx1, sy1, sx2, sy2, dist(sx1, sy1, sx2, sy2));
|
||||
cnt = 1;
|
||||
v.d1 = -1;
|
||||
v.d2 = -1;
|
||||
v.id = 0;
|
||||
v.pre = -1;
|
||||
q.push(v);
|
||||
way[0] = v;
|
||||
vis[sx1][sy1][sx2][sy2] = v.dis;
|
||||
while (!q.empty()) {
|
||||
Node u = q.top();
|
||||
q.pop();
|
||||
//u.output();
|
||||
if (u.check()) {
|
||||
printf("%.2f\n", sqrt(1.0 * u.dis));
|
||||
Node p = u;
|
||||
string s1, s2;
|
||||
while (p.pre != -1) {
|
||||
if (p.d1 != '*') {
|
||||
s1.pb(p.d1);
|
||||
}
|
||||
if (p.d2 != '*') {
|
||||
s2.pb(p.d2);
|
||||
}
|
||||
p = way[p.pre];
|
||||
}
|
||||
reverse(s1.begin(), s1.end());
|
||||
reverse(s2.begin(), s2.end());
|
||||
cout << s1 << endl;
|
||||
cout << s2 << endl;
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
int nx1 = u.x1 + dx[i];
|
||||
int ny1 = u.y1 + dy[i];
|
||||
int d1 = direct[i];
|
||||
if (u.check1()) {
|
||||
nx1 = u.x1;
|
||||
ny1 = u.y1;
|
||||
d1 = '*';
|
||||
}
|
||||
if (!mp[nx1][ny1] || mp[nx1][ny1] == '#' || mp[nx1][ny1] == '*') {
|
||||
;
|
||||
} else {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
int nx2 = u.x2 + dx[j];
|
||||
int ny2 = u.y2 + dy[j];
|
||||
int d2 = direct[j];
|
||||
if (u.check2()) {
|
||||
nx2 = u.x2;
|
||||
ny2 = u.y2;
|
||||
d2 = '*';
|
||||
}
|
||||
if (!mp[nx2][ny2] || mp[nx2][ny2] == '$' || mp[nx2][ny2] == '*') {
|
||||
;
|
||||
} else {
|
||||
int ndis = dist(nx1, ny1, nx2, ny2);
|
||||
ndis = min(ndis, u.dis);
|
||||
int vv = vis[nx1][ny1][nx2][ny2];
|
||||
if (vv == -1 || ndis > vis[nx1][ny1][nx2][ny2]) {
|
||||
vis[nx1][ny1][nx2][ny2] = ndis;
|
||||
v = Node(nx1, ny1, nx2, ny2, ndis);
|
||||
v.d1 = d1;
|
||||
v.d2 = d2;
|
||||
v.pre = u.id;
|
||||
v.id = cnt;
|
||||
q.push(v);
|
||||
way[cnt++] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
while (scanf("%d", &n) != EOF) {
|
||||
if (!n) {
|
||||
break;
|
||||
}
|
||||
clr(mp, 0);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
scanf("%s", mp[i] + 1);
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
if (mp[i][j] == 'H') {
|
||||
sx1 = i;
|
||||
sy1 = j;
|
||||
mp[i][j] = '$';
|
||||
} else if (mp[i][j] == 'S') {
|
||||
ex1 = i;
|
||||
ey1 = j;
|
||||
mp[i][j] = '$';
|
||||
} else if (mp[i][j] == 'h') {
|
||||
sx2 = i;
|
||||
sy2 = j;
|
||||
mp[i][j] = '#';
|
||||
} else if (mp[i][j] == 's') {
|
||||
ex2 = i;
|
||||
ey2 = j;
|
||||
mp[i][j] = '#';
|
||||
}
|
||||
}
|
||||
}
|
||||
bfs();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <deque>
|
||||
#include <list>
|
||||
using namespace std;
|
||||
#define MAXWORKER 110
|
||||
#define MAXBENCH 16005
|
||||
|
||||
#define dprintf(x,args...)
|
||||
struct _worker
|
||||
{
|
||||
int L,P,S;
|
||||
};
|
||||
bool operator < (const _worker& inca,const _worker& incb)
|
||||
{
|
||||
return inca.S<incb.S;
|
||||
}
|
||||
_worker worker[MAXWORKER];
|
||||
int dp[MAXWORKER][MAXBENCH];
|
||||
|
||||
int main()
|
||||
{
|
||||
int N,K;
|
||||
while(scanf("%d %d",&N,&K)==2)
|
||||
{
|
||||
for(int i=1;i<=K;i++)
|
||||
{
|
||||
scanf("%d %d %d",&worker[i].L,&worker[i].P,&worker[i].S);
|
||||
}
|
||||
|
||||
sort(worker+1,worker+K+1);
|
||||
/// DP Init
|
||||
memset(dp,0,sizeof(dp));
|
||||
|
||||
/*
|
||||
/// Enumerate By Worker (K)
|
||||
for(int i=0;i<K;i++)
|
||||
{
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
/// dp[i][j] means the MAX income while
|
||||
/// i worker have operated j benches.
|
||||
for(int i=1;i<=K;i++)
|
||||
{
|
||||
dprintf(">>>>>>> i=%d\n",i);
|
||||
deque<int> pbus;
|
||||
pbus.push_back(max(0,worker[i].S-worker[i].L));
|
||||
for(int j=1;j<=N;j++)
|
||||
{
|
||||
dprintf(">>> j=%d (i=%d)\n",j,i);
|
||||
|
||||
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
|
||||
|
||||
/// i-th Worker cannot reach j-th bench
|
||||
/// !!! Must include 'equal' (=) !!!
|
||||
if(worker[i].L+worker[i].S<=j) continue;
|
||||
|
||||
while(!pbus.empty()&&pbus.front()+worker[i].L<j) pbus.pop_front();
|
||||
|
||||
if(j<worker[i].S)
|
||||
{
|
||||
while(!pbus.empty()&&
|
||||
dp[i-1][pbus.back()]-pbus.back()*worker[i].P
|
||||
<
|
||||
dp[i-1][j]-j*worker[i].P
|
||||
)
|
||||
pbus.pop_back();
|
||||
pbus.push_back(j);
|
||||
continue;
|
||||
}
|
||||
|
||||
dp[i][j]=max(dp[i][j],dp[i-1][pbus.front()]+worker[i].P*(j-pbus.front()));
|
||||
|
||||
/*
|
||||
/// Output DP
|
||||
for(int pi=1;pi<=K;pi++)
|
||||
{
|
||||
for(int pj=1;pj<=N;pj++) printf("%d ",dp[pi][pj]);
|
||||
printf("\n");
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
printf("%d\n",dp[K][N]);
|
||||
|
||||
}/// End of Main Loop
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
#include<cstring>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int dp[110][16000+50];
|
||||
|
||||
struct person
|
||||
{
|
||||
int l,p,s;
|
||||
}P[110];
|
||||
int cmp(person p1,person p2)
|
||||
{
|
||||
return p1.s<p2.s;
|
||||
}
|
||||
|
||||
int Q[16000+50];
|
||||
int main()
|
||||
{
|
||||
int N,K;
|
||||
while(scanf("%d%d",&N,&K)!=EOF)
|
||||
{
|
||||
for(int i=1;i<=K;i++)
|
||||
scanf("%d%d%d",&P[i].l,&P[i].p,&P[i].s);
|
||||
sort(P+1,P+K+1,cmp);
|
||||
|
||||
int front,rear;
|
||||
memset(dp,0,sizeof(dp));
|
||||
int ans=0;
|
||||
for(int i=1;i<=K;i++)
|
||||
{
|
||||
front=0;rear=1;
|
||||
Q[0]=max(P[i].s-P[i].l,0);
|
||||
for(int j=1;j<=N;j++)
|
||||
{
|
||||
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
|
||||
if(j>=P[i].s+P[i].l)//这些木块涂不了
|
||||
continue;
|
||||
while(front<rear&&Q[front]+P[i].l<j)//队中的k过小,出队
|
||||
front++;
|
||||
if(j<P[i].s)//符合k的取值范围即可以考虑入队
|
||||
{
|
||||
int temp=dp[i-1][j]-j*P[i].p;
|
||||
while(front<rear&&dp[i-1][Q[rear-1]]-Q[rear-1]*P[i].p<temp)//更优的k出现,队尾出队
|
||||
rear--;
|
||||
Q[rear++]=j;
|
||||
continue;//第i个人无法涂这些木块
|
||||
}
|
||||
dp[i][j]=max(dp[i][j],dp[i-1][Q[front]]+P[i].p*(j-Q[front]));
|
||||
}
|
||||
}
|
||||
printf("%d\n",dp[K][N]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
#define N 120
|
||||
#define M 17000
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct RE
|
||||
{
|
||||
int l,p,s;
|
||||
} re[N];
|
||||
|
||||
int q[M],l[N],r[N],dp[N][17000],n,k;
|
||||
|
||||
inline bool cmp(const RE &a,const RE &b)
|
||||
{
|
||||
return a.s<b.s;
|
||||
}
|
||||
|
||||
void read()
|
||||
{
|
||||
for(int i=1; i<=k; i++)
|
||||
scanf("%d%d%d",&re[i].l,&re[i].p,&re[i].s);
|
||||
sort(re+1,re+1+k,cmp);
|
||||
|
||||
for(int i=1; i<=k; i++)
|
||||
{
|
||||
l[i]=max(0,re[i].s-re[i].l);
|
||||
r[i]=min(n,re[i].s+re[i].l-1);
|
||||
}
|
||||
}
|
||||
|
||||
void go()
|
||||
{
|
||||
for(int i=0; i<=n; i++) dp[0][i]=0;
|
||||
for(int i=1; i<=k; i++)
|
||||
{
|
||||
for(int j=0; j<re[i].s; j++) dp[i][j]=dp[i-1][j]; //第i个粉刷匠不刷任何墙
|
||||
int h=0,t=0;
|
||||
for(int j=l[i],tmp; j<re[i].s; j++) //将dp[i-1]层的最优状态存入单调队列
|
||||
{
|
||||
tmp=dp[i-1][j]-j*re[i].p;
|
||||
while(t>h&&dp[i-1][q[t-1]]-q[t-1]*re[i].p<=tmp) t--;
|
||||
q[t++]=j;
|
||||
}
|
||||
for(int j=re[i].s,tmp; j<=r[i]; j++)
|
||||
{
|
||||
while(t>h&&j-q[h]>re[i].l) h++;//弹出不在范围中的元素
|
||||
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
|
||||
dp[i][j]=max(dp[i][j],dp[i-1][q[h]]+(j-q[h])*re[i].p);
|
||||
}
|
||||
for(int j=r[i]+1; j<=n; j++) dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
|
||||
}
|
||||
int ans=0;
|
||||
for(int i=1; i<=n; i++) ans=max(ans,dp[k][i]);
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d%d",&n,&k)!=EOF)
|
||||
{
|
||||
read();
|
||||
go();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
/*==========================================================*\
|
||||
| 划分树
|
||||
\*==========================================================*/
|
||||
#define MAX_SIZE 100005
|
||||
int sorted[MAX_SIZE];//已经排好序的数据
|
||||
int toleft[25][MAX_SIZE];
|
||||
int tree[25][MAX_SIZE];
|
||||
void build_tree(int left, int right, int deep)
|
||||
{
|
||||
int i;
|
||||
if (left == right) return ;
|
||||
int mid = (left + right) >> 1;
|
||||
int same = mid - left + 1; //位于左子树的数据
|
||||
for (i = left; i <= right; ++i) {//计算放于左子树中与中位数相等的数字个数
|
||||
if (tree[deep][i] < sorted[mid]) {
|
||||
--same;
|
||||
}
|
||||
}
|
||||
int ls = left;
|
||||
int rs = mid + 1;
|
||||
for (i = left; i <= right; ++i) {
|
||||
int flag = 0;
|
||||
if ((tree[deep][i] < sorted[mid]) || (tree[deep][i] == sorted[mid] && same > 0)) {
|
||||
flag = 1;
|
||||
tree[deep + 1][ls++] = tree[deep][i];
|
||||
if (tree[deep][i] == sorted[mid])
|
||||
same--;
|
||||
} else {
|
||||
tree[deep + 1][rs++] = tree[deep][i];
|
||||
}
|
||||
toleft[deep][i] = toleft[deep][i - 1]+flag;
|
||||
}
|
||||
build_tree(left, mid, deep + 1);
|
||||
build_tree(mid + 1, right, deep + 1);
|
||||
}
|
||||
int query(int left, int right, int k, int L, int R, int deep)
|
||||
{
|
||||
if (left == right)
|
||||
return tree[deep][left];
|
||||
int mid = (L + R) >> 1;
|
||||
int x = toleft[deep][left - 1] - toleft[deep][L - 1];//位于left左边的放于左子树中的数字个数
|
||||
int y = toleft[deep][right] - toleft[deep][L - 1];//到right为止位于左子树的个数
|
||||
int ry = right - L - y;//到right右边为止位于右子树的数字个数
|
||||
int cnt = y - x;//[left,right]区间内放到左子树中的个数
|
||||
int rx = left - L - x;//left左边放在右子树中的数字个数
|
||||
if (cnt >= k) {
|
||||
//printf("sss %d %d %d\n", xx++, x, y);
|
||||
return query(L + x, L + y - 1, k, L, mid, deep + 1);
|
||||
}
|
||||
else {
|
||||
//printf("qqq %d %d %d\n", xx++, x, y);
|
||||
return query(mid + rx + 1, mid + 1 + ry, k - cnt, mid + 1, R, deep + 1);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int m, n;
|
||||
int a, b, k;
|
||||
int i;
|
||||
while (scanf("%d%d", &m, &n) == 2) {
|
||||
for (i = 1; i <= m; ++i) {
|
||||
scanf("%d", &sorted[i]);
|
||||
tree[0][i] = sorted[i];
|
||||
}
|
||||
sort(sorted + 1, sorted + 1 + m);
|
||||
build_tree(1, m, 0);
|
||||
for (i = 0; i < n; ++i) {
|
||||
scanf("%d%d%d", &a, &b, &k);
|
||||
printf("%d\n", query(a, b, k, 1, m, 0));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
|
||||
void getfill(char* s,int L,int* f)
|
||||
{
|
||||
//memset(f,0,sizeof(f)); //根据其前一个字母得到
|
||||
for(int i=1;i<L;i++)
|
||||
{
|
||||
int j=f[i];
|
||||
while(j && s[i]!=s[j])
|
||||
j=f[j];
|
||||
f[i+1]=(s[i]==s[j])?j+1:0;
|
||||
}
|
||||
}
|
||||
|
||||
#define MAXLEN 1000100
|
||||
int next[MAXLEN];
|
||||
char buffer[MAXLEN];
|
||||
int main()
|
||||
{
|
||||
while(scanf("%s",buffer)==1)
|
||||
{
|
||||
if(buffer[0]=='.') break;
|
||||
memset(next,0,sizeof(next));
|
||||
int len=strlen(buffer);
|
||||
getfill(buffer,len,next);
|
||||
if(len%(len-next[len])==0)
|
||||
{
|
||||
printf("%d\n",len/(len-next[len]));
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("1\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
struct Point {
|
||||
double x, y;
|
||||
} P[20010], o;
|
||||
|
||||
int cmp(Point a, Point b) {
|
||||
if(a.x == b.x)
|
||||
return a.y < b.y;
|
||||
return a.x < b.x;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
int T;
|
||||
scanf("%d", &T);
|
||||
while(T--) {
|
||||
bool flag = true;
|
||||
scanf("%d", &n);
|
||||
for(int i = 1; i <= n; ++i) {
|
||||
scanf("%lf%lf", &P[i].x, &P[i].y);
|
||||
}
|
||||
sort(P+1, P+n+1, cmp);
|
||||
o.x = (P[1].x+P[n].x)/2;
|
||||
o.y = (P[1].y+P[n].y)/2;
|
||||
for(int i = 2; i <= n; ++i) {
|
||||
double x = (P[i].x+P[n-i+1].x)/2;
|
||||
double y = (P[i].y+P[n-i+1].y)/2;
|
||||
if(x != o.x || y != o.y) {
|
||||
flag = false;
|
||||
//printf("%d\n", i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(flag) {
|
||||
printf("yes\n");
|
||||
}
|
||||
else {
|
||||
printf("no\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
|
||||
const int maxn = 30;
|
||||
int n, ans;
|
||||
int a[maxn][maxn];
|
||||
int dep[maxn];
|
||||
|
||||
void dfs(int id, int data)
|
||||
{
|
||||
dep[id] = 1;
|
||||
int tmp = data;
|
||||
for(int i = 1; i <= n; i ++)
|
||||
{
|
||||
if(dep[i] == 0)
|
||||
tmp += a[i][id];
|
||||
else
|
||||
tmp -= a[i][id];
|
||||
}
|
||||
if(ans < tmp)
|
||||
ans = tmp;
|
||||
for(int i = id + 1; i <= n; i ++)
|
||||
{
|
||||
if(tmp > data)
|
||||
{
|
||||
dfs(i, tmp);
|
||||
dep[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
while(~scanf("%d", &n))
|
||||
{
|
||||
for(int i = 1; i <= n; i ++)
|
||||
for(int j = 1; j <= n; j ++)
|
||||
scanf("%d", &a[i][j]);
|
||||
memset(dep, 0, sizeof(dep));
|
||||
ans = 0;
|
||||
dfs(1, 0);
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <cstring>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct node {
|
||||
int x, y;
|
||||
}q[9*9+10];
|
||||
|
||||
bool row[10][10], col[10][10], sq[4][4][10];
|
||||
int G[10][10], cnt;
|
||||
|
||||
bool dfs(int cn) {
|
||||
if(cn < 0) return true;
|
||||
|
||||
int x = q[cn].x, y = q[cn].y;
|
||||
|
||||
for(int k=1; k<=9; k++) {
|
||||
if(row[x][k] || col[y][k] || sq[x/3][y/3][k]) continue;
|
||||
row[x][k] = col[y][k] = sq[x/3][y/3][k] = true;
|
||||
|
||||
G[x][y] = k;
|
||||
if(dfs(cn-1)) return true;
|
||||
|
||||
row[x][k] = col[y][k] = sq[x/3][y/3][k] = false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int T;
|
||||
scanf("%d", &T);
|
||||
|
||||
while(T--) {
|
||||
cnt = 0;
|
||||
memset(row, false, sizeof(row));
|
||||
memset(col, false, sizeof(col));
|
||||
memset(sq, false, sizeof(sq));
|
||||
|
||||
for(int i=0; i<9; i++) {
|
||||
for(int j=0; j<9; j++) {
|
||||
scanf("%1d", &G[i][j]);
|
||||
int k = G[i][j];
|
||||
if(k != 0) {
|
||||
row[i][k] = col[j][k] = sq[i/3][j/3][k] = true;
|
||||
}
|
||||
else q[cnt++] = (node){i, j};
|
||||
}
|
||||
}
|
||||
|
||||
dfs(cnt-1);
|
||||
|
||||
for(int i=0; i<9; i++) {
|
||||
for(int j=0; j<9; j++) {
|
||||
printf("%d", G[i][j]);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#define MAXN 100005
|
||||
#define MAXM 1000005
|
||||
using namespace std;
|
||||
struct Trie
|
||||
{
|
||||
int flag;
|
||||
int next[3];
|
||||
void init()
|
||||
{
|
||||
memset(next, 0, sizeof(next));
|
||||
flag = 0;
|
||||
}
|
||||
} trie[MAXM];
|
||||
int e;
|
||||
void init()
|
||||
{
|
||||
for(int i = 0; i < MAXM; i++) trie[i].init();
|
||||
e = 0;
|
||||
}
|
||||
int getid(char c)
|
||||
{
|
||||
if(c == '*') return 0;
|
||||
else if(c == 'b') return 1;
|
||||
else return 2;
|
||||
}
|
||||
void make_trie(char s[6][6])
|
||||
{
|
||||
int u = 0, index;
|
||||
for(int i = 0; i < 4; i++)
|
||||
for(int j = 0; j < 4; j++)
|
||||
{
|
||||
index = getid(s[i][j]);
|
||||
if(!trie[u].next[index]) trie[u].next[index] = ++e;
|
||||
u = trie[u].next[index];
|
||||
}
|
||||
trie[u].flag = 1;
|
||||
}
|
||||
bool match(char s[6][6])
|
||||
{
|
||||
int u = 0, index;
|
||||
for(int i = 0; i < 4; i++)
|
||||
for(int j = 0; j < 4; j++)
|
||||
{
|
||||
index = getid(s[i][j]);
|
||||
u = trie[u].next[index];
|
||||
}
|
||||
return trie[u].flag;
|
||||
}
|
||||
char s1[6][6];
|
||||
char s2[6][6];
|
||||
char tmp[22];
|
||||
int xx[] = {0, 1, 0, -1, 1, 1, -1, -1};
|
||||
int yy[] = {1, 0, -1, 0, -1, 1, -1, 1};
|
||||
struct P
|
||||
{
|
||||
char s[6][6];
|
||||
int num;
|
||||
P() {}
|
||||
P(char _s[6][6], int _num)
|
||||
{
|
||||
num = _num;
|
||||
for(int i = 0; i < 4; i++)
|
||||
for(int j = 0; j < 4; j++)
|
||||
s[i][j] = _s[i][j];
|
||||
}
|
||||
} q[MAXM];
|
||||
bool findit(P tp)
|
||||
{
|
||||
for(int i = 0; i < 4; i++)
|
||||
for(int j = 0; j < 4; j++)
|
||||
if(tp.s[i][j] != s2[i][j]) return false;
|
||||
return true;
|
||||
}
|
||||
bool isok(int x, int y, char s[6][6])
|
||||
{
|
||||
if(x < 0 || y < 0 || x >= 4 || y >= 4 || s[x][y] != '*') return false;
|
||||
return true;
|
||||
}
|
||||
void bfs()
|
||||
{
|
||||
int ans = -1;
|
||||
int h = 0, t = 0;
|
||||
q[t++] = P(s1, 0);
|
||||
make_trie(s1);
|
||||
while(h < t)
|
||||
{
|
||||
P tp = q[h++];
|
||||
if(findit(tp))
|
||||
{
|
||||
ans = tp.num;
|
||||
break;
|
||||
}
|
||||
char fk;
|
||||
if(tp.num % 2 == 0) fk = 'w';
|
||||
else fk = 'b';
|
||||
for(int i = 0; i < 4; i++)
|
||||
for(int j = 0; j < 4; j++)
|
||||
{
|
||||
if(tp.s[i][j] == fk)
|
||||
{
|
||||
for(int k = 0; k < 8; k++)
|
||||
{
|
||||
int tx = i, ty = j;
|
||||
tx += xx[k];
|
||||
ty += yy[k];
|
||||
while(isok(tx, ty, tp.s))
|
||||
{
|
||||
tx += xx[k];
|
||||
ty += yy[k];
|
||||
}
|
||||
tx -= xx[k];
|
||||
ty -= yy[k];
|
||||
swap(tp.s[i][j], tp.s[tx][ty]);
|
||||
|
||||
if(!match(tp.s))
|
||||
{
|
||||
|
||||
|
||||
q[t++] = P(tp.s, tp.num + 1);
|
||||
make_trie(tp.s);
|
||||
}
|
||||
swap(tp.s[i][j], tp.s[tx][ty]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
scanf("%d", &T);
|
||||
while(T--)
|
||||
{
|
||||
init();
|
||||
for(int i = 0; i < 4; i++) scanf("%s", s1[i]);
|
||||
for(int i = 0; i < 4; i++) scanf("%s", s2[i]);
|
||||
bfs();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
#include<iostream>
|
||||
#include<string>
|
||||
#include<stdio.h>
|
||||
using namespace std;
|
||||
int weekday(int y,int m,int d)
|
||||
{
|
||||
|
||||
|
||||
int tm=m>=3?(m-2):(m+10);
|
||||
|
||||
|
||||
int ty=m>=3?y:(y-1);
|
||||
|
||||
|
||||
return (ty+ty/4-ty/100+ty/400+(int)(2.6*tm-0.2)+d)%7;
|
||||
}
|
||||
int preoperate()
|
||||
{
|
||||
int year,mon,da,cont=0;
|
||||
for(year=1753;year<=1753+400;year++)
|
||||
{
|
||||
for(mon=1;mon<=12;mon++)
|
||||
{
|
||||
for(da=1;da<=21;da=da+10)
|
||||
{
|
||||
if(weekday(year,mon,da)==1)
|
||||
cont++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cont;
|
||||
}
|
||||
void gotowhat(int y,int m,int d,int howmany)
|
||||
{
|
||||
int cont=howmany;
|
||||
int year,mon,da;
|
||||
year=y;
|
||||
for(mon=m;mon<=12;mon++)
|
||||
{
|
||||
for(da=(mon==m?d:1);da<=21;da=da+10)
|
||||
{
|
||||
int tag=weekday(year,mon,da);
|
||||
if(tag==1)
|
||||
cont--;
|
||||
if(cont==0)
|
||||
{
|
||||
cout<<year<<" "<<mon<<" "<<da<<endl;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(year=y+1;year<=y+500;year++)
|
||||
{
|
||||
for(mon=1;mon<=12;mon++)
|
||||
{
|
||||
for(da=1;da<=21;da=da+10)
|
||||
{
|
||||
int tag=weekday(year,mon,da);
|
||||
if(tag==1)
|
||||
cont--;
|
||||
if(cont==0)
|
||||
{
|
||||
cout<<year<<" "<<mon<<" "<<da<<endl;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t,y,m,d,number,st,ts;
|
||||
int oneturn=preoperate();
|
||||
cin>>t;
|
||||
while(t--)
|
||||
{
|
||||
cin>>y>>m>>d>>number;
|
||||
st=number%2058;
|
||||
ts=number/2058;
|
||||
y+=400*ts;
|
||||
number=number-2058*ts;
|
||||
gotowhat(y,m,d,number);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<string>
|
||||
#include<sstream>
|
||||
using namespace std;
|
||||
const char* alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
long long a;
|
||||
long long stand[15];
|
||||
|
||||
long long jia(stringstream&);
|
||||
long long cheng(stringstream&);
|
||||
|
||||
long long pow (long long a, long long b) {
|
||||
long long ans = 1;
|
||||
for (int i = 0; i < b; i++) ans *= a;
|
||||
return ans;
|
||||
}
|
||||
|
||||
bool check () {
|
||||
string line; getline(cin, line);
|
||||
for (a = 0; a < 10; a++) {
|
||||
stringstream ss(line);
|
||||
if (jia(ss) != stand[a]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main ()
|
||||
{
|
||||
int n;
|
||||
string line;
|
||||
getline(cin, line);
|
||||
for (a = 0; a < 10; a++) {
|
||||
stringstream ss(line);
|
||||
stand[a] = jia(ss);
|
||||
}
|
||||
cin >> n;
|
||||
getchar();
|
||||
for(int i = 0; i < n; i++)
|
||||
if (check()) cout << alph[i];
|
||||
return 0;
|
||||
}
|
||||
|
||||
long long pan(stringstream& ss) {
|
||||
long long num = 0;
|
||||
char c = 0;
|
||||
ss >> c;
|
||||
if (c == 'a') return (long long)a;
|
||||
else {
|
||||
ss.putback(c);
|
||||
ss >> num;
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
long long getnum(stringstream& ss) {
|
||||
long long b = 0;
|
||||
char c = 0;
|
||||
b = pan(ss);
|
||||
ss >> c;
|
||||
while (c == '^') {
|
||||
long long d;
|
||||
d = pan(ss);
|
||||
b = pow(b, d);
|
||||
c = 0;
|
||||
ss >> c;
|
||||
}
|
||||
ss.putback(c);
|
||||
return b;
|
||||
}
|
||||
|
||||
long long cheng (stringstream& ss) {
|
||||
char c = 0;
|
||||
long long ans = 0;
|
||||
long long num = 0;
|
||||
ss >> c;
|
||||
if (c == '(') ans = jia(ss);
|
||||
else {
|
||||
ss.putback(c);
|
||||
ans = getnum(ss);
|
||||
}
|
||||
while (ss >> c) {
|
||||
switch (c) {
|
||||
case ' ' :
|
||||
continue;
|
||||
case '*' :
|
||||
ss >> c;
|
||||
if (c == '(') num = jia(ss);
|
||||
else {
|
||||
ss.putback(c);
|
||||
num = getnum(ss);
|
||||
}
|
||||
ans *= num;
|
||||
break;
|
||||
default :
|
||||
ss.putback(c);
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
long long jia (stringstream& ss) {
|
||||
char c = 0;
|
||||
long long ans = 0;
|
||||
ans = cheng(ss);
|
||||
while (ss >> c) {
|
||||
switch (c) {
|
||||
case ' ' :
|
||||
continue;
|
||||
case '+' :
|
||||
ans += cheng(ss);
|
||||
break;
|
||||
case '-' :
|
||||
ans -= cheng(ss);
|
||||
break;
|
||||
case ')' :
|
||||
ss >> c;
|
||||
while (c == '^') {
|
||||
long long b;
|
||||
b = pan(ss);
|
||||
ans = pow(ans, b);
|
||||
c = 0;
|
||||
ss >> c;
|
||||
}
|
||||
ss.putback(c);
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<string>
|
||||
#include<sstream>
|
||||
using namespace std;
|
||||
const char* alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
long long a;
|
||||
long long stand[15];
|
||||
|
||||
long long jia(stringstream&);
|
||||
long long cheng(stringstream&);
|
||||
|
||||
long long pow (long long a, long long b) {
|
||||
long long ans = 1;
|
||||
for (int i = 0; i < b; i++) ans *= a;
|
||||
return ans;
|
||||
}
|
||||
|
||||
bool check () {
|
||||
string line; getline(cin, line);
|
||||
for (a = 0; a < 10; a++) {
|
||||
stringstream ss(line);
|
||||
if (jia(ss) != stand[a]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main ()
|
||||
{
|
||||
int n;
|
||||
string line;
|
||||
getline(cin, line);
|
||||
for (a = 0; a < 10; a++) {
|
||||
stringstream ss(line);
|
||||
stand[a] = jia(ss);
|
||||
}
|
||||
cin >> n;
|
||||
getchar();
|
||||
for(int i = 0; i < n; i++)
|
||||
if (check()) cout << alph[i];
|
||||
return 0;
|
||||
}
|
||||
|
||||
long long pan(stringstream& ss) {
|
||||
long long num = 0;
|
||||
char c = 0;
|
||||
ss >> c;
|
||||
if (c == 'a') return (long long)a;
|
||||
else {
|
||||
ss.putback(c);
|
||||
ss >> num;
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
long long getnum(stringstream& ss) {
|
||||
long long b = 0;
|
||||
char c = 0;
|
||||
b = pan(ss);
|
||||
ss >> c;
|
||||
while (c == '^') {
|
||||
long long d;
|
||||
d = pan(ss);
|
||||
b = pow(b, d);
|
||||
c = 0;
|
||||
ss >> c;
|
||||
}
|
||||
ss.putback(c);
|
||||
return b;
|
||||
}
|
||||
|
||||
long long cheng (stringstream& ss) {
|
||||
char c = 0;
|
||||
long long ans = 0;
|
||||
long long num = 0;
|
||||
ss >> c;
|
||||
if (c == '(') ans = jia(ss);
|
||||
else {
|
||||
ss.putback(c);
|
||||
ans = getnum(ss);
|
||||
}
|
||||
while (ss >> c) {
|
||||
switch (c) {
|
||||
case ' ' :
|
||||
continue;
|
||||
case '*' :
|
||||
ss >> c;
|
||||
if (c == '(') num = jia(ss);
|
||||
else {
|
||||
ss.putback(c);
|
||||
num = getnum(ss);
|
||||
}
|
||||
ans *= num;
|
||||
break;
|
||||
default :
|
||||
ss.putback(c);
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
long long jia (stringstream& ss) {
|
||||
char c = 0;
|
||||
long long ans = 0;
|
||||
ans = cheng(ss);
|
||||
while (ss >> c) {
|
||||
switch (c) {
|
||||
case ' ' :
|
||||
continue;
|
||||
case '+' :
|
||||
ans += cheng(ss);
|
||||
break;
|
||||
case '-' :
|
||||
ans -= cheng(ss);
|
||||
break;
|
||||
case ')' :
|
||||
ss >> c;
|
||||
while (c == '^') {
|
||||
long long b;
|
||||
b = pan(ss);
|
||||
ans = pow(ans, b);
|
||||
c = 0;
|
||||
ss >> c;
|
||||
}
|
||||
ss.putback(c);
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
Loading…
Reference in New Issue