parent
7a2a9109f5
commit
1b4d6396e4
|
@ -0,0 +1,37 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
int main()
|
||||
{
|
||||
char a[35];
|
||||
int i,c,l;
|
||||
while(gets(a)&&a[0]!='#')
|
||||
{
|
||||
c=0;
|
||||
l=strlen(a);
|
||||
for(i=0;i<=l-2;i++)
|
||||
if(a[i]=='1')
|
||||
c++;
|
||||
if(a[l-1]=='e')
|
||||
{
|
||||
if(c%2!=0)
|
||||
a[l-1]='1';
|
||||
else
|
||||
a[l-1]='0';
|
||||
}
|
||||
else
|
||||
{
|
||||
if(c%2!=0)
|
||||
a[l-1]='0';
|
||||
else
|
||||
a[l-1]='1';
|
||||
}
|
||||
for(i=0;i<l;i++)
|
||||
{
|
||||
if(i==l-1)
|
||||
printf("%c\n",a[i]);
|
||||
else
|
||||
printf("%c",a[i]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
# include <stdio.h>
|
||||
# include <math.h>
|
||||
int r, x, y ;
|
||||
double dist(double ax, double ay, double bx, double by)
|
||||
{
|
||||
return sqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by)) ;
|
||||
}
|
||||
void work(int cas)
|
||||
{
|
||||
double sx = x, sy = y, ex, ey, t ;
|
||||
int flag = 0 ;
|
||||
while (1)
|
||||
{
|
||||
scanf ("%lf%lf", &ex, &ey) ;
|
||||
if (ex < 0 && ey < 0) break ;
|
||||
if (flag) continue ;
|
||||
if (dist(sx, sy, ex, ey) <= r+1.0)
|
||||
{
|
||||
printf ("Firefly %d caught at (%.0lf,%.0lf)\n", cas, ex, ey) ;
|
||||
flag = 1 ;
|
||||
}
|
||||
t = r*1.0 / dist(sx, sy, ex, ey) ;
|
||||
sx = sx + (ex-sx)*t ;
|
||||
sy = sy + (ey-sy)*t ;
|
||||
}
|
||||
if (!flag) printf ("Firefly %d not caught\n", cas) ;
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
int nCase = 1 ;
|
||||
while (scanf ("%d%d%d", &r, &x, &y) && (r||x||y))
|
||||
work(nCase++) ;
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,171 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
using namespace std;
|
||||
int R,C;
|
||||
char Map[10][10];
|
||||
int Move2[6][2] = {{-1,-1},{-1,0},{0,-1},{0,1},{1,-1},{1,0}};
|
||||
int Move1[6][2] = {{-1,0},{-1,1},{0,-1},{0,1},{1,0},{1,1}};
|
||||
char Path[20];
|
||||
bool bVisited[10][10];
|
||||
int ileft,iright;
|
||||
int cnum,pos;
|
||||
inline bool InBoundary(int x,int y)
|
||||
{
|
||||
if(x >= 0 && x<R && y>=0 && y<C+x%2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
bool Evaluate(int start,int n,int &value)
|
||||
{
|
||||
stack<int> s1;
|
||||
stack<char> s2;
|
||||
int i = start;
|
||||
int t = 0;
|
||||
t = Path[i]-'0'; i++;
|
||||
if(isdigit(Path[i]))
|
||||
{
|
||||
if(t == 0) return false;
|
||||
}
|
||||
while(isdigit(Path[i]))
|
||||
{
|
||||
t*=10; t+=Path[i]-'0';
|
||||
if(t >= 100)
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
s1.push(t);
|
||||
while(1)
|
||||
{
|
||||
if(i > n-1)
|
||||
{
|
||||
if(!isdigit(Path[n-1])) return false;
|
||||
break;
|
||||
}
|
||||
if(!isdigit(Path[i]))
|
||||
{
|
||||
if(!s2.empty()) return false;
|
||||
else s2.push(Path[i]);
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
t = Path[i]-'0'; i++;
|
||||
if(isdigit(Path[i]))
|
||||
{
|
||||
if(t == 0)
|
||||
return false;
|
||||
}
|
||||
while(isdigit(Path[i]))
|
||||
{
|
||||
t*=10; t+=Path[i]-'0';
|
||||
if(t >= 100)
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
char op = s2.top();s2.pop();
|
||||
int re = s1.top();s1.pop();
|
||||
switch(op)
|
||||
{
|
||||
case '+':s1.push(re+t); break;
|
||||
case '-':s1.push(re-t);break;
|
||||
case '*':s1.push(re*t);break;
|
||||
case '/':
|
||||
if(t == 0 || re%t!=0)
|
||||
return false;
|
||||
s1.push(re/t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
value = s1.top();
|
||||
return true;
|
||||
}
|
||||
bool DFS(int i,int j,int index)
|
||||
{
|
||||
if(index == cnum)
|
||||
{
|
||||
Path[index] = 0;
|
||||
if(Evaluate(pos,index,iright) && iright == ileft)
|
||||
return true;
|
||||
else return false;
|
||||
}
|
||||
if(Map[i][j] == '=')
|
||||
{
|
||||
if(!Evaluate(0,index-1,ileft))
|
||||
return false;
|
||||
pos = index;
|
||||
}
|
||||
if(i%2==1)
|
||||
{
|
||||
for(int k = 0; k<6; k++)
|
||||
{
|
||||
int x = Move2[k][0]+i; int y = Move2[k][1]+j;
|
||||
if(InBoundary(x,y) && bVisited[x][y] == false)
|
||||
{
|
||||
bVisited[x][y] = true;
|
||||
Path[index] = Map[x][y];
|
||||
if(DFS(x,y,index+1))
|
||||
return true;
|
||||
bVisited[x][y] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int k = 0; k <6; k++)
|
||||
{
|
||||
int x = Move1[k][0]+i; int y = Move1[k][1]+j;
|
||||
if(InBoundary(x,y) && bVisited[x][y] == false)
|
||||
{
|
||||
bVisited[x][y] = true;
|
||||
Path[index] = Map[x][y];
|
||||
if(DFS(x,y,index+1))
|
||||
return true;
|
||||
bVisited[x][y] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d",&R)==1 && R!=0)
|
||||
{
|
||||
scanf("%d",&C);
|
||||
int i,j;
|
||||
cnum = C*R+R/2;
|
||||
getchar();
|
||||
for(i = 0; i<R; i++)
|
||||
{
|
||||
if(i%2 == 0)
|
||||
{
|
||||
for(j = 0; j<C; j++)
|
||||
{ scanf(" %c",&Map[i][j]); bVisited[i][j] = false;}
|
||||
getchar();
|
||||
}
|
||||
else
|
||||
{
|
||||
for(j = 0; j<=C; j++)
|
||||
{scanf("%c",&Map[i][j]); bVisited[i][j] = false;getchar();}
|
||||
}
|
||||
}
|
||||
for(i = 0; i<R; i++)
|
||||
{
|
||||
for(j = 0; j<C+i%2; j++)
|
||||
{
|
||||
if(isdigit(Map[i][j]))
|
||||
{
|
||||
bVisited[i][j] = true;
|
||||
Path[0] = Map[i][j];
|
||||
if(DFS(i,j,1)) goto PRINT;
|
||||
bVisited[i][j] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
PRINT: printf("%s\n",Path);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
int n ;
|
||||
int dp[25][2010] ;
|
||||
int p[25], sec[25] ;
|
||||
int min(int a, int b){return a<b?a:b;}
|
||||
int max(int a, int b){return a>b?a:b;}
|
||||
int gao()
|
||||
{
|
||||
int ans = 0, t = 0 ;
|
||||
int i, j ;
|
||||
for (i = 0 ; i < n ; i++)
|
||||
{
|
||||
for (j = 0 ; ; j++)
|
||||
{
|
||||
if (j != 0) dp[i][j] += dp[i][j-1] ;
|
||||
if (j > ans && dp[i][j] == 0) break ;
|
||||
if (t != 0) t-- ;
|
||||
else if (dp[i][j] != 0)
|
||||
{
|
||||
dp[i+1][j+sec[i]] += min(dp[i][j], p[i]) ;
|
||||
dp[i][j] -= min(dp[i][j], p[i]) ;
|
||||
t = sec[i]-1 ;
|
||||
ans = max(ans, j+sec[i]) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans ;
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
int m, i ;
|
||||
while (1)
|
||||
{
|
||||
scanf ("%d%d", &n, &m) ;
|
||||
if (n == 0 && m == 0) break ;
|
||||
n = -n ;
|
||||
for (i = 0 ; i < n ; i++)
|
||||
scanf ("%d%d", &p[i], &sec[i]) ;
|
||||
memset (dp, 0, sizeof(dp)) ;
|
||||
dp[0][0] = m ;
|
||||
printf ("%d\n", gao()) ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
#define MAXN 210
|
||||
#define MAXD 40010
|
||||
int N, W, H, dep[4 * MAXD], ml[4 * MAXD], cnt[4 * MAXD], len[4 * MAXD];
|
||||
struct Seg
|
||||
{
|
||||
int x, y1, y2, c;
|
||||
bool operator < (const Seg &t) const
|
||||
{
|
||||
if(x == t.x) return c < t.c;
|
||||
return x < t.x;
|
||||
}
|
||||
Seg(){}
|
||||
Seg(int _x, int _y1, int _y2, int _c) : x(_x), y1(_y1), y2(_y2), c(_c){}
|
||||
}seg[MAXN];
|
||||
void build(int cur, int x, int y)
|
||||
{
|
||||
int mid = x + y >> 1, ls = cur << 1, rs = cur << 1 | 1;
|
||||
cnt[cur] = dep[cur] = len[cur] = 0, ml[cur] = y - x + 1;
|
||||
if(x == y) return ;
|
||||
build(ls, x, mid), build(rs, mid + 1, y);
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int i, x1, x2, y1, y2;
|
||||
scanf("%d%d", &W, &H);
|
||||
for(i = 0; i < N; i ++)
|
||||
{
|
||||
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
|
||||
seg[i << 1] = Seg(x1, y1, y2, 1), seg[i << 1 | 1] = Seg(x2, y1, y2, -1);
|
||||
}
|
||||
std::sort(seg, seg + 2 * N);
|
||||
build(1, 1, H);
|
||||
}
|
||||
void update(int cur, int x, int y)
|
||||
{
|
||||
int ls = cur << 1, rs = cur << 1 | 1;
|
||||
if(dep[ls] > dep[rs]) dep[cur] = dep[ls], ml[cur] = ml[ls];
|
||||
else if(dep[rs] > dep[ls]) dep[cur] = dep[rs], ml[cur] = ml[rs];
|
||||
else dep[cur] = dep[ls], ml[cur] = ml[ls] + ml[rs];
|
||||
dep[cur] += cnt[cur];
|
||||
if(cnt[cur]) len[cur] = y - x + 1;
|
||||
else len[cur] = len[ls] + len[rs];
|
||||
}
|
||||
void refresh(int cur, int x, int y, int s, int t, int c)
|
||||
{
|
||||
int mid = x + y >> 1, ls = cur << 1, rs = cur << 1 | 1;
|
||||
if(x >= s && y <= t)
|
||||
{
|
||||
cnt[cur] += c;
|
||||
if(c > 0) ++ dep[cur], len[cur] = y - x + 1;
|
||||
else
|
||||
{
|
||||
-- dep[cur];
|
||||
if(cnt[cur]) len[cur] = y - x + 1;
|
||||
else
|
||||
{
|
||||
if(x == y) len[cur] = 0;
|
||||
else len[cur] = len[ls] + len[rs];
|
||||
}
|
||||
}
|
||||
return ;
|
||||
}
|
||||
if(mid >= s) refresh(ls, x, mid, s, t, c);
|
||||
if(mid + 1 <= t) refresh(rs, mid + 1, y, s, t, c);
|
||||
update(cur, x, y);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int i, md, ans, area;
|
||||
area = md = ans = 0;
|
||||
seg[2 * N].x = seg[2 * N - 1].x;
|
||||
for(i = 0; i < 2 * N; i ++)
|
||||
{
|
||||
refresh(1, 1, H, seg[i].y1 + 1, seg[i].y2, seg[i].c);
|
||||
area += len[1] * (seg[i + 1].x - seg[i].x);
|
||||
if(dep[1] > md) md = dep[1], ans = ml[1] * (seg[i + 1].x - seg[i].x);
|
||||
else if(dep[1] == md) ans += ml[1] * (seg[i + 1].x - seg[i].x);
|
||||
}
|
||||
printf("%d %d %d\n", W * H - area, md, ans);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d", &N), N)
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
char str[110] ;
|
||||
char tab[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ',-.?" ;
|
||||
int cnt, label[1100] ;
|
||||
void gao()
|
||||
{
|
||||
int i, sp = 0 ;
|
||||
for (i = 0 ; str[i] ; i++)
|
||||
{
|
||||
if (str[i] != ' ' && sp != 0)
|
||||
label[cnt++] = ((sp&1)?0:1),
|
||||
sp = 0 ;
|
||||
else if (str[i] == ' ') sp++ ;
|
||||
}
|
||||
}
|
||||
void Print()
|
||||
{
|
||||
int i, j, buff ;
|
||||
for (i = 0 ; i < cnt ; i+= 5)
|
||||
{
|
||||
buff = 0 ;
|
||||
for (j = 0 ;j < 5 ; j++)
|
||||
{
|
||||
if (i+j>=cnt) label[i+j] = 0 ;
|
||||
buff = buff * 2 + label[i+j] ;
|
||||
}
|
||||
putchar (tab[buff]) ;
|
||||
}
|
||||
printf("\n") ;
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
gets (str) ;
|
||||
while (1)
|
||||
{
|
||||
cnt = 0 ;
|
||||
if (strcmp(str, "#") == 0) break ;
|
||||
while (1)
|
||||
{
|
||||
if (strcmp(str, "*") == 0) break ;
|
||||
gao() ;
|
||||
gets (str) ;
|
||||
}
|
||||
Print() ;
|
||||
gets (str) ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
int main()
|
||||
{
|
||||
int i,j,len,flag[28],rear[300];
|
||||
int top;
|
||||
char str[5][74];
|
||||
while(gets(str[1]))
|
||||
{
|
||||
gets(str[2]);
|
||||
gets(str[3]);
|
||||
gets(str[4]);
|
||||
memset(flag,0,sizeof(flag));
|
||||
for(i=1;i<=4;i++)
|
||||
{
|
||||
len=strlen(str[i]);
|
||||
for(j=0;j<len;j++)
|
||||
{
|
||||
if(str[i][j]>='A'&&str[i][j]<='Z')
|
||||
{
|
||||
flag[str[i][j]-'A']++;
|
||||
}
|
||||
}
|
||||
}
|
||||
top=0;
|
||||
for(i=0;i<26;i++)
|
||||
{
|
||||
if(top<flag[i])
|
||||
{
|
||||
top=flag[i];
|
||||
}
|
||||
}
|
||||
memset(rear,0,sizeof(rear));
|
||||
for(i=top;i>0;i--)
|
||||
{
|
||||
for(j=25;j>=0;j--)
|
||||
{
|
||||
if(flag[j]>=i)
|
||||
{
|
||||
rear[i]=j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for(i=top;i>0;i--)
|
||||
{
|
||||
for(j=0;j<26&&j<=rear[i];j++)
|
||||
{
|
||||
if(flag[j]>=i)
|
||||
{
|
||||
printf("*");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
if(j<25&&j<rear[i])
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
for(i=0;i<25;i++)
|
||||
{
|
||||
printf("%c ",i+'A');
|
||||
}
|
||||
printf("Z\n");
|
||||
}
|
||||
return (0);
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
long long dp[1000001];
|
||||
int main()
|
||||
{
|
||||
int n,i,j;
|
||||
while(scanf("%d",&n)!=EOF)
|
||||
{
|
||||
memset(dp,0,sizeof(dp)); dp[0]=1;
|
||||
for(i=1;i<=n;i*=2)
|
||||
{
|
||||
for(j=i;j<=n;j++)
|
||||
{
|
||||
dp[j]+=dp[j-i];
|
||||
dp[j]%=1000000000;
|
||||
}
|
||||
}
|
||||
printf("%lld\n",dp[n]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#include<iostream>
|
||||
#include<cmath>
|
||||
using namespace std;
|
||||
#define MAX 2270
|
||||
int data[MAX];
|
||||
int j;
|
||||
void Judge(int a)
|
||||
{
|
||||
int i;
|
||||
int tmp=sqrt(double(a))+1;
|
||||
for(i=2;i<=tmp;i++)
|
||||
{
|
||||
if(a%i==0)
|
||||
return;
|
||||
}
|
||||
data[j]=a;
|
||||
j++;
|
||||
return;
|
||||
}
|
||||
void Set()
|
||||
{
|
||||
int i;
|
||||
j=1;
|
||||
data[0]=1;
|
||||
data[1]=2;
|
||||
j++;
|
||||
for(i=3;i<=20010;i++)
|
||||
Judge(i);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
Set();
|
||||
int n,tmp,Max,result,num,i;
|
||||
while(cin>>n)
|
||||
{
|
||||
result=0;
|
||||
while(n--)
|
||||
{
|
||||
cin>>tmp;
|
||||
for(i=0;i<2263;i++)
|
||||
{
|
||||
if(data[i]==tmp||(data[i]<tmp&&tmp%data[i]==0))
|
||||
Max=data[i];
|
||||
if(data[i]>tmp)
|
||||
break;
|
||||
}
|
||||
if(result<Max)
|
||||
{
|
||||
result=Max;
|
||||
num=tmp;
|
||||
}
|
||||
}
|
||||
cout<<num<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
#include<stdio.h>
|
||||
#include<iostream>
|
||||
#include<stdlib.h>
|
||||
#include<algorithm>
|
||||
#include<math.h>
|
||||
#include<iomanip>
|
||||
#include<time.h>
|
||||
#include<string.h>
|
||||
#include<queue>
|
||||
#include<string>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
int p[9000];
|
||||
int ans[9000];
|
||||
int main(){
|
||||
int n;
|
||||
while(~scanf("%d",&n)){
|
||||
memset(p,0,sizeof(p));
|
||||
int i,cas,t=-1;
|
||||
for(i=2;i<=n;i++){
|
||||
scanf("%d",p+i);
|
||||
}
|
||||
for(cas=1;cas<=n;cas++){
|
||||
t=-1;
|
||||
for(i=2;i<=n;i++){
|
||||
if(p[i]==0){
|
||||
t=i;
|
||||
}
|
||||
}
|
||||
if(t==-1)
|
||||
t=1;
|
||||
ans[t]=cas;
|
||||
for(i=t;i<=n;i++)
|
||||
p[i]--;
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
printf("%d\n",ans[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <cstdlib>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#define LL long long
|
||||
#define mod 1000000007
|
||||
#define inf 0x3f3f3f3f
|
||||
#define N 10010
|
||||
#define clr(a) (memset(a,0,sizeof(a)))
|
||||
using namespace std;
|
||||
int n,k,x;
|
||||
int vis[N];
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d%d",&n,&k)>0)
|
||||
{
|
||||
memset(vis,0,sizeof(vis));
|
||||
int num=0,ans=0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
scanf("%d",&x);
|
||||
if(!vis[x])
|
||||
{
|
||||
vis[x]=1;num++;
|
||||
}
|
||||
if(num==k)
|
||||
{
|
||||
memset(vis,0,sizeof(vis));
|
||||
num=0;ans++;
|
||||
}
|
||||
}
|
||||
printf("%d\n",ans+1);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
#include<iostream>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
int a[150010];
|
||||
int main()
|
||||
{
|
||||
int p,i;
|
||||
while(scanf("%d",&p)!=EOF)
|
||||
{
|
||||
memset(a,0,sizeof(a));
|
||||
for (i=1;i<=p;i++)
|
||||
scanf("%d",&a[i]);
|
||||
int s=0;
|
||||
int x=0;
|
||||
for (i=0;i<=p;i++)
|
||||
{
|
||||
if(x==0&&a[i]-a[i+1]<=0&&a[i+1]-a[i+2]>=0)
|
||||
{
|
||||
s=s+a[i+1];
|
||||
x=1;
|
||||
}
|
||||
else
|
||||
if (x==1&&a[i]-a[i+1]>=0&&a[i+1]-a[i+2]<=0)
|
||||
{
|
||||
s-=a[i+1];
|
||||
x=0;
|
||||
}
|
||||
}
|
||||
printf("%d\n",s);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
int main ()
|
||||
{
|
||||
string str;
|
||||
while ( cin >> str )
|
||||
{
|
||||
int N = str.size();
|
||||
int sum = 0, ask = 0;
|
||||
for ( int i = 1; i <= N; ++ i )
|
||||
{
|
||||
switch ( str[i-1] )
|
||||
{
|
||||
case 'X':
|
||||
sum += (10-i+1) * 10; break;
|
||||
case '?':
|
||||
ask = 10-i+1; break;
|
||||
default :
|
||||
sum += (10-i+1) * (str[i-1] - '0'); break;
|
||||
}
|
||||
}
|
||||
int pos = -1;
|
||||
for ( int i = 0; i <= 10; ++ i )
|
||||
{
|
||||
if ( ( sum + ask * i ) % 11 == 0 )
|
||||
{
|
||||
pos = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( ask != 1 && pos == 10 ) pos = -1;
|
||||
if ( pos == 10 )
|
||||
cout << 'X' << endl;
|
||||
else
|
||||
cout << pos << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
typedef __int64 ll;
|
||||
ll m;
|
||||
ll cal(ll s,ll x)
|
||||
{
|
||||
return (2*s+x-1)*x;
|
||||
}
|
||||
ll binary_search(ll x)
|
||||
{
|
||||
ll left,right,mid,tmp;
|
||||
left=1,right=m/2+1;
|
||||
while(left<=right)
|
||||
{
|
||||
mid=(left+right)>>1;
|
||||
tmp=cal(mid,x);
|
||||
if(tmp==2*m)
|
||||
return mid;
|
||||
else if(tmp<2*m)
|
||||
left=mid+1;
|
||||
else
|
||||
right=mid-1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
ll i,tmp,num,cnt=1;
|
||||
while(scanf("%I64d",&m)!=EOF)
|
||||
{
|
||||
num=0;
|
||||
for(i=2;i*i<=2*m+10;i++)
|
||||
{
|
||||
tmp=binary_search(i);
|
||||
if(tmp!=-1)
|
||||
num++;
|
||||
}
|
||||
printf("%I64d\n",num+1);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
char key[28],str[84];
|
||||
while(scanf("%s",key)!=EOF)
|
||||
{
|
||||
getchar();
|
||||
gets(str);
|
||||
int i,len;
|
||||
len=strlen(str);
|
||||
for(i=0;i<len;i++)
|
||||
{
|
||||
if(str[i]>='A'&&str[i]<='Z')
|
||||
{
|
||||
str[i]=key[str[i]-'A']-'a'+'A';
|
||||
}
|
||||
else if(str[i]>='a'&&str[i]<='z')
|
||||
{
|
||||
str[i]=key[str[i]-'a'];
|
||||
}
|
||||
}
|
||||
printf("%s\n",str);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
#include <stdio.h>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int n,m;
|
||||
int vis[200005];
|
||||
struct node
|
||||
{
|
||||
int x;
|
||||
int step;
|
||||
};
|
||||
int bfs(int n,int m)
|
||||
{
|
||||
int i;
|
||||
queue<node>q;
|
||||
node st,ed;
|
||||
st.x=n;
|
||||
st.step=0;
|
||||
vis[st.x]=1;
|
||||
q.push(st);
|
||||
while(!q.empty())
|
||||
{
|
||||
st=q.front();
|
||||
q.pop();
|
||||
if(st.x==m)
|
||||
return st.step;
|
||||
for(i=0;i<3;i++)
|
||||
{
|
||||
if(i==0)
|
||||
ed.x=st.x+1;
|
||||
if(i==1)
|
||||
ed.x=st.x-1;
|
||||
if(i==2)
|
||||
ed.x=st.x*2;
|
||||
if(vis[ed.x] || ed.x<0 ||ed.x>100000)
|
||||
continue;
|
||||
vis[ed.x]=1;
|
||||
ed.step=st.step+1;
|
||||
q.push(ed);
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(cin>>n>>m)
|
||||
{
|
||||
memset(vis,0,sizeof(vis));
|
||||
int ans=bfs(n,m);
|
||||
cout<<ans<<endl;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
char s[85];
|
||||
while(gets(s)&&s[0]!='#')
|
||||
{
|
||||
int l=strlen(s);
|
||||
for(i=0;i<l;i++)
|
||||
{
|
||||
if(s[i]==' ')
|
||||
s[i]=1;
|
||||
if(s[i]=='!')
|
||||
s[i]=2;
|
||||
if(s[i]=='$')
|
||||
s[i]=3;
|
||||
if(s[i]=='%')
|
||||
s[i]=4;
|
||||
if(s[i]=='(')
|
||||
s[i]=5;
|
||||
if(s[i]==')')
|
||||
s[i]=6;
|
||||
if(s[i]=='*')
|
||||
s[i]=7;
|
||||
}
|
||||
for(i=0;i<l;i++)
|
||||
{
|
||||
if(s[i]==1)
|
||||
printf("%%20");
|
||||
if(s[i]==2)
|
||||
printf("%%21");
|
||||
if(s[i]==3)
|
||||
printf("%%24");
|
||||
if(s[i]==4)
|
||||
printf("%%25");
|
||||
if(s[i]==5)
|
||||
printf("%%28");
|
||||
if(s[i]==6)
|
||||
printf("%%29");
|
||||
if(s[i]==7)
|
||||
printf("%%2a");
|
||||
if(s[i]!=1 &&s[i]!=2 &&s[i]!=3 &&s[i]!=4&&s[i]!=5 &&s[i]!=6 &&s[i]!=7)
|
||||
printf("%c",s[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
int aa[100000];
|
||||
void convert(int a,int b)
|
||||
{
|
||||
char s[16];
|
||||
int i=0;
|
||||
for(;i<16;i++)
|
||||
{
|
||||
int k=a%2;
|
||||
if(k==1)s[i]='1';
|
||||
else if(b%2==1) s[i]='?';
|
||||
else s[i]='0';
|
||||
a/=2;b/=2;
|
||||
}
|
||||
for(i=15;i>=0;i--)
|
||||
printf("%c",s[i]);
|
||||
printf("\n");
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int a,b,c,d;
|
||||
int top=0;
|
||||
while(true)
|
||||
{
|
||||
top=1;
|
||||
cin>>a;
|
||||
if(a==0) break;
|
||||
cin>>b>>c>>d;
|
||||
aa[0]=d;
|
||||
for(int i=0;i< 65537;i++)
|
||||
{
|
||||
aa[top]=(a%c*d%c+b%c)%c;
|
||||
d=aa[top++];
|
||||
if(d==aa[0])break;
|
||||
}
|
||||
int result=aa[0];
|
||||
int com=aa[0];
|
||||
for(int i=1;i<top&&i<=65536;i++)
|
||||
{
|
||||
result &= aa[i];
|
||||
com |=aa[i];
|
||||
}
|
||||
convert(result,com);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<string>
|
||||
#include<algorithm>
|
||||
#include<map>
|
||||
#include<queue>
|
||||
#define INF 100000000
|
||||
#define L 2520
|
||||
using namespace std;
|
||||
int N,M;
|
||||
int d[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
|
||||
int dp[25][25];
|
||||
struct Block
|
||||
{
|
||||
int a[4];
|
||||
}mp[25][25];
|
||||
int main()
|
||||
{
|
||||
int i,j,k,v;
|
||||
char c;
|
||||
while(cin>>N>>M,N||M)
|
||||
{
|
||||
memset(mp,0,sizeof(mp));
|
||||
for(i=0;i<=N;i++)
|
||||
{
|
||||
for(j=0;j<M;j++)
|
||||
{
|
||||
cin>>v>>c;
|
||||
if(v)
|
||||
v=L/v;
|
||||
if(c=='*')
|
||||
mp[i][j].a[0]=mp[i][j+1].a[1]=v;
|
||||
if(c=='>')
|
||||
mp[i][j].a[0]=v;
|
||||
if(c=='<')
|
||||
mp[i][j+1].a[1]=v;
|
||||
}
|
||||
if(i!=N)
|
||||
for(j=0;j<=M;j++)
|
||||
{
|
||||
cin>>v>>c;
|
||||
if(v)
|
||||
v=L/v;
|
||||
if(c=='*')
|
||||
mp[i][j].a[2]=mp[i+1][j].a[3]=v;
|
||||
if(c=='v')
|
||||
mp[i][j].a[2]=v;
|
||||
if(c=='^')
|
||||
mp[i+1][j].a[3]=v;
|
||||
}
|
||||
}
|
||||
for(i=0;i<=N;i++)
|
||||
for(j=0;j<=M;j++)
|
||||
dp[i][j]=INF;
|
||||
dp[0][0]=0;
|
||||
for(v=0;v<40;v++)
|
||||
for(i=0;i<=N;i++)
|
||||
for(j=0;j<=M;j++)
|
||||
for(k=0;k<4;k++)
|
||||
if(mp[i][j].a[k])
|
||||
if(dp[i+d[k][0]][j+d[k][1]]>dp[i][j]+mp[i][j].a[k])
|
||||
dp[i+d[k][0]][j+d[k][1]]=dp[i][j]+mp[i][j].a[k];
|
||||
if(dp[N][M]==INF)
|
||||
cout<<"Holiday"<<endl;
|
||||
else
|
||||
cout<<dp[N][M]<<" blips"<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
# include <stdio.h>
|
||||
# include <string.h>
|
||||
int ACL[30], out[30][2] ;
|
||||
char str[100] ;
|
||||
char entity[100], right[100] ;
|
||||
void gao(char s[])
|
||||
{
|
||||
char *p = s, op ;
|
||||
int ent, rit, i, j ;
|
||||
memset (ACL, 0, sizeof(ACL)) ;
|
||||
while (*p)
|
||||
{
|
||||
sscanf (p, "%[A-Z]%c%[a-z]%*c", entity, &op, right) ;
|
||||
p += strlen(entity) + strlen(right) + 2 ;
|
||||
for (i = 0 ; entity[i] ; i++)
|
||||
{
|
||||
ent = entity[i]-'A' ;
|
||||
for (rit = 0, j = 0 ; right[j] ; j++) rit |= (1<<(right[j]-'a')) ;
|
||||
if (op == '+') ACL[ent] |= rit ;
|
||||
else if (op == '-') ACL[ent] &= ~rit ;
|
||||
else //op == '='
|
||||
ACL[ent] = rit ;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
int nCase = 1 ;
|
||||
int i, j, cnt ;
|
||||
while (~scanf ("%s", str) && strcmp(str, "#"))
|
||||
{
|
||||
gao(str) ;
|
||||
memset (str, 0, sizeof(str)) ;
|
||||
printf ("%d:", nCase++) ;
|
||||
for (i = 0, cnt = 0 ; i < 26 ; i++) if (ACL[i])
|
||||
out[cnt][0] = i, out[cnt++][1] = ACL[i] ;
|
||||
for (i = 0 ; i < cnt ; i++)
|
||||
{
|
||||
printf ("%c", out[i][0]+'A') ;
|
||||
if (i==cnt-1 || (i<cnt-1 && out[i][1] != out[i+1][1]))
|
||||
for (j = 0 ; j < 26 ; j++) if (out[i][1] & (1<<j))
|
||||
printf ("%c", j+'a') ;
|
||||
}
|
||||
printf ("\n") ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
...=...**..#...@....:.:.:..=..
|
||||
______________________________
|
||||
11111111112222222222
|
||||
012345678901234567890123456789
|
|
@ -0,0 +1,72 @@
|
|||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
#include<list>
|
||||
using namespace std;
|
||||
class Bookshelf;
|
||||
class Book{
|
||||
public:
|
||||
Book(int _id,int _width):id(_id),width(_width){}
|
||||
int id,width;
|
||||
friend class Bookshelf;
|
||||
};
|
||||
class Bookshelf{
|
||||
public:
|
||||
Bookshelf(int w):width(w){
|
||||
occupied_width=0;
|
||||
}
|
||||
void add(const Book &book){
|
||||
shelf.push_front(book);
|
||||
occupied_width += book.width;
|
||||
while(occupied_width>width){
|
||||
occupied_width -= (shelf.back()).width;
|
||||
shelf.pop_back();
|
||||
}
|
||||
}
|
||||
void remove(const int &id){
|
||||
list<Book>::iterator i;
|
||||
for(i=shelf.begin();i!=shelf.end();i++){
|
||||
if(i->id == id) break;
|
||||
}
|
||||
if(i==shelf.end()) return;
|
||||
occupied_width -= i->width;
|
||||
shelf.erase(i);
|
||||
}
|
||||
friend ostream& operator<<(ostream &out,Bookshelf &bookshelf);
|
||||
private:
|
||||
list<Book> shelf;
|
||||
int width;
|
||||
int occupied_width;
|
||||
};
|
||||
ostream& operator<<(ostream &out, Bookshelf &bookshelf)
|
||||
{
|
||||
list<Book>::iterator i;
|
||||
for(i=(bookshelf.shelf).begin();i!=(bookshelf.shelf).end();i++)
|
||||
out<<" "<<i->id;
|
||||
return out;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int width;
|
||||
int p=1;
|
||||
char cmd;
|
||||
while(cin>>width,width!=-1)
|
||||
{
|
||||
Bookshelf bookshelf(width);
|
||||
int id,w;
|
||||
while(cin>>cmd)
|
||||
{
|
||||
if(cmd=='E') break;
|
||||
else if(cmd=='R')
|
||||
{
|
||||
cin>>id;
|
||||
bookshelf.remove(id);
|
||||
}else
|
||||
{
|
||||
cin>>id>>w;
|
||||
bookshelf.add(Book(id,w));
|
||||
}
|
||||
}
|
||||
cout<<"PROBLEM "<<p++<<":"<<bookshelf<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,380 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
const int size=23;
|
||||
const int num=251;
|
||||
int n;
|
||||
int m;
|
||||
int lastx,lasty;
|
||||
int map[size][size];
|
||||
bool link[num][num];
|
||||
int posx[]={0,-1,-2,-2,-1,1,2,2,1};
|
||||
int posy[]={0,2,1,-1,-2,-2,-1,1,2};
|
||||
typedef class chess
|
||||
{
|
||||
public:
|
||||
int color;
|
||||
int r,c;
|
||||
int connet[8];
|
||||
int pc;
|
||||
chess()
|
||||
{
|
||||
color=-1;
|
||||
pc=0;
|
||||
}
|
||||
}PEG;
|
||||
void LinePeg(PEG* peg,int i);
|
||||
bool CheckWin(PEG* peg,bool flag);
|
||||
int main(void)
|
||||
{
|
||||
while(cin>>n>>m)
|
||||
{
|
||||
if(!n && !m)
|
||||
break;
|
||||
memset(map,0,sizeof(map));
|
||||
memset(link,false,sizeof(link));
|
||||
PEG* peg=new PEG[m+1];
|
||||
for(int i=1;i<=m;i++)
|
||||
{
|
||||
int x,y;
|
||||
cin>>x>>y;
|
||||
map[x][y]=i;
|
||||
peg[i].r=x;
|
||||
peg[i].c=y;
|
||||
if(i%2)
|
||||
peg[i].color=1;
|
||||
else
|
||||
peg[i].color=0;
|
||||
if(i==m)
|
||||
{
|
||||
lastx=x;
|
||||
lasty=y;
|
||||
}
|
||||
LinePeg(peg,i);
|
||||
}
|
||||
if(CheckWin(peg,true) && !CheckWin(peg,false))
|
||||
cout<<"yes"<<endl;
|
||||
else
|
||||
cout<<"no"<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void LinePeg(PEG* peg,int i)
|
||||
{
|
||||
int color=peg[i].color;
|
||||
for(int k=1;k<=8;k++)
|
||||
{
|
||||
int r=peg[i].r+posx[k];
|
||||
int c=peg[i].c+posy[k];
|
||||
if(r>=0 && r<=n && c>=0 && c<=n)
|
||||
{
|
||||
if(map[r][c] && peg[ map[r][c] ].color==color)
|
||||
{
|
||||
switch(k)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
if(link[ map[r][c-2] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(c-3>=0 && link[ map[r][c-3] ][ map[r+1][c-1] ])
|
||||
break;
|
||||
if(c+1<=n && link[ map[r][c-1] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
if(r-1>=0)
|
||||
{
|
||||
if(link[ map[r-1][c-2] ][ map[r+1][c-1] ])
|
||||
break;
|
||||
if(link[ map[r-1][c-1] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(link[ map[r-1][c] ][ map[r+1][c-1] ])
|
||||
break;
|
||||
}
|
||||
if(r+2<=n)
|
||||
{
|
||||
if(link[ map[r+2][c-2] ][ map[r][c-1] ])
|
||||
break;
|
||||
if(link[ map[r+2][c-1] ][ map[r][c-2] ])
|
||||
break;
|
||||
if(link[ map[r+2][c] ][ map[r][c-1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 2: //60搴逛
|
||||
{
|
||||
if(link[ map[r][c-1] ][ map[r+2][c] ])
|
||||
break;
|
||||
if(r-1>=0 && link[ map[r-1][c-1] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(r+3<=n && link[ map[r+1][c-1] ][ map[r+3][c] ])
|
||||
break;
|
||||
if(c-2>=0)
|
||||
{
|
||||
if(link[ map[r][c-2] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(link[ map[r+1][c-2] ][ map[r+2][c] ])
|
||||
break;
|
||||
if(link[ map[r+2][c-2] ][ map[r+1][c] ])
|
||||
break;
|
||||
}
|
||||
if(c+1<=n)
|
||||
{
|
||||
if(link[ map[r][c-1] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r+1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(link[ map[r+1][c-1] ][ map[r+2][c+1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 3: //120搴逛
|
||||
{
|
||||
if(link[ map[r][c+1] ][ map[r+2][c] ])
|
||||
break;
|
||||
if(r-1>=0 && link[ map[r-1][c+1] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(r+3<=n && link[ map[r+1][c+1] ][ map[r+3][c] ])
|
||||
break;
|
||||
if(c-1>=0)
|
||||
{
|
||||
if(link[ map[r][c-1] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r+1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(link[ map[r+2][c-1] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
}
|
||||
if(c+2<=n)
|
||||
{
|
||||
if(link[ map[r+1][c] ][ map[r][c+2] ])
|
||||
break;
|
||||
if(link[ map[r+2][c] ][ map[r+1][c+2] ])
|
||||
break;
|
||||
if(link[ map[r+1][c] ][ map[r+2][c+2] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 4: //150搴逛
|
||||
{
|
||||
if(link[ map[r][c+2] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(c-1>=0 && link[ map[r+1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(c+3<=n && link[ map[r+1][c+1] ][ map[r][c+3] ])
|
||||
break;
|
||||
if(r-1>=0)
|
||||
{
|
||||
if(link[ map[r-1][c] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r-1][c+1] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(link[ map[r-1][c+2] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
}
|
||||
if(r+2<=n)
|
||||
{
|
||||
if(link[ map[r][c+1] ][ map[r+2][c] ])
|
||||
break;
|
||||
if(link[ map[r][c+1] ][ map[r+2][c+2] ])
|
||||
break;
|
||||
if(link[ map[r][c+2] ][ map[r+2][c+1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 5: //210搴逛
|
||||
{
|
||||
if(link[ map[r-1][c] ][ map[r][c+2] ])
|
||||
break;
|
||||
if(c-1>=0 && link[ map[r-1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(c+3<=n && link[ map[r-1][c+1] ][ map[r][c+3] ])
|
||||
break;
|
||||
if(r-2>=0)
|
||||
{
|
||||
if(link[ map[r-2][c] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(link[ map[r-2][c+1] ][ map[r][c+2] ])
|
||||
break;
|
||||
if(link[ map[r-2][c+2] ][ map[r][c+1] ])
|
||||
break;
|
||||
}
|
||||
if(r+1<=n)
|
||||
{
|
||||
if(link[ map[r][c] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r+1][c+1] ][ map[r-1][c] ])
|
||||
break;
|
||||
if(link[ map[r+1][c+2] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 6: //240搴逛
|
||||
{
|
||||
if(link[ map[r-2][c] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(r-3>=0 && link[ map[r-3][c] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
if(r+1<=n && link[ map[r-1][c] ][ map[r+1][c+1] ])
|
||||
break;
|
||||
if(c-1>=0)
|
||||
{
|
||||
if(link[ map[r-2][c-1] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r-1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
if(link[ map[r][c-1] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
}
|
||||
if(c+2<=n)
|
||||
{
|
||||
if(link[ map[r-1][c] ][ map[r-2][c+2] ])
|
||||
break;
|
||||
if(link[ map[r-2][c] ][ map[r-1][c+2] ])
|
||||
break;
|
||||
if(link[ map[r-1][c] ][ map[r][c+2] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 7: //300搴逛
|
||||
{
|
||||
if(link[ map[r-2][c] ][ map[r][c-1] ])
|
||||
break;
|
||||
if(r-3>=0 && link[ map[r-3][c] ][ map[r-1][c-1] ])
|
||||
break;
|
||||
if(r+1<=n && link[ map[r-1][c] ][ map[r+1][c-1] ])
|
||||
break;
|
||||
if(c-2>=0)
|
||||
{
|
||||
if(link[ map[r-2][c-2] ][ map[r-1][c] ])
|
||||
break;
|
||||
if(link[ map[r-1][c-2] ][ map[r-2][c] ])
|
||||
break;
|
||||
if(link[ map[r][c-2] ][ map[r-1][c] ])
|
||||
break;
|
||||
}
|
||||
if(c+1<=n)
|
||||
{
|
||||
if(link[ map[r-1][c-1] ][ map[r-2][c+1] ])
|
||||
break;
|
||||
if(link[ map[r][c-1] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
if(link[ map[r-1][c-1] ][ map[r][c+1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
case 8: //330搴逛
|
||||
{
|
||||
if(link[ map[r][c-2] ][ map[r-1][c] ])
|
||||
break;
|
||||
if(c-3>=0 && link[ map[r][c-3] ][ map[r-1][c-1] ])
|
||||
break;
|
||||
if(c+1<=n && link[ map[r][c-1] ][ map[r-1][c+1] ])
|
||||
break;
|
||||
if(r-2>=0)
|
||||
{
|
||||
if(link[ map[r-2][c-2] ][ map[r][c-1] ])
|
||||
break;
|
||||
if(link[ map[r-2][c-1] ][ map[r][c-2] ])
|
||||
break;
|
||||
if(link[ map[r-2][c] ][ map[r][c-1] ])
|
||||
break;
|
||||
}
|
||||
if(r+1<=n)
|
||||
{
|
||||
if(link[ map[r-1][c-1] ][ map[r+1][c-2] ])
|
||||
break;
|
||||
if(link[ map[r-1][c-1] ][ map[r+1][c] ])
|
||||
break;
|
||||
if(link[ map[r-1][c] ][ map[r+1][c-1] ])
|
||||
break;
|
||||
}
|
||||
int a=map[peg[i].r][peg[i].c];
|
||||
int b=map[r][c];
|
||||
peg[a].connet[peg[a].pc++]=b;
|
||||
peg[b].connet[peg[b].pc++]=a;
|
||||
link[a][b]=link[b][a]=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
bool CheckWin(PEG* peg,bool flag)
|
||||
{
|
||||
int NUM;
|
||||
if(!flag)
|
||||
NUM=map[lastx][lasty];
|
||||
for(int k=0;k<=n;k++)
|
||||
{
|
||||
int p=map[0][k];
|
||||
if(p && p!=NUM && peg[p].color==1)
|
||||
{
|
||||
int queue[num];
|
||||
bool vist[num]={false};
|
||||
int head=0;
|
||||
int tail=0;
|
||||
queue[tail++]=p;
|
||||
vist[p]=true;
|
||||
while(head<tail)
|
||||
{
|
||||
int s=queue[head++];
|
||||
if(peg[s].r==n)
|
||||
return true;
|
||||
for(int i=0;i<peg[s].pc;i++)
|
||||
{
|
||||
int x=peg[s].connet[i];
|
||||
if(!vist[x])
|
||||
{
|
||||
vist[x]=true;
|
||||
if(!flag && x==NUM)
|
||||
continue;
|
||||
queue[tail++]=x;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
#include<stdio.h>
|
||||
double max(double a,double b)
|
||||
{
|
||||
if(a>b)
|
||||
return a;
|
||||
else
|
||||
return b;
|
||||
}
|
||||
double min(double a,double b)
|
||||
{
|
||||
if(a>b)
|
||||
return b;
|
||||
else
|
||||
return a;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int a,b,c,d,z;
|
||||
double x1,x2,x3,x4,y1,y2;
|
||||
while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF&&(a!=0||b!=0||c!=0||d!=0))
|
||||
{
|
||||
if(c>=a&&d>=b)
|
||||
printf("100%%\n");
|
||||
else if(c>=b&&d>=a)
|
||||
printf("100%%\n");
|
||||
else
|
||||
{
|
||||
x1=c*1.0/a;
|
||||
x2=d*1.0/b;
|
||||
y1=min(x1,x2);
|
||||
x3=c*1.0/b;
|
||||
x4=d*1.0/a;
|
||||
y2=min(x3,x4);
|
||||
z=max(y1,y2)*100;
|
||||
printf("%d%%\n",z);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#define oo 100000
|
||||
bool dfs(int, int);
|
||||
int n,m,mini;
|
||||
bool finished[14];
|
||||
struct CLASS {
|
||||
char name[8];
|
||||
int time,preNum;
|
||||
int prerequisites[13];
|
||||
} subject[15];
|
||||
bool permit(int num) {
|
||||
int i;
|
||||
for(i=0; i<subject[num].preNum && finished[subject[num].prerequisites[i]]; i++);
|
||||
if(i == subject[num].preNum) return true;
|
||||
return false;
|
||||
}
|
||||
bool selectAndDfs(int total, int elect, int from, int que[], int result[], int lev, int semester) {
|
||||
int i;
|
||||
if(total-from < elect) return false;
|
||||
if(from == total && elect == 0) {
|
||||
for(i=0; i<m; i++) finished[result[i]] = true;
|
||||
dfs(lev+m, semester+1);
|
||||
for(i=0; i<m; i++) finished[result[i]] = false;
|
||||
return true;
|
||||
}
|
||||
for(i=from; i<total; i++) {
|
||||
result[m-elect] = que[i];
|
||||
selectAndDfs(total, elect-1, i+1, que, result, lev, semester);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool dfs(int lev, int semester) {
|
||||
if(semester-1 >= mini) return false;
|
||||
if(lev == n) {
|
||||
if(semester-1 < mini) mini = semester-1;
|
||||
return true;
|
||||
}
|
||||
int i,tn=0,que[15];
|
||||
for(i=0; i<n; i++) if(!finished[i] && permit(i) && (subject[i].time & (1<<semester%2)) != 0) que[tn++] = i;
|
||||
if(tn <= m) {
|
||||
for(i=0; i<tn; i++) finished[que[i]] = true;
|
||||
dfs(lev+tn,semester+1);
|
||||
for(i=0; i<tn; i++) finished[que[i]] = false;
|
||||
}
|
||||
else {
|
||||
int temp[15];
|
||||
selectAndDfs(tn,m,0,que,temp,lev,semester);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
int main() {
|
||||
while(scanf("%d%d", &n, &m) == 2 && !(n == -1 && m == -1)) {
|
||||
int i,j,k,p,t;
|
||||
for(i=0; i<n; i++) {
|
||||
scanf("%s", &subject[i].name);
|
||||
subject[i].time = subject[i].preNum = 0;
|
||||
}
|
||||
for(i=0; i<n; i++) {
|
||||
char s[10];
|
||||
scanf("%s", s);
|
||||
for(j=0; j<n && strcmp(s, subject[j].name) != 0; j++);
|
||||
scanf("%s", s);
|
||||
if(s[0] == 'B') subject[j].time = 3;
|
||||
else if(s[0] == 'S') subject[j].time = 1;
|
||||
else subject[j].time = 2;
|
||||
scanf("%d", &t);
|
||||
for(k=0; k<t; k++) {
|
||||
scanf("%s", s);
|
||||
for(p=0; p<n && strcmp(s, subject[p].name) != 0; p++);
|
||||
subject[j].prerequisites[subject[j].preNum++] = p;
|
||||
}
|
||||
}
|
||||
memset(finished, 0, sizeof(finished));
|
||||
mini = oo;
|
||||
dfs(0,1);
|
||||
printf("The minimum number of semesters required to graduate is %d.\n", mini);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#define N 13
|
||||
int cmp(const void *p, const void *q)
|
||||
{
|
||||
return *(int *)p > *(int *)q ? -1 : 1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i, j;
|
||||
int n, grey, count, max, colors[N];
|
||||
while(scanf("%d", &n) && n)
|
||||
{
|
||||
memset(colors, 0, sizeof(colors));
|
||||
max = 0;
|
||||
for(i=0; i<n; i++)
|
||||
{
|
||||
scanf("%d", &colors[i]);
|
||||
if(colors[i] > max)
|
||||
max = colors[i];
|
||||
}
|
||||
scanf("%d", &grey);
|
||||
if(max % 50)
|
||||
count = max / 50 + 1;
|
||||
else count = max / 50;
|
||||
for(i=0; i<n; i++)
|
||||
{
|
||||
colors[i] = count * 50 - colors[i];
|
||||
}
|
||||
qsort(colors, n, sizeof(colors[0]), cmp);
|
||||
while(1)
|
||||
{
|
||||
if(colors[2] == 0 && grey > 0)
|
||||
{
|
||||
count++;
|
||||
for(i=0; i<n; i++)
|
||||
colors[i] += 50;
|
||||
}
|
||||
grey --;
|
||||
if(grey <= 0)
|
||||
break;
|
||||
else {
|
||||
colors[0]--;
|
||||
colors[1]--;
|
||||
colors[2]--;
|
||||
qsort(colors, n, sizeof(colors[0]), cmp);
|
||||
}
|
||||
}
|
||||
printf("%d\n", count);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int cases, x = 1;
|
||||
cin>>cases;
|
||||
while (cases--) {
|
||||
int a, b, c, d;
|
||||
cin>>a>>b>>c>>d;
|
||||
printf("Problem set %d: %d / %d, base 7 digits %d through %d: ", x++, a, b, c, d);
|
||||
int i;
|
||||
a %= b;
|
||||
for (i = 0; i <= d; ++i) {
|
||||
a *= 7;
|
||||
if (i >= c) {
|
||||
printf("%d", a / b);
|
||||
}
|
||||
a %= b;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,158 @@
|
|||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#define RE(x) ((x)^1)
|
||||
#define CP(x) ((x)+500)
|
||||
#define INF 0x3fffffff
|
||||
using namespace std;
|
||||
int N, M, MM, dis[1000], head[1000], idx;
|
||||
const int source = 980, sink = 981;
|
||||
char G[25][25], S[25][25];
|
||||
struct Edge
|
||||
{
|
||||
int v, cap, next;
|
||||
}e[20000];
|
||||
void init()
|
||||
{
|
||||
idx = -1;
|
||||
memset(head, 0xff, sizeof (head));
|
||||
}
|
||||
inline int to(int x, int y)
|
||||
{
|
||||
return x*M+y;
|
||||
}
|
||||
inline bool out(int x, int y)
|
||||
{
|
||||
int u = x+1, d = N-x, l = y+1, r = M-y;
|
||||
int dist = min(u, min(d, min(l, r)));
|
||||
return dist <= MM;
|
||||
}
|
||||
inline bool judge(int x, int y)
|
||||
{
|
||||
if (x < 0 || x >= N || y < 0 || y >= M) {
|
||||
return false;
|
||||
}
|
||||
else if (!G[x][y]) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void insert(int a, int b, int c)
|
||||
{
|
||||
++idx;
|
||||
e[idx].v = b, e[idx].cap = c;
|
||||
e[idx].next = head[a], head[a] = idx;
|
||||
}
|
||||
void build(int x, int y)
|
||||
{
|
||||
int xx, yy;
|
||||
if (G[x][y]) {
|
||||
insert(to(x, y), CP(to(x,y)), G[x][y]);
|
||||
insert(CP(to(x, y)), to(x,y), G[x][y]);
|
||||
if (out(x, y)) {
|
||||
insert(CP(to(x, y)), sink, INF);
|
||||
insert(sink, CP(to(x, y)), 0);
|
||||
}
|
||||
else {
|
||||
for (int i = -MM; i <= MM; ++i) {
|
||||
for (int j = -(MM-abs(i)); j <= (MM-abs(i)); ++j) {
|
||||
xx = x + i, yy = y + j;
|
||||
if (judge(xx, yy) && !(x == xx && y == yy)) {
|
||||
insert(CP(to(x, y)), to(xx, yy), G[x][y]);
|
||||
insert(to(xx, yy), CP(to(x, y)), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
bool spfa(int u)
|
||||
{
|
||||
queue<int>q;
|
||||
memset(dis, 0xff, sizeof (dis));
|
||||
dis[u] = 0;
|
||||
q.push(u);
|
||||
while (!q.empty()) {
|
||||
u = q.front();
|
||||
q.pop();
|
||||
for (int i = head[u]; i != -1; i = e[i].next) {
|
||||
if (dis[e[i].v] == -1 && e[i].cap > 0) {
|
||||
dis[e[i].v] = dis[u] + 1;
|
||||
q.push(e[i].v);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dis[sink] != -1;
|
||||
}
|
||||
int dfs(int u, int flow)
|
||||
{
|
||||
if (u == sink) {
|
||||
return flow;
|
||||
}
|
||||
int tf = 0, sf;
|
||||
for (int i = head[u]; i != -1; i = e[i].next) {
|
||||
if (dis[u]+1 == dis[e[i].v] && e[i].cap > 0 && (sf = dfs(e[i].v, min(flow-tf, e[i].cap)))) {
|
||||
e[i].cap -= sf, e[RE(i)].cap += sf;
|
||||
tf += sf;
|
||||
if (tf == flow) {
|
||||
return flow;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!tf) {
|
||||
dis[u] = -1;
|
||||
}
|
||||
return tf;
|
||||
}
|
||||
int dinic()
|
||||
{
|
||||
int ans = 0;
|
||||
while (spfa(source)) {
|
||||
ans += dfs(source, INF);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T, ca = 0, ans;
|
||||
scanf("%d", &T);
|
||||
while (T--) {
|
||||
init();
|
||||
ans = 0;
|
||||
scanf("%d %d", &N, &MM);
|
||||
for (int i = 0; i < N; ++i) {
|
||||
scanf("%s", G[i]);
|
||||
}
|
||||
M = strlen(G[0]);
|
||||
for (int i = 0; i < N; ++i) {
|
||||
for (int j = 0; j < M; ++j) {
|
||||
G[i][j] -= '0';
|
||||
build(i, j);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < N; ++i) {
|
||||
scanf("%s", S[i]);
|
||||
for (int j = 0; j < M; ++j) {
|
||||
if (S[i][j] == 'L') {
|
||||
++ans;
|
||||
insert(source, to(i, j), 1);
|
||||
insert(to(i, j), source, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
ans -= dinic();
|
||||
printf("Case #%d: ", ++ca);
|
||||
if (!ans) {
|
||||
puts("no lizard was left behind.");
|
||||
}
|
||||
else if (ans == 1){
|
||||
printf("%d lizard was left behind.\n", ans);
|
||||
}
|
||||
else {
|
||||
printf("%d lizards were left behind.\n", ans);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,144 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
char s[81];
|
||||
int l;
|
||||
int Dig(char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int Upper(char c)
|
||||
{
|
||||
if (c >= 'A' && c <= 'Z')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int Lower(char c)
|
||||
{
|
||||
if (c >= 'a' && c <= 'z')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int Char(char c)
|
||||
{
|
||||
if (Upper(c) || Lower(c))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int Punc(char c)
|
||||
{
|
||||
if (Dig(c) || Char(c) || c == ' ')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int Iso(char c)
|
||||
{
|
||||
if (c == 'A' || c == 'a' || c == 'I')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int check1()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < l - 1; i++)
|
||||
{
|
||||
if(Upper(s[i]) && Upper(s[i + 1]))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int check2()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < l - 1; i++)
|
||||
{
|
||||
if ((Char(s[i]) && Dig(s[i + 1])) || (Char(s[i + 1]) && Dig(s[i])))
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int check3()
|
||||
{
|
||||
int i;
|
||||
if (l == 1)
|
||||
{
|
||||
if (Iso(s[0]))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (s[1] == ' '&& Iso(s[0]) == 0 && s[0] != ' ')
|
||||
return 0;
|
||||
if (s[l - 2] == ' ' && Iso(s[l - 1]) == 0 && s[l - 1] != ' ')
|
||||
return 0;
|
||||
for (i = 1; i < l - 1; i++)
|
||||
{
|
||||
if(s[i] != ' ' && s[i - 1] == ' ' && s[i + 1] == ' ' && Iso(s[i]) == 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int check4()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < l - 1; i++)
|
||||
{
|
||||
if(Punc(s[i]) && Punc(s[i + 1]) && s[i] != '\"' && s[i + 1] != '\"')
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int check()
|
||||
{
|
||||
if (check1() == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (check2() == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (check3() == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (check4() == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
gets(s);
|
||||
if (strcmp(s, "#") == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
l = strlen(s);
|
||||
if (check())
|
||||
{
|
||||
printf("OK\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("suspicious\n");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int main()
|
||||
{
|
||||
char a[300];
|
||||
int i;
|
||||
int b[300];
|
||||
while(gets(a) &&a[0]!='#')
|
||||
{
|
||||
memset(b,0,sizeof(b));
|
||||
int l=strlen(a);
|
||||
for(i=0;i<l;i++)
|
||||
{
|
||||
if(a[i]!=' ')
|
||||
b[i]=a[i]-'A'+1;
|
||||
else
|
||||
b[i]=0;
|
||||
}
|
||||
int sum=0;
|
||||
for(i=0;i<l;i++)
|
||||
sum+=b[i]*(i+1);
|
||||
printf("%d\n",sum);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
int main(){
|
||||
char str[100];
|
||||
while(scanf("%s", str)&&str[0]!='#'){
|
||||
int flag=1, ans=0, len=strlen(str), t=0, i;
|
||||
for(i=0; i<len; i++){
|
||||
switch(str[i]){
|
||||
case '/':if(flag)ans+=100;flag=0;break;
|
||||
case '\\':t=100;break;
|
||||
case '.':ans+=t+100;t=0;flag=1;break;
|
||||
case '_':break;
|
||||
case '|':if(flag)ans+=50;flag=0;t=50;break;
|
||||
}
|
||||
}
|
||||
printf("%d\n", (ans+t)/len);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
int main()
|
||||
{
|
||||
int i,j,len,k,z,l,flag;
|
||||
char ch[100];
|
||||
char s[100][2];
|
||||
while(gets(ch))
|
||||
{
|
||||
memset(s,0,sizeof(s));
|
||||
flag=0;
|
||||
if(ch[0]=='*')
|
||||
break;
|
||||
len=strlen(ch);
|
||||
for(i=1;i<len-1;i++)
|
||||
{
|
||||
k=0;
|
||||
for(j=0;j+i<len;j++)
|
||||
{
|
||||
s[k][0]=ch[j];
|
||||
s[k][1]=ch[j+i];
|
||||
k++;
|
||||
}
|
||||
for(z=0;z<k;z++)
|
||||
for(l=z+1;l<k;l++)
|
||||
{
|
||||
if(s[z][0]==s[l][0]&&s[z][1]==s[l][1])
|
||||
{flag=1;break;}
|
||||
}
|
||||
if(flag==1)
|
||||
break;
|
||||
}
|
||||
if(flag==1)
|
||||
{
|
||||
printf("%s is NOT surprising.\n",ch);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%s is surprising.\n",ch);
|
||||
}
|
||||
memset(ch,0,sizeof(ch));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,113 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define maxn 101
|
||||
struct Term
|
||||
{
|
||||
int s[maxn];
|
||||
char ch[maxn];
|
||||
}term[maxn];
|
||||
string word[maxn];
|
||||
string st;
|
||||
int n, m, termnum[maxn];
|
||||
bool input()
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
getline(cin, word[i]);
|
||||
if (word[i] == "#")
|
||||
return false;
|
||||
if (word[i] == "*")
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
n = i;
|
||||
return true;
|
||||
}
|
||||
void getterm(string &a, Term &term, int &num)
|
||||
{
|
||||
int i = 0, j = 0;
|
||||
while (i < a.length())
|
||||
{
|
||||
if (a[i] != '-' && a[i] != '+')
|
||||
{
|
||||
term.ch[j] = a[i];
|
||||
term.s[j] = 1;
|
||||
i++;
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
if (a[i] == '-')
|
||||
term.s[j] = 0;
|
||||
if (a[i] == '+')
|
||||
term.s[j] = 2;
|
||||
i++;
|
||||
term.ch[j] = a[i];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
num = j;
|
||||
}
|
||||
void maketerms()
|
||||
{
|
||||
int temp;
|
||||
int i = 0;
|
||||
temp = st.find('|');
|
||||
while (temp != string::npos)
|
||||
{
|
||||
string sub = st.substr(0, temp);
|
||||
getterm(sub, term[i], termnum[i]);
|
||||
st.erase(0, temp + 1);
|
||||
i++;
|
||||
temp = st.find('|');
|
||||
}
|
||||
getterm(st, term[i], termnum[i]);
|
||||
m = i + 1;
|
||||
}
|
||||
bool match(string &word, Term &term, int &num)
|
||||
{
|
||||
for (int i = 0; i < num; i++)
|
||||
if (term.s[i] == 0 && word.find(term.ch[i]) != string::npos)
|
||||
return false;
|
||||
for (int i = 0; i < num; i++)
|
||||
if (term.s[i] == 2 && word.find(term.ch[i]) == string::npos)
|
||||
return false;
|
||||
for (int i = 0; i < num; i++)
|
||||
if (term.s[i] == 1 && word.find(term.ch[i]) != string::npos)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
if (!input())
|
||||
break;
|
||||
sort(word, word + n);
|
||||
while (getline(cin, st) && st != "**")
|
||||
{
|
||||
bool found = false;
|
||||
maketerms();
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < m; j++)
|
||||
if (match(word[i], term[j], termnum[j]))
|
||||
{
|
||||
found = true;
|
||||
cout << word[i] << endl;
|
||||
break;
|
||||
}
|
||||
if (found)
|
||||
break;
|
||||
}
|
||||
if (!found)
|
||||
cout << "NONE" << endl;
|
||||
}
|
||||
cout << "$" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
#include<stdio.h>
|
||||
#include "math.h"
|
||||
int e[2001][2];//杈[0],to.[1]next;
|
||||
int a[1001][5];//[0],ID,[1]next;2x3y4z
|
||||
int c[1001];
|
||||
int cnum;
|
||||
int max;int m;
|
||||
bool sign[1001];
|
||||
int n;//圭扮
|
||||
int en;//杈圭扮
|
||||
void initt();
|
||||
void init();
|
||||
int DFS(int s);
|
||||
int eq(int x);
|
||||
int main()
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
if(n==0)return 0;
|
||||
initt();
|
||||
init();
|
||||
int min=1000;
|
||||
int i;
|
||||
for(i=1;i<=n;i++)//n娆DFS
|
||||
{
|
||||
max=0;
|
||||
m=0;
|
||||
DFS(i);
|
||||
if(max<min)min=max,c[1]=a[i][0],cnum=1;
|
||||
else if(max==min)cnum++,c[cnum]=a[i][0];
|
||||
else ;
|
||||
}
|
||||
if(cnum==1)printf("%d",c[1]);
|
||||
else
|
||||
{
|
||||
if(c[1]>c[2])printf("%d %d",c[2],c[1]);
|
||||
else printf("%d %d",c[1],c[2]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
int DFS(int s)
|
||||
{
|
||||
if(sign[s])return 0;
|
||||
if((!e[a[s][1]][1])&&sign[e[a[s][1]][0]]){
|
||||
if(max<m){max=m; return 0;}
|
||||
else return 0;}
|
||||
m++;
|
||||
sign[s]=true;
|
||||
int next=a[s][1];
|
||||
while(next)
|
||||
{
|
||||
DFS(e[next][0]);
|
||||
next=e[next][1];
|
||||
}
|
||||
m--;sign[s]=false;
|
||||
return 0;
|
||||
}
|
||||
void initt()
|
||||
{
|
||||
int i;
|
||||
cnum=0;
|
||||
for(i=0;i<=n;i++)
|
||||
a[i][1]=0;
|
||||
for(i=0;i<=2*n;i++)
|
||||
e[i][1]=0;
|
||||
}
|
||||
void init()
|
||||
{
|
||||
int k=0;int en=0;
|
||||
k++;
|
||||
int id;int x;int y;int z;
|
||||
scanf("%d%d%d%d",&id,&x,&y,&z);
|
||||
a[k][0]=id;a[k][2]=x,a[k][3]=y,a[k][4]=z;
|
||||
while(k<n)
|
||||
{
|
||||
k++;
|
||||
scanf("%d%d%d%d",&id,&x,&y,&z);
|
||||
a[k][0]=id;a[k][2]=x,a[k][3]=y,a[k][4]=z;
|
||||
int ee;ee=eq(k);
|
||||
en++;e[en][0]=ee;e[en][1]=a[k][1];a[k][1]=en;
|
||||
en++;e[en][0]=k;e[en][1]=a[ee][1];a[ee][1]=en;
|
||||
}
|
||||
}
|
||||
int eq(int x)
|
||||
{
|
||||
int i;double min=10000;int xx;
|
||||
for(i=1;i<x;i++)
|
||||
{
|
||||
double temp=((a[x][3]-a[i][3])*(a[x][3]-a[i][3])+(a[x][4]-a[i][4])*(a[x][4]-a[i][4])+(a[x][2]-a[i][2])*(a[x][2]-a[i][2]));
|
||||
double val=sqrt(temp);
|
||||
if(val<min)min=val,xx=i;
|
||||
}
|
||||
return xx;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
int main()
|
||||
{
|
||||
double B,N;
|
||||
int A;
|
||||
while(scanf("%lf%lf",&B,&N)&&(B!=0.0||N!=0.0))
|
||||
{
|
||||
A=pow(B,1/N);
|
||||
if((B-pow(A,N))<(pow(A+1,N)-B))
|
||||
printf("%d\n",A);
|
||||
else
|
||||
printf("%d\n",A+1);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std ;
|
||||
#define M 100000 + 10
|
||||
#define eps 1e-4
|
||||
int A[M] ;
|
||||
int n ;
|
||||
int m ;
|
||||
int slove(double x)
|
||||
{
|
||||
int sum = 0 ;
|
||||
double d = A[0] ;
|
||||
d += 2 * x ;
|
||||
int i = 0 ;
|
||||
++ sum ;
|
||||
while(d < A[m -1] * 1.0 )
|
||||
{
|
||||
while(A[i] <= d) ++ i ;
|
||||
d = A[i] + 2 * x ;
|
||||
sum ++ ;
|
||||
}
|
||||
return sum ;
|
||||
}
|
||||
double Find(double left , double right)
|
||||
{
|
||||
double mid ;
|
||||
while(right - left > eps)
|
||||
{
|
||||
mid = left + (right - left) / 2 ;
|
||||
if(slove(mid) <= n)
|
||||
right = mid ;
|
||||
else left = mid +eps ;
|
||||
}
|
||||
return left ;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int cas ;
|
||||
double low ;
|
||||
double high ;
|
||||
double ans ;
|
||||
while(scanf("%d",&cas) != EOF)
|
||||
{
|
||||
while(cas --)
|
||||
{
|
||||
scanf("%d%d",&n,&m) ;
|
||||
for(int i = 0 ;i < m ; ++ i)
|
||||
scanf("%d",&A[i]) ;
|
||||
sort(A , A+ m) ;
|
||||
low = 0 ;
|
||||
high = A[m -1] * 1.0 / n ;
|
||||
ans = Find(low , high) ;
|
||||
printf("%.1lf\n",ans) ;
|
||||
}
|
||||
}
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
#include <set>
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <stack>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
const int INF = 0x3f3f3f3f;
|
||||
const double eps = 1e-5;
|
||||
int n,m;
|
||||
int sum;
|
||||
int Dp[110000];
|
||||
int a[110];
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
while(~scanf("%d",&T))
|
||||
{
|
||||
while(T--)
|
||||
{
|
||||
scanf("%d %d",&m,&n);
|
||||
sum=0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
scanf("%d",&a[i]);
|
||||
sum=max(sum,a[i]);
|
||||
}
|
||||
sum+=m;
|
||||
memset(Dp,INF,sizeof(Dp));
|
||||
Dp[0]=0;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
for(int j=sum;j>=a[i];j--)
|
||||
{
|
||||
if(Dp[j-a[i]]!=INF)
|
||||
{
|
||||
Dp[j]=min(Dp[j],Dp[j-a[i]]+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=m;;i++)
|
||||
{
|
||||
if(Dp[i]!=INF)
|
||||
{
|
||||
printf("%d %d\n",i,Dp[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
#include <cstdlib>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <fstream>
|
||||
#include <numeric>
|
||||
#include <iomanip>
|
||||
#include <bitset>
|
||||
#include <list>
|
||||
#include <stdexcept>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
typedef unsigned long long ULL;
|
||||
#define MAX(a,b) ((a) > (b) ? (a) : (b))
|
||||
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
||||
#define MEM(a,b) memset((a),(b),sizeof(a))
|
||||
const int N = 10000;
|
||||
vector<int> v[N + 1];
|
||||
int flag[N + 1];
|
||||
int cnt;
|
||||
void dfs(int x)
|
||||
{
|
||||
if (flag[x]) return;
|
||||
flag[x] = 1;
|
||||
cnt++;
|
||||
for (int i = 0; i < v[x].size(); i++)
|
||||
{
|
||||
dfs(v[x][i]);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int ncase;
|
||||
while (cin >> ncase)
|
||||
{
|
||||
while (ncase--)
|
||||
{
|
||||
int n, m, l;
|
||||
cin >> n >> m >> l;
|
||||
for (int i = 1; i <= n; i++) v[i].clear();
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
int x, y;
|
||||
cin >> x >> y;
|
||||
v[x].push_back(y);
|
||||
}
|
||||
MEM(flag, 0);
|
||||
cnt = 0;
|
||||
while (l--)
|
||||
{
|
||||
int x;
|
||||
cin >> x;
|
||||
dfs(x);
|
||||
}
|
||||
cout << cnt << endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
#define PI 3.14159265358979
|
||||
#define EPSILON 1e-6
|
||||
typedef struct Point
|
||||
{
|
||||
double x,y;
|
||||
Point(double a = 0,double b = 0):x(a),y(b) {}
|
||||
Point(const Point & p):x(p.x),y(p.y) {}
|
||||
friend Point operator + (const Point &p1, const Point & p2)
|
||||
{
|
||||
return Point(p1.x+p2.x,p1.y+p2.y);
|
||||
}
|
||||
Point & operator += (const Point & p)
|
||||
{
|
||||
x += p.x;
|
||||
y += p.y;
|
||||
return *this;
|
||||
}
|
||||
friend Point operator * (const Point & p, int u)
|
||||
{
|
||||
return Point(p.x*u,p.y*u);
|
||||
}
|
||||
Point & operator = (const Point & p)
|
||||
{
|
||||
x = p.x;
|
||||
y = p.y;
|
||||
return * this;
|
||||
}
|
||||
friend bool operator == (const Point & p1, const Point & p2)
|
||||
{
|
||||
if(fabs(p1.x-p2.x)<EPSILON && fabs(p1.y-p2.y)<EPSILON)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}Vector;
|
||||
struct Logo
|
||||
{
|
||||
char str[6];
|
||||
int u;
|
||||
};
|
||||
Logo ls[1000];
|
||||
int num;
|
||||
char cmd[6];
|
||||
Point Test(double theta, Vector v,Point pt,int j,int degree)
|
||||
{
|
||||
if(cmd[0] == 'l')
|
||||
{ theta+=degree; v = Vector(cos(PI*theta/180),sin(PI*theta/180));}
|
||||
else
|
||||
{ theta-=degree; v = Vector(cos(PI*theta/180),sin(PI*theta/180)); }
|
||||
for(int k = j+1; k<num; k++)
|
||||
{
|
||||
int t = ls[k].u;
|
||||
switch(ls[k].str[0])
|
||||
{
|
||||
case 'f': pt += v*t; break;
|
||||
case 'b': pt += v*(-t); break;
|
||||
case 'l': theta+=t; v = Vector(cos(PI*theta/180),sin(PI*theta/180));
|
||||
break;
|
||||
case 'r': theta-=t; v = Vector(cos(PI*theta/180),sin(PI*theta/180));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return pt;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int test,i,j,k,t;
|
||||
char unit[128];
|
||||
while(scanf("%d",&test)==1)
|
||||
{
|
||||
for(i = 0; i<test; i++)
|
||||
{
|
||||
scanf("%d",&num);
|
||||
Point pt;
|
||||
Vector v(1,0);
|
||||
Vector vx;
|
||||
double theta = 0.0;
|
||||
for(j = 0; j<num; j++)
|
||||
{
|
||||
scanf("%s %s",cmd,unit);
|
||||
if(unit[0] == '?')
|
||||
break;
|
||||
t = atoi(unit);
|
||||
switch(cmd[0])
|
||||
{
|
||||
case 'f': pt += v*t; break;
|
||||
case 'b': pt += v*(-t); break;
|
||||
case 'l': theta+=t; v = Vector(cos(PI*theta/180),sin(PI*theta/180));
|
||||
break;
|
||||
case 'r': theta-=t; v = Vector(cos(PI*theta/180),sin(PI*theta/180));
|
||||
break;
|
||||
}
|
||||
}
|
||||
for(k = j+1; k<num; k++)
|
||||
{
|
||||
scanf("%s %d",ls[k].str,&ls[k].u);
|
||||
}
|
||||
if(cmd[0] == 'l' || cmd[0] == 'r')
|
||||
{
|
||||
for(int degree = 0; degree < 360; degree++)
|
||||
{
|
||||
if(Test(theta,v,pt,j,degree) == Point(0.0,0.0) )
|
||||
{
|
||||
printf("%d\n",degree);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
vx = v;
|
||||
for(k = j+1; k<num; k++)
|
||||
{
|
||||
t = ls[k].u;
|
||||
switch(ls[k].str[0])
|
||||
{
|
||||
case 'f': pt += v*t; break;
|
||||
case 'b': pt += v*(-t); break;
|
||||
case 'l': theta+=t; v = Vector(cos(PI*theta/180),sin(PI*theta/180));
|
||||
break;
|
||||
case 'r': theta-=t; v = Vector(cos(PI*theta/180),sin(PI*theta/180));
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("%d\n",(int)(sqrt(pt.x*pt.x + pt.y*pt.y)+0.5));
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
#include <cstdio>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
bool vis[1000000];
|
||||
int main(){
|
||||
int T, n;
|
||||
while (scanf("%d",&T)==1){
|
||||
while (T--){
|
||||
scanf("%d", &n);
|
||||
int temp, ans = 0;
|
||||
queue<int> que;
|
||||
while (n--){
|
||||
scanf("%d", &temp);
|
||||
while (vis[temp]){
|
||||
vis[que.front()] = false;
|
||||
que.pop();
|
||||
}
|
||||
que.push(temp);
|
||||
vis[temp] = true;
|
||||
if (que.size() > ans)
|
||||
ans = que.size();
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
while (que.size()){
|
||||
vis[que.front()] = false;
|
||||
que.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
int sx,sy,ex,ey;
|
||||
char a[1002][1002];
|
||||
int b[1002][1002];
|
||||
int dir[8][2]={0,-1,1,-1,1,0,1,1,0,1,-1,1,-1,0,-1,-1};
|
||||
struct fuck{
|
||||
int x, y,t;
|
||||
friend bool operator< (const fuck &a, const fuck &b)
|
||||
{
|
||||
return a.t>b.t;
|
||||
}
|
||||
}f;
|
||||
priority_queue<fuck> q;
|
||||
int bfs()
|
||||
{
|
||||
int nx,ny,nt,x,y,t,i;
|
||||
if(sx==ex&&sy==ey)
|
||||
return 0;
|
||||
f.x=sx;f.y=sy;f.t=0;
|
||||
q.push(f);
|
||||
b[sx][sy]=0;
|
||||
while(!q.empty())
|
||||
{
|
||||
x=q.top().x;
|
||||
y=q.top().y;
|
||||
t=q.top().t;
|
||||
q.pop();
|
||||
for(i=0;i<8;i++)
|
||||
{
|
||||
nx=x+dir[i][1];
|
||||
ny=y+dir[i][0];
|
||||
nt=t+(i!=(a[x][y]-48));
|
||||
if(a[nx][ny]!='x'&&nt<b[ex][ey]&&nt<b[nx][ny])
|
||||
{
|
||||
f.x=nx;f.y=ny;f.t=nt;
|
||||
if(nx!=ex||ny!=ey)
|
||||
q.push(f);
|
||||
b[nx][ny]=nt;
|
||||
}
|
||||
}
|
||||
}
|
||||
return b[ex][ey];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,t,m,i,j;
|
||||
memset(a,'x',sizeof(a));
|
||||
while(scanf("%d%d",&n,&m)==2)
|
||||
{
|
||||
getchar();
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
for(j=1;j<=m;j++)
|
||||
scanf("%c",&a[i][j]);
|
||||
getchar();
|
||||
}
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
|
||||
for(i=1;i<=n;i++)
|
||||
for(j=1;j<=m;j++)
|
||||
b[i][j]=2002;
|
||||
printf("%d\n",bfs());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int T;
|
||||
string str[105];
|
||||
int l, n;
|
||||
void work()
|
||||
{
|
||||
scanf("%d%d\n", &l, &n);
|
||||
for (int i = 0; i < n; ++i) cin >> str[i];
|
||||
str[n] = str[0];
|
||||
int ans = n * l;
|
||||
string cur = str[0];
|
||||
for (int i = 1; i < n; ++i) {
|
||||
int common = 0;
|
||||
for (int cl = 1; cl <= l; ++cl) if (cur.substr(cur.size() - cl, cl) == str[i].substr(0, cl)) common = cl;
|
||||
cur += str[i].substr(common);
|
||||
}
|
||||
printf("%d\n", cur.size());
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while (scanf("%d", &T) != EOF)
|
||||
while (T--) work();
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
const int MAXP = 100;
|
||||
int dis(int *p1, int *p2)
|
||||
{
|
||||
int d = 0;
|
||||
for(int i = 0; i < 3; ++i)
|
||||
{
|
||||
d += abs(p1[i] - p2[i]);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int N;
|
||||
cin>>N;
|
||||
int points[MAXP][3];
|
||||
for(int no = 0; no < N; ++no)
|
||||
{
|
||||
int P;
|
||||
cin>>P;
|
||||
fscanf(stdin, "%d,%d,%d", &points[0][0], &points[0][1], &points[0][2]);
|
||||
int area = 6;
|
||||
bool iscorrect = true;
|
||||
cout<<no + 1<<" ";
|
||||
for(int i = 1; i < P; ++i)
|
||||
{
|
||||
fscanf(stdin, "%d,%d,%d", &points[i][0], &points[i][1], &points[i][2]);
|
||||
if(!iscorrect)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
area += 6;
|
||||
int j = 0;
|
||||
bool isconnected = false;
|
||||
while(j < i)
|
||||
{
|
||||
int d = dis(points[i], points[j]);
|
||||
if(d == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if(d == 1)
|
||||
{
|
||||
isconnected = true;
|
||||
area -= 2;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
if(j != i || !isconnected)
|
||||
{
|
||||
iscorrect = false;
|
||||
cout<<"NO "<<i + 1<<endl;
|
||||
}
|
||||
}
|
||||
if(iscorrect)
|
||||
{
|
||||
cout<<area<<endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
const int N = 21;
|
||||
const int M = 20;
|
||||
int ans[21][N] = {{1}};
|
||||
int BigNumPow(int x[], int n);
|
||||
int BigNumCopy(int x[], int y[]);
|
||||
int BigNumMul(int x[], int y[]);
|
||||
int BigNumAdd(int x[], int y[]);
|
||||
int main()
|
||||
{
|
||||
int n, t;
|
||||
int i, j;
|
||||
int BigNum[N], BigPow[N], BigStep[N], BigCycle[N], tem[N];
|
||||
memset(BigNum, 0, sizeof(BigNum));
|
||||
memset(BigPow, 0, sizeof(BigPow));
|
||||
memset(BigStep, 0, sizeof(BigStep));
|
||||
memset(BigCycle, 0, sizeof(BigCycle));
|
||||
BigNum[0] = 2;
|
||||
BigStep[0] = 6;
|
||||
BigStep[1] = 1;
|
||||
BigPow[0] = 1;
|
||||
BigCycle[0] = 4;
|
||||
for(i = 1; i < 20; ++i)
|
||||
{
|
||||
while(BigNum[i] == 0 || BigNum[i] > 2)
|
||||
{
|
||||
BigNumMul(BigNum, BigStep);
|
||||
BigNumAdd(BigPow, BigCycle);
|
||||
}
|
||||
BigNumCopy(ans[i], BigPow);
|
||||
BigNumPow(BigStep, 5);
|
||||
memset(tem, 0, sizeof(tem));
|
||||
for(j = 0; j < M; ++j)
|
||||
{
|
||||
tem[j] += BigCycle[j] * 5;
|
||||
if(tem[j] > 9)
|
||||
{
|
||||
tem[j + 1] += tem[j] / 10;
|
||||
tem[j] = tem[j] % 10;
|
||||
}
|
||||
}
|
||||
BigNumCopy(BigCycle, tem);
|
||||
}
|
||||
scanf("%d", &t);
|
||||
for(j = 1; j <= t; ++j)
|
||||
{
|
||||
scanf("%d", &n);
|
||||
printf("%d %d ", j, n);
|
||||
--n;
|
||||
i = M;
|
||||
while(ans[n][i] == 0)
|
||||
--i;
|
||||
while(i >= 0)
|
||||
printf("%d", ans[n][i--]);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int BigNumPow(int x[], int n)
|
||||
{
|
||||
int tmp[N];
|
||||
int res[N];
|
||||
BigNumCopy(tmp, x);
|
||||
memset(res, 0, sizeof(res));
|
||||
res[0] = 1;
|
||||
while(n > 0)
|
||||
{
|
||||
if(n & 1)
|
||||
{
|
||||
BigNumMul(res, tmp);
|
||||
}
|
||||
BigNumMul(tmp, tmp);
|
||||
n = n >> 1;
|
||||
}
|
||||
BigNumCopy(x, res);
|
||||
return 0;
|
||||
}
|
||||
int BigNumCopy(int x[], int y[])
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < N; ++i)
|
||||
x[i] = y[i];
|
||||
return 0;
|
||||
}
|
||||
int BigNumMul(int x[], int y[])
|
||||
{
|
||||
int i, j, k;
|
||||
int tem[N];
|
||||
int res[N];
|
||||
memset(res, 0, sizeof(res));
|
||||
for(i = 0; i < M; ++i)
|
||||
{
|
||||
memset(tem, 0, sizeof(tem));
|
||||
for(k = i, j = 0; k < M; ++j, ++k)
|
||||
{
|
||||
tem[k] += y[i] * x[j];
|
||||
tem[k + 1] += tem[k] / 10;
|
||||
tem[k] = tem[k] % 10;
|
||||
}
|
||||
BigNumAdd(res, tem);
|
||||
}
|
||||
BigNumCopy(x, res);
|
||||
return 0;
|
||||
}
|
||||
int BigNumAdd(int x[], int y[])
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < M; ++i)
|
||||
{
|
||||
x[i] += y[i];
|
||||
if(x[i] > 9)
|
||||
{
|
||||
x[i] -= 10;
|
||||
++x[i + 1];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
const int MAX = 2140000000;
|
||||
int f[1000001];
|
||||
void dfs(int p)
|
||||
{
|
||||
int i,sum = 1,temp;
|
||||
if(p%2 == 1)
|
||||
{
|
||||
for(i=1;i<p;i+=2)
|
||||
{
|
||||
temp = (p-i)/2;
|
||||
if(f[temp]==MAX)
|
||||
dfs(temp);
|
||||
sum += f[temp];
|
||||
}
|
||||
}
|
||||
else //even
|
||||
{
|
||||
for(i=0;i<p;i+=2)
|
||||
{
|
||||
temp = (p-i)/2;
|
||||
if(f[temp]==MAX)
|
||||
dfs(temp);
|
||||
sum += f[temp];
|
||||
}
|
||||
}
|
||||
f[p] = sum;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t,n,i,temp;
|
||||
for(i=0;i<=10000;i++)
|
||||
f[i] = MAX;
|
||||
f[0] = 0;
|
||||
f[1] = 1;
|
||||
f[2] = 2;
|
||||
f[3] = 2;
|
||||
int cas = 1;
|
||||
cin>>t;
|
||||
while(t--)
|
||||
{
|
||||
cin>>n;
|
||||
if(f[n] == MAX)
|
||||
dfs(n);
|
||||
cout<<cas++<<" "<<f[n]<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
typedef __int64 ll;
|
||||
typedef vector<int> vi;
|
||||
typedef vector<ll> vll;
|
||||
string buf;
|
||||
vll weight;
|
||||
int deal(){
|
||||
weight.clear();
|
||||
cin >> buf;
|
||||
int depth = 0;
|
||||
ll tmp;
|
||||
for (string::iterator ii = buf.begin(); ii != buf.end(); ii++){
|
||||
switch (*ii){
|
||||
case '[':
|
||||
{
|
||||
depth++;
|
||||
}break;
|
||||
case ']':
|
||||
{
|
||||
depth--;
|
||||
}break;
|
||||
case ',':break;
|
||||
default:
|
||||
{
|
||||
tmp = 0;
|
||||
while ('0' <= *ii && *ii <= '9') tmp = tmp * 10 + *ii - '0', ii++;
|
||||
ii--;
|
||||
weight.push_back(tmp << depth);
|
||||
}
|
||||
}
|
||||
}
|
||||
sort(weight.begin(), weight.end());
|
||||
int len = weight.size();
|
||||
int best = 0, cur = 1;
|
||||
weight.push_back(-1);
|
||||
for (int i = 0; i < len; i++){
|
||||
if (weight[i + 1] != weight[i]){
|
||||
best = max(best, cur);
|
||||
cur = 1;
|
||||
}
|
||||
else cur++;
|
||||
}
|
||||
return len - best;
|
||||
}
|
||||
int main(){
|
||||
int T;
|
||||
scanf("%d", &T);
|
||||
while (T--){
|
||||
cout << deal() << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <stack>
|
||||
using namespace std;
|
||||
#define MAXN 200010
|
||||
#define clr(x,k) memset((x),(k),sizeof(x))
|
||||
struct node
|
||||
{
|
||||
int st,to,next;
|
||||
}
|
||||
edge[MAXN];
|
||||
int n,m,ct,id;
|
||||
int head[MAXN],low[MAXN],dfn[MAXN],belong[MAXN],in[MAXN],to[MAXN];
|
||||
bool instack[MAXN];
|
||||
stack<int>q;
|
||||
void add_e(int i,int u,int v)
|
||||
{
|
||||
edge[i].st=u;
|
||||
edge[i].to=v;
|
||||
edge[i].next=head[u];
|
||||
head[u]=i;
|
||||
}
|
||||
void tarjan(int i)
|
||||
{
|
||||
int j;
|
||||
dfn[i]=low[i]=++id;
|
||||
q.push(i);
|
||||
instack[i]=1;
|
||||
for(int u=head[i]; ~u; u=edge[u].next)
|
||||
{
|
||||
j=edge[u].to;
|
||||
if(dfn[j]==0)
|
||||
{
|
||||
tarjan(j);
|
||||
if(low[i]>low[j])
|
||||
low[i]=low[j];
|
||||
}
|
||||
else if(instack[j]&&low[i]>low[j])
|
||||
low[i]=dfn[j];
|
||||
}
|
||||
if(dfn[i]==low[i])
|
||||
{
|
||||
ct++;
|
||||
do
|
||||
{
|
||||
j=q.top();
|
||||
q.pop();
|
||||
instack[j]=0;
|
||||
belong[j]=ct;
|
||||
}
|
||||
while(i!=j);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t,i,u,v,sum1,sum2;
|
||||
cin>>t;
|
||||
while(t--)
|
||||
{
|
||||
clr(head,-1);
|
||||
clr(low,0);
|
||||
clr(dfn,0);
|
||||
clr(belong,0);
|
||||
clr(in,0);
|
||||
clr(to,0);
|
||||
while(!q.empty())
|
||||
q.pop();
|
||||
cin>>n>>m;
|
||||
for(i=0; i<m; i++)
|
||||
{
|
||||
cin>>u>>v;
|
||||
add_e(i,u,v);
|
||||
}
|
||||
id=ct=0;
|
||||
for(i=1; i<=n; i++)
|
||||
{
|
||||
if(!dfn[i])
|
||||
tarjan(i);
|
||||
}
|
||||
if(ct==1)
|
||||
{
|
||||
cout<<0<<endl;
|
||||
continue;
|
||||
}
|
||||
for(i=1; i<=ct; i++)
|
||||
{
|
||||
in[i]=to[i]=0;
|
||||
}
|
||||
for(i=0; i<m; i++)
|
||||
{
|
||||
if(belong[edge[i].st]!=belong[edge[i].to])
|
||||
{
|
||||
in[belong[edge[i].st]]++;
|
||||
to[belong[edge[i].to]]++;
|
||||
}
|
||||
}
|
||||
sum1=sum2=0;
|
||||
for(i=1; i<=ct; i++)
|
||||
{
|
||||
if(in[i]==0)
|
||||
sum1++;
|
||||
if(to[i]==0)
|
||||
sum2++;
|
||||
}
|
||||
cout<<max(sum1,sum2)<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
int tot,list[20005],mark_dfs[20005],mark_gx[20005],n,m,k;
|
||||
struct dian
|
||||
{
|
||||
char need[50];
|
||||
char no[50];
|
||||
}cun1[20005];
|
||||
struct ren
|
||||
{
|
||||
int date,next;
|
||||
}cun[20005];
|
||||
void add(int a,int b)
|
||||
{
|
||||
cun[tot].date=b;
|
||||
cun[tot].next=list[a];
|
||||
list[a]=tot++;
|
||||
}
|
||||
int dfs(int x)
|
||||
{
|
||||
for(int i=list[x];i;i=cun[i].next)
|
||||
{
|
||||
int date=cun[i].date;
|
||||
if(mark_dfs[date]) continue;
|
||||
mark_dfs[date]=1;
|
||||
if(mark_gx[date]==-1||dfs(mark_gx[date]))
|
||||
{
|
||||
mark_gx[date]=x;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
memset(list,0,sizeof(list));
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
tot=1;
|
||||
clear();
|
||||
scanf("%d%d%d",&n,&m,&k);
|
||||
for(int i=1;i<=k;i++)
|
||||
{
|
||||
scanf("%s%s",cun1[i].need,cun1[i].no);
|
||||
}
|
||||
for(int i=1;i<k;i++)
|
||||
for(int j=i+1;j<=k;j++)
|
||||
{
|
||||
if((!strcmp(cun1[i].need,cun1[j].no)||!strcmp(cun1[i].no,cun1[j].need)))
|
||||
{
|
||||
add(i,j);
|
||||
add(j,i);
|
||||
}
|
||||
}
|
||||
memset(mark_gx,255,sizeof(mark_gx));
|
||||
int sum=0;
|
||||
for(int i=1;i<=k;i++)
|
||||
{
|
||||
memset(mark_dfs,0,sizeof(mark_dfs));
|
||||
sum+=dfs(i);
|
||||
}
|
||||
printf("%d\n",k-sum/2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
#include<string>
|
||||
#include<ctype.h>
|
||||
using namespace std;
|
||||
#define mod 10001
|
||||
long long in[10010];
|
||||
long long out[10010];
|
||||
long long exgcd(long long a,long long b,long long &x,long long &y)
|
||||
{
|
||||
if(!b)
|
||||
{
|
||||
x=1;
|
||||
y=0;
|
||||
return a;
|
||||
}
|
||||
long long tt=exgcd(b,a%b,x,y);
|
||||
long long t;
|
||||
t=x;
|
||||
x=y;
|
||||
y=(t-a/b*y);
|
||||
return tt;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++)
|
||||
scanf("%I64d",in+i);
|
||||
int ok=0;
|
||||
for(long long i=0;i<=10000;i++)
|
||||
{
|
||||
long long c=((in[1]-i*i*in[0]%10001)%10001+10001)%10001;
|
||||
long long a=i+1;
|
||||
long long b=-10001;
|
||||
long long x,y;
|
||||
long long d=exgcd(a,b,x,y);
|
||||
if(c%d)
|
||||
continue;
|
||||
long long t=abs(b/d);
|
||||
x*=c/d;
|
||||
x=(x%t+t)%t;
|
||||
for(int j=0;j<n;j++)
|
||||
{
|
||||
out[j]=(i*in[j]+x)%10001;
|
||||
if(j==n-1)
|
||||
{
|
||||
ok=1;
|
||||
break;
|
||||
}
|
||||
if(((i*out[j]+x)%10001)!=in[j+1])
|
||||
break;
|
||||
}
|
||||
if(ok)
|
||||
break;
|
||||
}
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
printf("%I64d\n",out[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const long long inf=1LL<<62;
|
||||
const int maxn=110;
|
||||
const int maxm=maxn*maxn*2;
|
||||
int n,cnt;
|
||||
long long h[maxn],s[maxm],dp[maxn][maxm],d;
|
||||
int q[maxm];
|
||||
void Init()
|
||||
{
|
||||
for(int i=0;i<=n;i++)
|
||||
for(int j=0;j<cnt;j++)
|
||||
dp[i][j]=inf;
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
Init();
|
||||
int ind=lower_bound(s,s+cnt,h[1])-s;
|
||||
dp[1][ind]=0;
|
||||
for(int i=2;i<=n;i++)
|
||||
{
|
||||
int pre=0,last=0,now=0;
|
||||
for(int j=0;j<cnt;j++)
|
||||
{
|
||||
while(now<cnt&&s[now]<=s[j]+d)
|
||||
{
|
||||
while(pre<last&&dp[i-1][q[last-1]]>=dp[i-1][now])
|
||||
last--;
|
||||
q[last++]=now++;
|
||||
}
|
||||
while(pre<last&&s[j]-d>s[q[pre]])
|
||||
pre++;
|
||||
dp[i][j]=abs(h[i]-s[j])+dp[i-1][q[pre]];
|
||||
}
|
||||
}
|
||||
ind=lower_bound(s,s+cnt,h[n])-s;
|
||||
if(dp[n][ind]>=inf)
|
||||
printf("impossible\n");
|
||||
else
|
||||
printf("%I64d\n",dp[n][ind]);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
cnt=0;
|
||||
scanf("%d%I64d",&n,&d);
|
||||
for(int i=1;i<=n;i++)
|
||||
scanf("%I64d",&h[i]);
|
||||
for(int i=1;i<=n;i++)
|
||||
for(long long j=0;j<n;j++)
|
||||
{
|
||||
s[cnt++]=h[i]-j*d;
|
||||
s[cnt++]=h[i]+j*d;
|
||||
}
|
||||
sort(s,s+cnt);
|
||||
cnt=unique(s,s+cnt)-s;
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int maxn = 50 + 5;
|
||||
const int maxc = 1000 + 1;
|
||||
int n;
|
||||
int x0[maxn], y0[maxn], z0[maxn], x1[maxn], y1[maxn], z1[maxn];
|
||||
int xs[maxn*2], ys[maxn*2], zs[maxn*2], nx, ny, nz;
|
||||
int color[maxn*2][maxn*2][maxn*2];
|
||||
int dx[] = {0, 0, 0, 0, -1, 1};
|
||||
int dy[] = {0, 0, -1, 1, 0, 0};
|
||||
int dz[] = {-1, 1, 0, 0, 0, 0};
|
||||
struct Cell
|
||||
{
|
||||
int x, y, z;
|
||||
Cell(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {}
|
||||
void setVis() const {
|
||||
color[x][y][z] = 2;
|
||||
}
|
||||
int volume() const {
|
||||
return (xs[x+1]-xs[x])*(ys[y+1]-ys[y])*(zs[z+1]-zs[z]);
|
||||
}
|
||||
Cell neighbor(int i) const {
|
||||
return Cell(x+dx[i], y+dy[i], z+dz[i]);
|
||||
}
|
||||
bool valid() const {
|
||||
return x>=0 && x<nx-1 && y>=0 && y<ny-1 && z>=0 && z<nz-1;
|
||||
}
|
||||
bool solid() const {
|
||||
return color[x][y][z] == 1;
|
||||
}
|
||||
int area(int i) const {
|
||||
if (dx[i] != 0) return (ys[y+1]-ys[y])*(zs[z+1]-zs[z]);
|
||||
else if(dy[i] != 0) return (xs[x+1]-xs[x])*(zs[z+1]-zs[z]);
|
||||
else return (xs[x+1]-xs[x])*(ys[y+1]-ys[y]);
|
||||
}
|
||||
bool getVis() const {
|
||||
return color[x][y][z] == 2;
|
||||
}
|
||||
};
|
||||
void discretize(int* x, int& n)
|
||||
{
|
||||
sort(x, x + n);
|
||||
n = (int)(unique(x, x+n) - x);
|
||||
}
|
||||
int ID(int* x, int n, int x0)
|
||||
{
|
||||
return (int)(lower_bound(x, x+n, x0) - x);
|
||||
}
|
||||
void floodfill(int& s, int& v)
|
||||
{
|
||||
s = v = 0;
|
||||
Cell c; c.setVis();
|
||||
queue<Cell> Q; Q.push(c);
|
||||
while (!Q.empty())
|
||||
{
|
||||
Cell now = Q.front(); Q.pop();
|
||||
v += now.volume();
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
Cell nxt = now.neighbor(i);
|
||||
if (!nxt.valid()) continue;
|
||||
if (nxt.solid()) s += now.area(i);
|
||||
else if(!nxt.getVis())
|
||||
{
|
||||
nxt.setVis();
|
||||
Q.push(nxt);
|
||||
}
|
||||
}
|
||||
}
|
||||
v = maxc*maxc*maxc - v;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t; scanf("%d", &t);
|
||||
while (t--)
|
||||
{
|
||||
scanf("%d", &n);
|
||||
nx = ny = nz = 2;
|
||||
xs[0] = ys[0] = zs[0] = 0;
|
||||
xs[1] = ys[1] = zs[1] = maxc;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d%d%d", &x0[i], &y0[i], &z0[i]);
|
||||
scanf("%d%d%d", &x1[i], &y1[i], &z1[i]);
|
||||
x1[i] += x0[i], y1[i] += y0[i], z1[i] += z0[i];
|
||||
xs[nx++] = x0[i], xs[nx++] = x1[i];
|
||||
ys[ny++] = y0[i], ys[ny++] = y1[i];
|
||||
zs[nz++] = z0[i], zs[nz++] = z1[i];
|
||||
}
|
||||
discretize(xs, nx), discretize(ys, ny), discretize(zs, nz);
|
||||
memset(color, 0, sizeof(color));
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
int X1 = ID(xs, nx, x0[i]), X2 = ID(xs, nx, x1[i]);
|
||||
int Y1 = ID(ys, ny, y0[i]), Y2 = ID(ys, ny, y1[i]);
|
||||
int Z1 = ID(zs, nz, z0[i]), Z2 = ID(zs, nz, z1[i]);
|
||||
for (int X = X1; X < X2; X++)
|
||||
{
|
||||
for (int Y = Y1; Y < Y2; Y++)
|
||||
{
|
||||
for (int Z = Z1; Z < Z2; Z++)
|
||||
{
|
||||
color[X][Y][Z] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int s, v;
|
||||
floodfill(s, v);
|
||||
printf("%d %d\n", s, v);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
int t,n,min,max_1,max_2,min_1,min_2;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
if(n<=1)
|
||||
return 0;
|
||||
else if(n==2)
|
||||
printf("1 1\n");
|
||||
else if(n==3)
|
||||
printf("7 7\n");
|
||||
else if(n==4)
|
||||
printf("4 11\n");
|
||||
else if(n==5)
|
||||
printf("2 71\n");
|
||||
else if(n==6)
|
||||
printf("6 111\n");
|
||||
else if(n==7)
|
||||
printf("8 711\n");
|
||||
else if(n==8)
|
||||
printf("10 1111\n");
|
||||
else if(n==9)
|
||||
printf("18 7111\n");
|
||||
else if(n==10)
|
||||
printf("22 11111\n");
|
||||
else if(n==11)
|
||||
printf("20 71111\n");
|
||||
else if(n==12)
|
||||
printf("28 111111\n");
|
||||
else if(n==13)
|
||||
printf("68 711111\n");
|
||||
else if(n==14)
|
||||
printf("88 1111111\n");
|
||||
else
|
||||
{
|
||||
max_1=(n-15)/7;
|
||||
max_2=(n-14)%7;
|
||||
if(max_2==1)
|
||||
printf("108");
|
||||
else if(max_2==2)
|
||||
printf("188");
|
||||
else if(max_2==3)
|
||||
printf("200");
|
||||
else if(max_2==4)
|
||||
printf("208");
|
||||
else if(max_2==5)
|
||||
printf("288");
|
||||
else if(max_2==6)
|
||||
printf("688");
|
||||
else if(max_2==0)
|
||||
printf("888");
|
||||
if(max_1)
|
||||
while(max_1--)
|
||||
printf("8");
|
||||
printf(" ");
|
||||
min_1=n/2-1;
|
||||
min_2=n%2;;
|
||||
if(min_2==0)
|
||||
printf("1");
|
||||
else if(min_2==1)
|
||||
printf("7");
|
||||
while(min_1--)
|
||||
printf("1");
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
#include<stdio.h>
|
||||
#include<cmath>
|
||||
#include<algorithm>
|
||||
#include<string.h>
|
||||
#include<iostream>
|
||||
#define eps 1e-8
|
||||
#define N 20010
|
||||
using namespace std;
|
||||
int Sig(double a)
|
||||
{
|
||||
return a<-eps?-1:a>eps;
|
||||
}
|
||||
struct Point
|
||||
{
|
||||
double x,y;
|
||||
void input()
|
||||
{
|
||||
scanf("%lf%lf",&x,&y);
|
||||
}
|
||||
Point(){}
|
||||
Point(double x0,double y0):x(x0),y(y0){}
|
||||
};
|
||||
int n1,n2;
|
||||
Point p1[110];
|
||||
Point p2[110];
|
||||
double Xmult(Point o,Point a,Point b)
|
||||
{
|
||||
return (a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
|
||||
}
|
||||
double Dis(Point a,Point b)
|
||||
{
|
||||
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
|
||||
}
|
||||
Point Intersection(Point u1,Point u2,Point v1,Point v2)
|
||||
{
|
||||
Point ret=u1;
|
||||
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/
|
||||
((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
|
||||
ret.x+=(u2.x-u1.x)*t;
|
||||
ret.y+=(u2.y-u1.y)*t;
|
||||
return ret;
|
||||
}
|
||||
void Judge(Point o,Point a,Point b,double &d)
|
||||
{
|
||||
Point tmp=Point(b.y-a.y+o.x,a.x-b.x+o.y);
|
||||
Point p=Intersection(o,tmp,a,b);
|
||||
if(Sig(Dis(a,b)-Dis(a,p)-Dis(b,p)))
|
||||
d=min(Dis(a,o),Dis(b,o));
|
||||
else
|
||||
d=fabs(Xmult(o,a,b))/Dis(a,b);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d",&n1);
|
||||
for(int i=0;i<n1;i++)
|
||||
p1[i].input();
|
||||
p1[n1]=p1[0];
|
||||
scanf("%d",&n2);
|
||||
for(int i=0;i<n2;i++)
|
||||
p2[i].input();
|
||||
p2[n2]=p2[0];
|
||||
double s1=999999999;
|
||||
double s2=999999999;
|
||||
double l;
|
||||
for(int i=0;i<n1;i++)
|
||||
{
|
||||
for(int j=0;j<n2;j++)
|
||||
{
|
||||
Judge(p1[i],p2[j],p2[j+1],l);
|
||||
s1=min(s1,l);
|
||||
}
|
||||
}
|
||||
for(int i=0;i<n2;i++)
|
||||
{
|
||||
for(int j=0;j<n1;j++)
|
||||
{
|
||||
Judge(p2[i],p1[j],p1[j+1],l);
|
||||
s2=min(s2,l);
|
||||
}
|
||||
}
|
||||
printf("%.8f\n",min(s1,s2)*0.5);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,165 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int v[10],n,a[8][2],rest[20],now[8],occ[23],tot[100];
|
||||
long long score[100];
|
||||
char s[21];
|
||||
int getrank(char ch){
|
||||
if(ch=='A')return 1;
|
||||
if(ch=='T')return 10;
|
||||
if(ch=='J')return 11;
|
||||
if(ch=='Q')return 12;
|
||||
if(ch=='K')return 13;
|
||||
return ch-'2'+2;
|
||||
}
|
||||
int getsuit(char ch){
|
||||
if(ch=='c')return 1;
|
||||
if(ch=='d')return 2;
|
||||
if(ch=='h')return 3;
|
||||
if(ch=='s')return 4;
|
||||
}
|
||||
bool can(int col,int mask)
|
||||
{
|
||||
for(int i=0;i<5;++i)
|
||||
for(int j=i+1;j<5;++j)
|
||||
if(now[i]==now[j])
|
||||
return false;
|
||||
for(int i=0;i<5;++i)
|
||||
if((mask&(1<<i))==0)
|
||||
{
|
||||
if(a[i][1]!=col)return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int j=0;j<5;++j)
|
||||
if(a[j][0]==now[i]&&a[j][1]==col)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool is0()
|
||||
{
|
||||
for(int i=1;i<=14;++i)
|
||||
if(occ[i]==2)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
bool is1()
|
||||
{
|
||||
int ret=0;
|
||||
for(int i=1;i<=14;++i)
|
||||
if(occ[i]==2)
|
||||
++ret;
|
||||
return ret==2;
|
||||
}
|
||||
bool is2()
|
||||
{
|
||||
for(int i=1;i<=14;++i)
|
||||
if(occ[i]==3)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
bool is3()
|
||||
{
|
||||
if(occ[1]&&occ[10]&&occ[11]&&occ[12]&&occ[13])
|
||||
return true;
|
||||
for(int i=1;i+5-1<=13;++i)
|
||||
if(occ[i]&&occ[i+1]&&occ[i+2]&&occ[i+3]&&occ[i+4])
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
bool is5()
|
||||
{
|
||||
for(int i=1;i<=14;++i)
|
||||
if (occ[i] == 3)
|
||||
for(int j=1;j<=14;++j)
|
||||
if(occ[j]==2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
bool is6()
|
||||
{
|
||||
for(int i=1;i<=14;++i)
|
||||
if(occ[i]==4)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
int getscore()
|
||||
{
|
||||
if(is6())return v[6];
|
||||
if(is5())return v[5];
|
||||
if(is3())return v[3];
|
||||
if(is2())return v[2];
|
||||
if(is1())return v[1];
|
||||
if(is0())return v[0];
|
||||
return 0;
|
||||
}
|
||||
int getsame(int *r)
|
||||
{
|
||||
if(occ[1]&&occ[10]&&occ[11]&&occ[12]&&occ[13])
|
||||
return 8;
|
||||
for(int i=1;i+5-1<=13;++i)
|
||||
if(occ[i]&&occ[i+1]&&occ[i+2]&&occ[i+3]&&occ[i+4])
|
||||
return 7;
|
||||
return 4;
|
||||
}
|
||||
void search(int u,int cnt, int mask)
|
||||
{
|
||||
if(u==5)
|
||||
{
|
||||
memset(occ,0,sizeof(occ));
|
||||
for(int i=0;i<5;++i)
|
||||
++occ[now[i]];
|
||||
tot[mask] += cnt;
|
||||
int tmp = 0;
|
||||
for(int i=1;i<=4;++i)
|
||||
if(can(i,mask)) {
|
||||
++ tmp;
|
||||
}
|
||||
if (is6() || is5()) tmp = 0;
|
||||
if (tmp) score[mask] += (long long)tmp * (long long)v[getsame(now)];
|
||||
score[mask]+=(long long)(cnt - tmp)*(long long)getscore();
|
||||
return;
|
||||
}
|
||||
now[u]=a[u][0];
|
||||
search(u + 1, cnt , mask);
|
||||
for(int i=1;i<=13;++i)
|
||||
if(rest[i])
|
||||
{
|
||||
--rest[i];
|
||||
now[u]=i;
|
||||
search(u+1,cnt*(rest[i]+1),mask|(1<<u));
|
||||
++rest[i];
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int _;
|
||||
for(scanf("%d",&_);_--;)
|
||||
{
|
||||
for(int i=0;i<9;++i)scanf("%d",v+i);
|
||||
scanf("%d%*c",&n);
|
||||
for (int k = 0; k < n; ++ k) {
|
||||
for(int i=1;i<=13;++i)rest[i]=4;
|
||||
for(int i=0;i<5;++i)
|
||||
{
|
||||
scanf("%s",s);
|
||||
a[i][0]=getrank(s[0]);
|
||||
--rest[a[i][0]];
|
||||
a[i][1]=getsuit(s[1]);
|
||||
}
|
||||
memset(tot, 0, sizeof(tot));
|
||||
memset(score, 0, sizeof(score));
|
||||
search(0,1,0);
|
||||
double ans = 0.;
|
||||
for (int i = 0; i < (1<<5); ++ i)
|
||||
if (tot[i]) {
|
||||
ans = max(ans, (double)score[i] / (double)tot[i]);
|
||||
}
|
||||
printf("%.8f\n",ans);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<functional>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
bool flag[50];
|
||||
struct time
|
||||
{
|
||||
int start;
|
||||
int end;
|
||||
bool operator <(const time a)const
|
||||
{
|
||||
if(a.end==end)
|
||||
return a.start<start;
|
||||
return a.end<end;
|
||||
}
|
||||
}now,temp;
|
||||
int main()
|
||||
{
|
||||
priority_queue<time>Q;
|
||||
int i,n,j=0,t;
|
||||
while(++j)
|
||||
{
|
||||
memset(flag,false,sizeof(flag));
|
||||
scanf("%d",&n);
|
||||
if(!n)
|
||||
return 0;
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
scanf("%d%d",&now.start,&now.end);
|
||||
now.start*=2,now.end*=2;
|
||||
Q.push(now);
|
||||
}
|
||||
now.start=now.end=8*2;
|
||||
int count=0;
|
||||
while(!Q.empty())
|
||||
{
|
||||
temp=Q.top();
|
||||
Q.pop();
|
||||
for(t=temp.start;t<=temp.end-1;t++)
|
||||
if(flag[t]==false)
|
||||
{
|
||||
now.start=max(t,now.start+1);
|
||||
flag[t]=true;
|
||||
count++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("On day %d Emma can attend as many as %d parties.\n",j,count);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cmath>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<algorithm>
|
||||
#include<memory.h>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
int cnt,mark;
|
||||
char vis[12][12];
|
||||
int map[12][12];
|
||||
struct Node
|
||||
{
|
||||
int x,y;
|
||||
}node[102];
|
||||
int Judge(int x,int y,int z)
|
||||
{
|
||||
int i,j,n,m;
|
||||
for(i=0;i<9;i++)
|
||||
{
|
||||
if(map[i][y]==z && i!=x)
|
||||
return 1;
|
||||
}
|
||||
for(j=0;j<9;j++)
|
||||
{
|
||||
if(map[x][j]==z && j!=y)
|
||||
return 1;
|
||||
}
|
||||
for(n=0,i=x/3*3;n<3;n++,i++)
|
||||
for(m=0,j=y/3*3;m<3;m++,j++)
|
||||
{
|
||||
if(i==x && j==y)
|
||||
continue;
|
||||
if(map[i][j]==z)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int judge()
|
||||
{
|
||||
int i,j;
|
||||
for(i=0;i<9;i++)
|
||||
for(j=0;j<9;j++)
|
||||
{
|
||||
if(!map[i][j])
|
||||
continue;
|
||||
if(Judge(i,j,map[i][j]))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void dfs(int n)
|
||||
{
|
||||
int i;
|
||||
if(n==cnt)
|
||||
{
|
||||
mark=1;
|
||||
return;
|
||||
}
|
||||
if(mark)
|
||||
return;
|
||||
for(i=1;i<=9;i++)
|
||||
{
|
||||
if(Judge(node[n].x,node[n].y,i))
|
||||
continue;
|
||||
map[node[n].x][node[n].y]=i;
|
||||
dfs(n+1);
|
||||
if(mark)
|
||||
return;
|
||||
map[node[n].x][node[n].y]=0;
|
||||
}
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
cnt=0;
|
||||
memset(map,0,sizeof(map));
|
||||
memset(vis,0,sizeof(vis));
|
||||
memset(node,0,sizeof(node));
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int k,i,j;
|
||||
bool f=false;
|
||||
scanf("%d",&k);
|
||||
while(k--)
|
||||
{
|
||||
if(f)
|
||||
printf("\n");
|
||||
f=true;
|
||||
clear();
|
||||
for(i=0;i<9;i++)
|
||||
{
|
||||
scanf("%s",vis[i]);
|
||||
}
|
||||
for(i=0;i<9;i++)
|
||||
{
|
||||
for(j=0;j<9;j++)
|
||||
{
|
||||
map[i][j]=vis[i][j]-'0';
|
||||
if(!map[i][j])
|
||||
{
|
||||
node[cnt].x=i;
|
||||
node[cnt].y=j;
|
||||
cnt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(judge())
|
||||
{
|
||||
printf("Could not complete this grid.\n");
|
||||
continue;
|
||||
}
|
||||
mark=0;
|
||||
dfs(0);
|
||||
if(mark)
|
||||
{
|
||||
for(i=0;i<9;i++)
|
||||
{
|
||||
for(j=0;j<9;j++)
|
||||
printf("%d",map[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Could not complete this grid.\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
#include <cstring>
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
int map[700][700];
|
||||
int vis[700][700];
|
||||
int ansx,ansy;
|
||||
int ansd,anssum;
|
||||
int n,m;
|
||||
int res=0;
|
||||
int xx[4]={0,-1,1,0};
|
||||
int yy[4]={1,0,0,-1};
|
||||
void dfs(int x,int y,int ans,int pre)
|
||||
{
|
||||
if(pre==-1)
|
||||
{
|
||||
int i;
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
int nx=x+xx[i];
|
||||
int ny=y+yy[i];
|
||||
if(nx<0||nx>=n||ny<0||ny>=m)
|
||||
continue;
|
||||
if(vis[nx][ny])
|
||||
continue;
|
||||
vis[nx][ny]=1;
|
||||
dfs(nx,ny,ans+1,i);
|
||||
vis[nx][ny]=0;
|
||||
if(res>anssum)
|
||||
{
|
||||
ansx=x;
|
||||
ansy=y;
|
||||
ansd=i;
|
||||
anssum=res;
|
||||
}
|
||||
}
|
||||
return ;
|
||||
}
|
||||
int i;
|
||||
int flag=1;
|
||||
int nx=x+xx[pre];
|
||||
int ny=y+yy[pre];
|
||||
if(nx>=0&&nx<n&&ny>=0&&ny<m&&!vis[nx][ny])
|
||||
{
|
||||
vis[nx][ny]=1;
|
||||
dfs(nx,ny,ans+1,pre);
|
||||
vis[nx][ny]=0;
|
||||
flag=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
if(i==pre)
|
||||
continue;
|
||||
nx=x+xx[i];
|
||||
ny=y+yy[i];
|
||||
if(nx<0||nx>=n||ny<0||ny>=m)
|
||||
continue;
|
||||
if(vis[nx][ny])
|
||||
continue;
|
||||
vis[nx][ny]=1;
|
||||
dfs(nx,ny,ans+1,i);
|
||||
vis[nx][ny]=0;
|
||||
flag=0;
|
||||
}
|
||||
}
|
||||
if(flag)
|
||||
{
|
||||
if(res<ans)
|
||||
res=ans;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int cas=0;
|
||||
while(scanf("%d%d",&n,&m)!=EOF)
|
||||
{
|
||||
cas++;
|
||||
if(n==0&&m==0)
|
||||
break;
|
||||
int r;
|
||||
scanf("%d",&r);
|
||||
int i;
|
||||
memset(vis,0,sizeof(vis));
|
||||
for(i=0;i<r;i++)
|
||||
{
|
||||
int x,y;
|
||||
scanf("%d%d",&x,&y);
|
||||
vis[x][y]=1;
|
||||
}
|
||||
int j;
|
||||
anssum=-1;
|
||||
res=0;
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
for(j=0;j<m;j++)
|
||||
{
|
||||
if(!vis[i][j])
|
||||
{
|
||||
vis[i][j]=1;
|
||||
dfs(i,j,1,-1);
|
||||
vis[i][j]=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("Case %d: %d %d %d ",cas,anssum,ansx,ansy);
|
||||
if(ansd==0)
|
||||
printf("E\n");
|
||||
else if(ansd==1)
|
||||
printf("N\n");
|
||||
else if(ansd==2)
|
||||
printf("S\n");
|
||||
else if(ansd==3)
|
||||
printf("W\n");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <stack>
|
||||
using namespace std ;
|
||||
typedef pair<int,int> pii ;
|
||||
#define X first
|
||||
#define Y second
|
||||
const int N = 1010 ;
|
||||
const int inf = 1e9+7 ;
|
||||
typedef long long LL;
|
||||
int n , xx[N] , pre[N] , ww[N] , tot ;
|
||||
vector<pii>g[N];
|
||||
struct node {
|
||||
int a , c , d , id ;
|
||||
node(){};
|
||||
node( int a , int c , int d , int id ):a(a),c(c),d(d),id(id){}
|
||||
bool operator < ( const node &A ) const {
|
||||
if( d != A.d ) return d > A.d ;
|
||||
else if( c != A.c ) return c > A.c ;
|
||||
else {
|
||||
stack<int>s1 , s2; int id1 = id , id2 = A.id ;
|
||||
while( id1 != -1 ) { s1.push( xx[id1] ); id1 = pre[id1] ; }
|
||||
while( id2 != -1 ) { s2.push( xx[id2] ); id2 = pre[id2] ; }
|
||||
while( !s1.empty() ) {
|
||||
int a = s1.top() ; s1.pop() ;
|
||||
int b = s2.top() ; s2.pop() ;
|
||||
if( a > b ) return true ;
|
||||
}
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
};
|
||||
int bestpath[N] , bestcnt , bestcost ;
|
||||
int tmppath[N] , tmpcnt , tmpcost ;
|
||||
void Choose_best( int id , int cnt ) {
|
||||
if( tmpcost > bestcost ) return ;
|
||||
stack<int>s ;
|
||||
while( id != -1 ) { s.push( xx[id] ); id = pre[id] ; }
|
||||
tmpcnt = 0 ;
|
||||
while( !s.empty() ) { tmppath[tmpcnt++] = s.top() ; s.pop() ; }
|
||||
if( tmpcost < bestcost ) {
|
||||
bestcnt = tmpcnt ;
|
||||
bestcost = tmpcost ;
|
||||
for( int i = 0 ; i < tmpcnt ; ++i ) bestpath[i] = tmppath[i] ;
|
||||
return ;
|
||||
}
|
||||
if( cnt > bestcnt ) return ;
|
||||
else if( cnt < bestcnt ) {
|
||||
bestcnt = tmpcnt ;
|
||||
for( int i = 0 ; i < tmpcnt ; ++i ) bestpath[i] = tmppath[i] ;
|
||||
} else {
|
||||
for( int i = 0 ; i < tmpcnt ; ++i ) {
|
||||
if( bestpath[i] > tmppath[i] ) {
|
||||
for( int j = i ; j < bestcnt ; ++j ) {
|
||||
bestpath[j] = tmppath[j] ;
|
||||
}
|
||||
} else if ( bestpath[i] < tmppath[i] ) {
|
||||
break ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int dis[N] ;
|
||||
void dij() {
|
||||
memset( dis , 0x3f , sizeof dis );
|
||||
priority_queue<node>que;
|
||||
tot = 0 ; xx[tot] = 0 , pre[tot] = -1 ; tot++ ;
|
||||
que.push( node(0,0,0,tot-1) );
|
||||
dis[0] = 0 ;
|
||||
while( !que.empty() ) {
|
||||
int u = que.top().a , cnt = que.top().c , cost = que.top().d , id = que.top().id ;
|
||||
que.pop();
|
||||
if( cost > dis[u] ) continue ;
|
||||
if( u == 1 ) {
|
||||
if( dis[u] ) {
|
||||
tmpcost = dis[u] ;
|
||||
Choose_best( id , cnt );
|
||||
}
|
||||
return ;
|
||||
}
|
||||
for( int i = 0 ; i < g[u].size() ; ++i ) {
|
||||
int v = g[u][i].X , w = ww[g[u][i].Y] ;
|
||||
if( dis[v] > dis[u] + w ) {
|
||||
dis[v] = dis[u] + w ;
|
||||
xx[tot] = v ; pre[tot] = id ; tot++;
|
||||
que.push( node( v , cnt+1 , dis[v] , tot - 1 ) ) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void Gao() {
|
||||
dij();
|
||||
for( int i = 0 ; i < n ; ++i ) {
|
||||
int cc = ww[i] ; ww[i] = 0 ;
|
||||
dij();
|
||||
for( int j = i + 1 ; j < n ; ++j ) {
|
||||
int dd = ww[j] ; ww[j] = 0 ;
|
||||
dij(); ww[j] = dd ;
|
||||
}
|
||||
ww[i] = cc ;
|
||||
}
|
||||
for( int i = 0 ; i < bestcnt ; ++i ) cout << bestpath[i] << ' ' ;
|
||||
cout << bestcost << endl ;
|
||||
}
|
||||
int Run() {
|
||||
while( cin >> n && n ) {
|
||||
bestcost = bestcnt = inf ;
|
||||
for( int i = 0 ; i < N ; ++i ) g[i].clear();
|
||||
for( int i = 0 ; i < n ; ++i ) {
|
||||
int u , v , w ; cin >> u >> v >> w ;
|
||||
ww[i] = w ;
|
||||
g[u].push_back(pii(v,i));
|
||||
g[v].push_back(pii(u,i));
|
||||
}
|
||||
Gao();
|
||||
}
|
||||
return 0 ;
|
||||
}
|
||||
int main() {
|
||||
ios::sync_with_stdio(0);
|
||||
return Run();
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<math.h>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int n;
|
||||
struct acc{
|
||||
int meb;
|
||||
int bam;
|
||||
double mo;
|
||||
}arr[110];
|
||||
int judge(int ac, int b){
|
||||
int i;
|
||||
for(i = 0; i < n; i++)
|
||||
{
|
||||
if(arr[i].meb == ac && arr[i].bam == b)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
char cao[100];
|
||||
while(cin >> n)
|
||||
{
|
||||
if(n == 0)
|
||||
{
|
||||
cout << "goodbye" << endl;
|
||||
break;
|
||||
}
|
||||
char s;
|
||||
for(i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d%c%d%lf", &arr[i].meb, &s, &arr[i].bam, &arr[i].mo);
|
||||
}
|
||||
while(1){
|
||||
scanf("%s", cao);
|
||||
int l = strlen(cao);
|
||||
if(strcmp(cao, "end") == 0)
|
||||
break;
|
||||
int ac, bank;
|
||||
double money;
|
||||
if(cao[0] == 'c')
|
||||
{
|
||||
scanf("%d%c%d", &ac, &s, &bank);
|
||||
if(judge(ac, bank) == -1)
|
||||
{
|
||||
arr[n].meb = ac;
|
||||
arr[n].bam = bank;
|
||||
arr[n++].mo = 0.0;
|
||||
cout << "create: ok" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "create: already exists" << endl;
|
||||
}
|
||||
}
|
||||
else if(cao[0] == 'd')
|
||||
{
|
||||
scanf("%d%c%d%lf", &ac, &s, &bank, &money);
|
||||
int num = judge(ac, bank);
|
||||
if(num == -1)
|
||||
{
|
||||
printf("deposit %.2lf: no such account\n", money);
|
||||
}
|
||||
else{
|
||||
arr[num].mo += money;
|
||||
printf("deposit %.2lf: ok\n", money);
|
||||
}
|
||||
}
|
||||
else if(cao[0] == 'w')
|
||||
{
|
||||
scanf("%d%c%d%lf", &ac, &s, &bank, &money);
|
||||
int num = judge(ac, bank);
|
||||
if(num == -1){
|
||||
printf("withdraw %.2lf: no such account\n", money);
|
||||
}
|
||||
else{
|
||||
if(arr[num].mo < money)
|
||||
{
|
||||
printf("withdraw %.2lf: insufficient funds\n", money);
|
||||
}
|
||||
else{
|
||||
arr[num].mo -= money;
|
||||
printf("withdraw %.2lf: ok\n", money);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(cao[0] == 't')
|
||||
{
|
||||
int ac1, bank1;
|
||||
char s1;
|
||||
scanf("%d%c%d%d%c%d%lf", &ac, &s, &bank, &ac1, &s1, &bank1, &money);
|
||||
int num = judge(ac, bank);
|
||||
int num1 = judge(ac1, bank1);
|
||||
if(num == -1 || num1 == -1){
|
||||
printf("transfer %.2lf: no such account\n", money);
|
||||
}
|
||||
else{
|
||||
if(arr[num].meb == arr[num1].meb && arr[num].bam == arr[num1].bam)
|
||||
{
|
||||
printf("transfer %.2lf: same account\n", money);
|
||||
}
|
||||
else if(arr[num].mo < money)
|
||||
{
|
||||
printf("transfer %.2lf: insufficient funds\n", money);
|
||||
}
|
||||
else{
|
||||
arr[num].mo -= money;
|
||||
arr[num1].mo += money;
|
||||
if(arr[num].bam == arr[num1].bam)
|
||||
{
|
||||
printf("transfer %.2lf: ok\n", money);
|
||||
}
|
||||
else{
|
||||
printf("transfer %.2lf: interbank\n", money);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << "end" << endl << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int MAX = 1010;
|
||||
struct BID{
|
||||
char name[25], type[5];
|
||||
double price;
|
||||
};
|
||||
BID bid[MAX], buy[MAX], sell[MAX];
|
||||
int main(){
|
||||
int N, flag;
|
||||
char issuer[12];
|
||||
while(scanf("%d%s", &N, issuer) != EOF && N)
|
||||
{
|
||||
int cntb = 0, cnts = 0;
|
||||
for(int i = 0; i < N; ++i)
|
||||
{
|
||||
scanf("%s%s%lf", bid[i].name, bid[i].type, &bid[i].price);
|
||||
if(strcmp(bid[i].type, "buy") == 0)
|
||||
buy[cntb++] = bid[i];
|
||||
if(strcmp(bid[i].type, "sell") == 0)
|
||||
sell[cnts++] = bid[i];
|
||||
}
|
||||
printf("%s\n", issuer);
|
||||
for(int i = 0; i < N; ++i)
|
||||
{
|
||||
flag = 0; printf("%s:", bid[i].name);
|
||||
if(strcmp(bid[i].type, "buy") == 0)
|
||||
{
|
||||
for(int j = 0; j < cnts; ++j)
|
||||
{
|
||||
if(sell[j].price <= bid[i].price)
|
||||
{
|
||||
printf(" %s", sell[j].name);
|
||||
flag = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(strcmp(bid[i].type, "sell") == 0)
|
||||
{
|
||||
for(int j = 0; j < cntb; ++j)
|
||||
{
|
||||
if(buy[j].price >= bid[i].price)
|
||||
{
|
||||
printf(" %s", buy[j].name);
|
||||
flag = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!flag)
|
||||
printf(" NO-ONE");
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int N = 50009;
|
||||
int a[N];
|
||||
int main(){
|
||||
int n, i, j, k;
|
||||
while(~scanf("%d", &n) && n)
|
||||
{
|
||||
for(i = 0; i < n; ++i)
|
||||
scanf("%d", &a[i]);
|
||||
sort(a, a+n);
|
||||
printf("%d-A", a[0]);
|
||||
for(i = n-1; i >= 1; --i){
|
||||
if(i&1)
|
||||
printf(" %d-B", a[i]);
|
||||
else
|
||||
printf(" %d-A",a[i]);
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const __int64 MAX_N = 5;
|
||||
const __int64 MOD = 1000000007;
|
||||
__int64 N;
|
||||
void multipy( __int64 a[MAX_N][MAX_N], __int64 b[MAX_N][MAX_N], __int64 c[MAX_N][MAX_N] ){
|
||||
for( __int64 i = 1; i <= 4; i++ ){
|
||||
for( __int64 j = 1; j <= 4; j++ ){
|
||||
c[i][j] = 0;
|
||||
for( __int64 k = 1; k <= 4; k++ ){
|
||||
c[i][j] = ( c[i][j] + a[i][k] * b[k][j] % MOD ) % MOD;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void get_matrix_pow( __int64 a[MAX_N][MAX_N], __int64 n ){
|
||||
__int64 ans[MAX_N][MAX_N] = {0};
|
||||
__int64 temp[MAX_N][MAX_N];
|
||||
for( __int64 i = 1; i <= 4; i++ ) ans[i][i] = 1;
|
||||
while( n ){
|
||||
if( n % 2 == 1 ){
|
||||
multipy( ans, a, temp );
|
||||
memcpy( ans, temp, sizeof( __int64 ) * MAX_N * MAX_ );
|
||||
}
|
||||
multipy( a, a, temp );
|
||||
memcpy( a, temp, sizeof( __int64 ) * MAX_N * MAX_ );
|
||||
n /= 2;
|
||||
}
|
||||
memcpy( a, ans, sizeof( __int64 ) * MAX_N * MAX_ );
|
||||
}
|
||||
__int64 solve( __int64 n ){
|
||||
__int64 a[MAX_N][MAX_N] = {0};
|
||||
if( n < 0 ){
|
||||
return 0;
|
||||
}
|
||||
if( n == 0 ){
|
||||
return 1;
|
||||
}else if( n == 1 ){
|
||||
return 2;
|
||||
}
|
||||
a[1][1] = 0;a[1][2] = 1;a[1][3] = 0;a[1][4] = 0;
|
||||
a[2][1] = 0;a[2][2] = 0;a[2][3] = 1;a[2][4] = 0;
|
||||
a[3][1] = 1;a[3][2] = 1;a[3][3] = 1;a[3][4] = 0;
|
||||
a[4][1] = 0;a[4][2] = 0;a[4][3] = 1;a[4][4] = 1;
|
||||
get_matrix_pow( a, n - 1 );
|
||||
__int64 ans = 0;
|
||||
__int64 b[MAX_N];
|
||||
b[1] = b[2] = b[3] = 1;
|
||||
b[4] = 2;
|
||||
for( __int64 i = 1; i <= 4; i++ ){
|
||||
ans = ( ans + a[4][i] * b[i] % MOD ) % MOD;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main(){
|
||||
__int64 A, B;
|
||||
while( scanf( "%I64d%I64d", &A, &B ) != EOF ){
|
||||
printf( "%I64d\n", ( solve( B ) - solve( A - 1 ) + MOD ) % MOD );
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#define MAXN 200050
|
||||
#define lchild rt << 1, l, m
|
||||
#define rchild rt << 1 | 1, m + 1, r
|
||||
using namespace std;
|
||||
int h, w, n, m;
|
||||
class Segment_Tree{
|
||||
private:
|
||||
int node[MAXN << 2];
|
||||
void push_up(int rt){
|
||||
node[rt] = min(node[rt << 1], node[rt << 1 | 1]);
|
||||
}
|
||||
public:
|
||||
void build(){
|
||||
memset(node, 0, sizeof(node));
|
||||
}
|
||||
int update(int val, int rt = 1, int l = 1, int r = n){
|
||||
if (l == r){
|
||||
if (node[rt] + val <= w){
|
||||
node[rt] += val;
|
||||
return l;
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
int m = (l + r) >> 1, ret = 0;
|
||||
if (node[rt << 1] + val <= w) ret += update(val, lchild);
|
||||
else ret += update(val, rchild);
|
||||
push_up(rt);
|
||||
return ret;
|
||||
}
|
||||
int query(){
|
||||
return node[1];
|
||||
}
|
||||
}st;
|
||||
int main(){
|
||||
int wi;
|
||||
while (scanf("%d %d %d", &h, &w, &m) != EOF){
|
||||
st.build();
|
||||
n = min(h, m);
|
||||
while (m--){
|
||||
scanf("%d", &wi);
|
||||
if (st.query() + wi <= w){
|
||||
printf("%d\n", st.update(wi));
|
||||
}
|
||||
else printf("-1\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
int a[100],n;
|
||||
int main(){
|
||||
while(scanf("%d",&n)!=EOF){
|
||||
memset(a,0,sizeof(a));
|
||||
for(int i=1;i<=n;i++){
|
||||
scanf("%d",&a[i]);
|
||||
}
|
||||
sort(a,a+n+1);
|
||||
int k=1;
|
||||
int fro=a[1];
|
||||
int site=1;
|
||||
int left=0;
|
||||
for(int i=2;i<=n;i++){
|
||||
if(a[i]>fro&&i-site+left>=k+1&&a[i]!=a[i-1]){
|
||||
fro=a[i];
|
||||
left=i-site-k-1+left;
|
||||
site=i;
|
||||
k++;
|
||||
}else continue;
|
||||
}
|
||||
printf("%d\n",k);
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue