parent
2b2ae8c977
commit
55762be649
|
@ -0,0 +1,151 @@
|
|||
#include "iostream"
|
||||
#include "algorithm"
|
||||
#include "cstdio"
|
||||
#include "cstdlib"
|
||||
#include "cstring"
|
||||
#include "iterator"
|
||||
using namespace std;
|
||||
#define $FOREACH(a, b) for(__typeof(b.begin())a=b.begin(), a##ed__=b.end(); a!=a##ed__; a++)
|
||||
#define $FOREACHC(a, b, cond) for (__typeof(b) a=b;(cond); b++, a=b)
|
||||
#define $QUEUEPROC(cond, U, V) \
|
||||
for(int u=(U);cond;u=(U))\
|
||||
$FOREACH(v, V)
|
||||
#define $STACKPROC(cond, U, V) \
|
||||
for (int u=(U); cond; u=(U))\
|
||||
for (bool child=false; !child; child=true)\
|
||||
$FOREACHC(v, (V), !child)
|
||||
#define $ASSERT(a) ({fprintf(stderr,"%s:%d: %s\n", __FILE__, __LINE__, #a); exit(-1);})
|
||||
char* ran = " 23456789TJQKA";
|
||||
char* suit = "SHDC";
|
||||
int revers[256];
|
||||
int rev[256];
|
||||
int card[200];
|
||||
int tot=0;
|
||||
int dist[4];
|
||||
int ihcp[15];
|
||||
inline void pre()
|
||||
{
|
||||
for (int i=0; i<15; i++) revers[(int)ran[i]]=i,
|
||||
ihcp[i] = (i>10?i-10:0);
|
||||
for (int i=0; i<4; i++) rev[(int)suit[i]]=i;
|
||||
}
|
||||
int hcp;
|
||||
bool read()
|
||||
{
|
||||
char s[1024];
|
||||
if (!gets(s)) return false;
|
||||
fill(dist, dist+4, 0);
|
||||
hcp=0;
|
||||
for (int i=0, j=0; j<13; i+=3, j++)
|
||||
{
|
||||
card[j] = rev[(int)s[i]] * 20 + revers[(int)s[i+1]],
|
||||
dist[rev[(int)s[i]]]++,
|
||||
hcp+=ihcp[revers[(int)s[i+1]]];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
char suits[][1000]={
|
||||
"Spades",
|
||||
"Hearts",
|
||||
"Diamonds",
|
||||
"Clubs",
|
||||
"No Trump",
|
||||
"Pass",
|
||||
};
|
||||
inline void bid(int level, int suit)
|
||||
{
|
||||
static int ncase = 0;
|
||||
if (suit < 5)
|
||||
printf("Hand #%d: %d %s\n", ++ncase, level, suits[suit]);
|
||||
else
|
||||
printf("Hand #%d: %s\n", ++ncase, suits[suit]);
|
||||
}
|
||||
inline bool check(int *z, int a, int b, int c, int d)
|
||||
{
|
||||
if (z[0] != a || z[1]!=b || z[2]!=c || z[3]!=d) return false;
|
||||
return true;
|
||||
}
|
||||
inline bool stopped(int *dist)
|
||||
{
|
||||
int stop = 0;
|
||||
for (int i=0; i<4; i++)
|
||||
{
|
||||
if (
|
||||
find(card, card+13, i*20+14)!=card+13 ||
|
||||
find(card, card+13, i*20+13)!=card+13 && dist[i]>1||
|
||||
find(card, card+13, i*20+12)!=card+13 && dist[i]>2||
|
||||
find(card, card+13, i*20+11)!=card+13 && dist[i]>3
|
||||
) stop++;
|
||||
}
|
||||
return stop>=3;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
pre();
|
||||
while (read())
|
||||
{
|
||||
int r = find(dist, dist+4, *max_element(dist, dist+4))-dist;
|
||||
int m = dist[r];
|
||||
#ifdef Debug
|
||||
for (int i=0; i<4; i++) printf("%d ", dist[i]);
|
||||
puts("");
|
||||
printf("%d %d %d\n", m, r, hcp);
|
||||
#endif
|
||||
int z[4];
|
||||
copy(dist, dist+4, z);
|
||||
sort(z, z+4, greater<int>());
|
||||
if (hcp >= 10 && m>=8)
|
||||
bid(4, r);
|
||||
else if (hcp >= 10 && hcp <= 13 && m == 7)
|
||||
bid(3, r);
|
||||
else if (hcp >= 8 && hcp <= 9 && (dist[0] >= 7 || dist[1]>=7))
|
||||
{
|
||||
if (dist[1] < 7 || dist[0] >= dist[1])
|
||||
bid(2, 0);
|
||||
else
|
||||
bid(2, 1);
|
||||
}
|
||||
else if (hcp >= 8 && hcp <= 11 && (dist[0]>=6 || dist[1]>=6))
|
||||
{
|
||||
if (dist[1] < 6 || dist[0] >= dist[1])
|
||||
bid(2, 0);
|
||||
else
|
||||
bid(2, 1);
|
||||
}
|
||||
else if (hcp >= 11 && hcp <= 15 &&
|
||||
(check(z,4,4,4,1)||check(z, 5,4,4,0))
|
||||
&& dist[0] >= 4)
|
||||
bid(2, 2);
|
||||
else if (hcp >=15 && hcp <= 17 &&
|
||||
(check(z, 4,3,3,3) || check(z, 4,4,3,2) || check(z,5,3,3,2))&&
|
||||
stopped(dist))
|
||||
bid(1, 4);
|
||||
else if (hcp >= 20 && hcp <= 22 && (check(z, 4,3,3,3) || check(z, 4,4,3,2) || check(z,5,3,3,2)))
|
||||
bid(2, 4);
|
||||
else if (hcp >= 22)
|
||||
bid(2, 3);
|
||||
else if (hcp >= 13 && hcp <= 16 && (dist[0]>=5 || dist[1]>=5))
|
||||
{
|
||||
if (dist[1] < 5 || dist[0] >= dist[1])
|
||||
bid(1, 0);
|
||||
else
|
||||
bid(1, 1);
|
||||
}
|
||||
else if (hcp >= 13 && hcp <= 16)
|
||||
if (dist[2] >= dist[3])
|
||||
bid(1, 2);
|
||||
else
|
||||
bid(1, 3);
|
||||
else if (hcp >= 17)
|
||||
{
|
||||
for (int i=3; i>=0; i--)
|
||||
if (dist[i] == m)
|
||||
{
|
||||
bid(1, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
bid(0, 5);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int n,w,d,s,cha,yuan,ans;
|
||||
while((cin>>n>>w>>d>>s)>0)
|
||||
{
|
||||
yuan=w*n*(n-1)/2;
|
||||
cha=yuan-s;
|
||||
ans=cha/d;
|
||||
if(!ans)
|
||||
cout<<n<<endl;
|
||||
else
|
||||
cout<<ans<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
#include "iostream"
|
||||
#include "algorithm"
|
||||
#include "cstdio"
|
||||
#include "cstdlib"
|
||||
#include "cstring"
|
||||
using namespace std;
|
||||
#define $FOREACH(a, b) for(__typeof(b.begin())a=b.begin(), a##ed__=b.end(); a!=a##ed__; a++)
|
||||
#define $FOREACHC(a, b, cond) for (__typeof(b) a=b;(cond); b++, a=b)
|
||||
#define $QUEUEPROC(cond, U, V) \
|
||||
for(int u=(U);cond;u=(U))\
|
||||
$FOREACH(v, V)
|
||||
#define $STACKPROC(cond, U, V) \
|
||||
for (int u=(U); cond; u=(U))\
|
||||
for (bool child=false; !child; child=true)\
|
||||
$FOREACHC(v, (V), !child)
|
||||
#define $ASSERT(a) ({fprintf(stderr,"%s:%d: %s\n", __FILE__, __LINE__, #a); exit(-1);})
|
||||
int mul[10000], gmul[10000];
|
||||
int add[10000], gadd[10000];
|
||||
int F[10000], G[10000];
|
||||
void pre()
|
||||
{
|
||||
for (int i=1; i<=6000; i++)
|
||||
mul[i] = add[i] = i,
|
||||
gmul[i] = gadd[i] = 0,
|
||||
F[i] = i, G[i]=0;
|
||||
for (int i=1; i<=6000; i++)
|
||||
for (int j=i-1; j>0; j--)
|
||||
if (i%j == 0 &&
|
||||
mul[i] > mul[j] + i/j + 2)
|
||||
mul[i] = mul[j] + i/j + 2,
|
||||
gmul[i] = j;
|
||||
for (int i=1; i<=6000; i++) add[i] = mul[i];
|
||||
for (int i=1; i<=6000; i++)
|
||||
for (int j=i-1; j>0; j--)
|
||||
if (add[i] > add[j] + mul[i-j] + 2)
|
||||
add[i] = add[j] + mul[i-j]+2,
|
||||
gadd[i]=j;
|
||||
}
|
||||
void printmul(int N)
|
||||
{
|
||||
if (gmul[N] == 0)
|
||||
for (int i=0; i<N; i++)
|
||||
printf("|");
|
||||
else
|
||||
{
|
||||
printmul(gmul[N]);
|
||||
printf("x");
|
||||
for (int i=0; i<N/gmul[N]; i++)
|
||||
printf("|");
|
||||
}
|
||||
}
|
||||
void print(int N)
|
||||
{
|
||||
if (gadd[N] == 0) printmul(N);
|
||||
else
|
||||
{
|
||||
print(gadd[N]);
|
||||
printf("+");
|
||||
printmul(N-gadd[N]);
|
||||
}
|
||||
}
|
||||
void printT(int N)
|
||||
{
|
||||
if (G[N] == 0) for (int i=0; i<N; i++) printf("|");
|
||||
else
|
||||
if (G[N] < 0)
|
||||
{
|
||||
printT(-G[N]);
|
||||
printf("x");
|
||||
for (int i=0; i<-N/G[N]; i++) printf("|");
|
||||
}else{
|
||||
printT(G[N]);
|
||||
printf("+");
|
||||
for (int i=0; i<N-G[N]; i++) printf("|");
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
pre();
|
||||
int N;
|
||||
while (scanf("%d", &N)!=EOF)
|
||||
{
|
||||
if (N==0) break;
|
||||
if (add[N] < mul[N])
|
||||
{
|
||||
printf("%d toothpicks: ", add[N]);
|
||||
print(N);
|
||||
printf("=%d\n", N);
|
||||
}else{
|
||||
printf("%d toothpicks: ", mul[N]);
|
||||
printmul(N);
|
||||
printf("=%d\n", N);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
#include <iostream>
|
||||
#include <string.h>
|
||||
using namespace std;
|
||||
int reserve[1000];
|
||||
int per[1000];
|
||||
int n;
|
||||
void permulation()
|
||||
{
|
||||
int i, j;
|
||||
memset(per,0,sizeof(per));
|
||||
for ( i = 1; i <= n; ++i )
|
||||
{
|
||||
for ( j = 1; j <= n; ++j )
|
||||
{
|
||||
if(per[j] == 0)
|
||||
{
|
||||
if ( reserve[i] != 0 )
|
||||
--reserve[i];
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
per[j] = i;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
while(cin >> n && n != 0)
|
||||
{
|
||||
for(i=1; i <= n; ++i)
|
||||
cin >> reserve[i];
|
||||
permulation();
|
||||
cout << per[1];
|
||||
for(i = 2; i <= n; ++i)
|
||||
cout << ',' << per[i];
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
#include <iostream>
|
||||
using namespace std ;
|
||||
int main()
|
||||
{
|
||||
int n ;
|
||||
while(~scanf("%d",&n))
|
||||
{
|
||||
double ans=1 ;
|
||||
for(int i=n-1;i>=2;i-=2)
|
||||
ans*=(i-1)*1.0/i ;
|
||||
printf("%.5lf\n",ans) ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int num;
|
||||
char name[50];
|
||||
char p[5];
|
||||
int time;
|
||||
int ok;
|
||||
};
|
||||
struct node p[50];
|
||||
int get_time(char s[])
|
||||
{
|
||||
int len=strlen(s),i;
|
||||
int t1,t2,flag,sum;
|
||||
t1=t2=0;
|
||||
flag=0;
|
||||
sum=0;
|
||||
for(i=0;i<len;i++)
|
||||
{
|
||||
if(s[i]==' ')
|
||||
{
|
||||
sum+=(t2-t1+1);
|
||||
flag=t1=t2=0;
|
||||
continue;
|
||||
}
|
||||
if(s[i]=='-')
|
||||
{
|
||||
flag=1;
|
||||
continue;
|
||||
}
|
||||
if(!flag)
|
||||
t1=t1*10+s[i]-'0';
|
||||
else
|
||||
t2=t2*10+s[i]-'0';
|
||||
}
|
||||
sum+=t2-t1+1;
|
||||
return sum;
|
||||
}
|
||||
bool cmp(node a,node b)
|
||||
{
|
||||
return a.num<b.num;
|
||||
}
|
||||
bool cmp2(node a,node b)
|
||||
{
|
||||
if(a.ok!=b.ok)
|
||||
return a.ok<b.ok;
|
||||
return a.num<b.num;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
char s[500],hh[500];
|
||||
int dd,mm,ss;
|
||||
int flag = 0;
|
||||
while(scanf("%d",&p[0].num)!=EOF,p[0].num)
|
||||
{
|
||||
for(i=0;i<22;i++)
|
||||
p[i].ok=0;
|
||||
int temp;
|
||||
scanf("%s %s",p[0].name,p[0].p);
|
||||
getchar();
|
||||
gets(s);
|
||||
p[0].time=get_time(s);
|
||||
for(i=1;i<22;i++)
|
||||
{
|
||||
scanf("%d %s %s",&p[i].num,p[i].name,p[i].p);
|
||||
getchar();
|
||||
gets(s);
|
||||
p[i].time=get_time(s);
|
||||
}
|
||||
scanf("%s",hh);
|
||||
dd=hh[0]-'0';
|
||||
mm=hh[2]-'0';
|
||||
ss=hh[4]-'0';
|
||||
int mark=0;
|
||||
sort(p,p+22,cmp);
|
||||
for(i=0;i<22;i++)
|
||||
{
|
||||
if(p[i].p[0]=='G')
|
||||
{
|
||||
p[i].ok=1;
|
||||
mark=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int count=0;
|
||||
for(i=0;i<22;i++)
|
||||
{
|
||||
if(p[i].p[0]=='D')
|
||||
{
|
||||
if(dd==0)
|
||||
break;
|
||||
p[i].ok=2;
|
||||
dd--;
|
||||
}
|
||||
}
|
||||
for(i=0;i<22;i++)
|
||||
{
|
||||
if(p[i].p[0]=='M')
|
||||
{
|
||||
if(mm==0)
|
||||
break;
|
||||
p[i].ok=3;
|
||||
mm--;
|
||||
}
|
||||
}
|
||||
for(i=0;i<22;i++)
|
||||
{
|
||||
if(p[i].p[0]=='S')
|
||||
{
|
||||
if(ss==0)
|
||||
break;
|
||||
p[i].ok=4;
|
||||
ss--;
|
||||
}
|
||||
}
|
||||
if(mark!=1 || dd!=0 || mm!=0 || ss!=0)
|
||||
{
|
||||
printf("IMPOSSIBLE TO ARRANGE\n\n");
|
||||
continue;
|
||||
}
|
||||
int maxn=-1,max_i;
|
||||
for(i=0;i<22;i++)
|
||||
{
|
||||
if(p[i].ok)
|
||||
{
|
||||
if(p[i].time>=maxn)
|
||||
{
|
||||
maxn=p[i].time;
|
||||
max_i=i;
|
||||
if(p[i].time==maxn)
|
||||
{
|
||||
if(p[i].num>p[max_i].num)
|
||||
max_i=i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d %s %s\n",p[max_i].num,p[max_i].name,p[max_i].p);
|
||||
int col=p[max_i].num;
|
||||
sort(p,p+22,cmp2);
|
||||
for(i=0;i<22;i++)
|
||||
{
|
||||
if(p[i].ok && col!=p[i].num)
|
||||
printf("%d %s %s\n",p[i].num,p[i].name,p[i].p);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
char str1[15], str2[15];
|
||||
LL dfs(int pos, LL cur1, LL cur2) {
|
||||
LL ret = 0;
|
||||
if (cur1 < cur2) return 0;
|
||||
if (cur1 > cur2) {
|
||||
ret = 1;
|
||||
while (str1[pos]) {
|
||||
if (str1[pos] == '?') ret *= 10;
|
||||
pos++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
if (str1[pos] == 0 || str2[pos] == 0) return 0;
|
||||
if (str1[pos] == '?') {
|
||||
for (int i = 0; i <= 9; i++) {
|
||||
if (i < str2[pos] - '0') continue;
|
||||
ret += dfs(pos + 1, cur1 * 10 + i, cur2 * 10 + str2[pos] - '0');
|
||||
}
|
||||
} else {
|
||||
ret += dfs(pos + 1, cur1 * 10 + str1[pos] - '0', cur2 * 10 + str2[pos] - '0');
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
int main() {
|
||||
while (cin >> str1 && str1[0] != '#') {
|
||||
cin >> str2;
|
||||
cout << dfs(0, 0, 0) << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
# include<stdio.h>
|
||||
# include<string.h>
|
||||
# include<stdlib.h>
|
||||
# define N 205
|
||||
char map[N][105];
|
||||
struct node{
|
||||
int from,to,next;
|
||||
}edge[2*N];
|
||||
int head[N],tol,visit[N],dp[N][2],dup[N][2];
|
||||
void add(int a,int b)
|
||||
{
|
||||
edge[tol].from=a;edge[tol].to=b;edge[tol].next=head[a];head[a]=tol++;
|
||||
}
|
||||
int max(int a,int b)
|
||||
{
|
||||
return a>b?a:b;
|
||||
}
|
||||
void dfs(int root)
|
||||
{
|
||||
int j,u;
|
||||
dp[root][0]=0;
|
||||
dp[root][1]=1;
|
||||
dup[root][0]=1;
|
||||
dup[root][1]=1;
|
||||
for(j=head[root];j!=-1;j=edge[j].next)
|
||||
{
|
||||
u=edge[j].to;
|
||||
dfs(u);
|
||||
dp[root][0]+=max(dp[u][0],dp[u][1]);
|
||||
dp[root][1]+=dp[u][0];
|
||||
if(dp[u][0]>dp[u][1] && dp[u][0]==0) dup[root][0]=0;
|
||||
else if(dp[u][1]>dp[u][0] && dp[u][1]==0) dup[root][0]=0;
|
||||
else if(dp[u][0]==dp[u][1]) dup[root][0]=0;
|
||||
if(dup[u][0]==0) dup[root][1]=0;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,n,k,ans1,ans2;
|
||||
char str1[105],str2[105];
|
||||
while(scanf("%d",&n)!=EOF && n)
|
||||
{
|
||||
scanf("%s",map[1]);
|
||||
k=1;
|
||||
memset(head,-1,sizeof(head));
|
||||
tol=0;
|
||||
for(i=1;i<n;i++)
|
||||
{
|
||||
scanf("%s%s",str1,str2);
|
||||
ans1=ans2=-1;
|
||||
for(j=1;j<=k;j++)
|
||||
{
|
||||
if(strcmp(str1,map[j])==0) ans1=j;
|
||||
if(strcmp(str2,map[j])==0) ans2=j;
|
||||
}
|
||||
if(ans1==-1)
|
||||
{
|
||||
k++;
|
||||
ans1=k;
|
||||
strcpy(map[k],str1);
|
||||
}
|
||||
if(ans2==-1)
|
||||
{
|
||||
k++;
|
||||
ans2=k;
|
||||
strcpy(map[k],str2);
|
||||
}
|
||||
add(ans2,ans1);
|
||||
}
|
||||
memset(dp,0,sizeof(dp));
|
||||
dfs(1);
|
||||
if(dp[1][0]>dp[1][1] && dup[1][0]==1) printf("%d Yes\n",dp[1][0]);
|
||||
else if(dp[1][1]>dp[1][0] && dup[1][1]==1) printf("%d Yes\n",dp[1][1]);
|
||||
else printf("%d No\n",max(dp[1][0],dp[1][1]));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <climits>
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
using namespace std;
|
||||
#include <queue>
|
||||
char mp[10][10];
|
||||
int xx[4]={-1,1,0,0};
|
||||
int yy[4]={0,0,-1,1};
|
||||
char fff[4]={'^','v','<','>'};
|
||||
int main()
|
||||
{
|
||||
while(scanf("%s",mp[0])!=EOF)
|
||||
{
|
||||
if(strcmp(mp[0],"--")==0) break;
|
||||
for(int i=1;i<8;i++)
|
||||
scanf("%s",mp[i]);
|
||||
int x,y,fx;
|
||||
for(int i=0;i<8;i++)
|
||||
for(int j=0;j<8;j++)
|
||||
{
|
||||
if(mp[i][j]=='^')
|
||||
x=i,y=j,fx=0;
|
||||
if(mp[i][j]=='v')
|
||||
x=i,y=j,fx=1;
|
||||
if(mp[i][j]=='<')
|
||||
x=i,y=j,fx=2;
|
||||
if(mp[i][j]=='>')
|
||||
x=i,y=j,fx=3;
|
||||
}
|
||||
getchar();
|
||||
char s[2001];
|
||||
while(1)
|
||||
{
|
||||
gets(s);
|
||||
if(s[0]=='#') break;
|
||||
if(s[0]=='m')
|
||||
{
|
||||
int step=s[5]-'0';
|
||||
if(s[6]>='0'&&s[6]<='9')
|
||||
step=8;
|
||||
int dx=x,dy=y;
|
||||
x+=xx[fx]*step;
|
||||
y+=yy[fx]*step;
|
||||
if(x>=8)x=7;
|
||||
if(y>=8)y=7;
|
||||
if(x<0)x=0;
|
||||
if(y<0)y=0;
|
||||
queue<char > q;
|
||||
while(!q.empty()) q.pop();
|
||||
int num=step;
|
||||
while(dx>=0&&dy>=0&&dx<8&&dy<8&&num)
|
||||
{
|
||||
num--;
|
||||
mp[dx][dy]='.';
|
||||
dx+=xx[fx],dy+=yy[fx];
|
||||
if((mp[dx][dy]>='a'&&mp[dx][dy]<='z')||mp[dx][dy]>='A'&&mp[dx][dy]<='Z')
|
||||
{
|
||||
q.push(mp[dx][dy]);
|
||||
}
|
||||
}
|
||||
mp[x][y]=fff[fx];
|
||||
dx=x,dy=y;
|
||||
while(dx>=0&&dy>=0&&dx<8&&dy<8)
|
||||
{
|
||||
dx+=xx[fx],dy+=yy[fx];
|
||||
if((mp[dx][dy]>='a'&&mp[dx][dy]<='z')||mp[dx][dy]>='A'&&mp[dx][dy]<='Z')
|
||||
q.push(mp[dx][dy]);
|
||||
if(!q.empty())
|
||||
{
|
||||
mp[dx][dy]=q.front();
|
||||
q.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(s[5]=='l')
|
||||
{
|
||||
if(fx==0) fx=2;
|
||||
else if(fx==1) fx=3;
|
||||
else if(fx==2) fx=1;
|
||||
else if(fx==3) fx=0;
|
||||
}
|
||||
if(s[5]=='r')
|
||||
{
|
||||
if(fx==0) fx=3;
|
||||
else if(fx==1) fx=2;
|
||||
else if(fx==2) fx=0;
|
||||
else if(fx==3) fx=1;
|
||||
}
|
||||
if(s[5]=='b')
|
||||
{
|
||||
if(fx==0) fx=1;
|
||||
else if(fx==1) fx=0;
|
||||
else if(fx==2) fx=3;
|
||||
else if(fx==3) fx=2;
|
||||
}
|
||||
mp[x][y]=fff[fx];
|
||||
}
|
||||
}
|
||||
for(int i=0;i<8;i++)
|
||||
{
|
||||
for(int j=0;j<8;j++)
|
||||
printf("%c",mp[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
memset(mp,0,sizeof(mp));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<string.h>
|
||||
#include<string>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
const int N = 210;
|
||||
int n,num,m,dp[N][N],w[N],in[N];
|
||||
char s[20110];
|
||||
bool vis[N];
|
||||
vector<int> g[N];
|
||||
typedef struct node
|
||||
{
|
||||
int cnt;
|
||||
struct node *next[52];
|
||||
}*tree,Trie;
|
||||
tree root;
|
||||
inline int GetNum(char *t){
|
||||
tree p = root,newnode;
|
||||
for(int i = 0;i < strlen(t); ++i){
|
||||
int u = t[i] - 'a';
|
||||
if(u<0) u=t[i]-'A'+26;
|
||||
if(p->next[u]==NULL)
|
||||
{
|
||||
newnode=(tree)malloc(sizeof(Trie));
|
||||
newnode->cnt=-1;
|
||||
for(int j=0;j<52;j++)
|
||||
newnode->next[j]=NULL;
|
||||
p->next[u]=newnode;
|
||||
p=newnode;
|
||||
}
|
||||
else
|
||||
p = p->next[u];
|
||||
}
|
||||
if(p->cnt == -1)
|
||||
p->cnt = num ++;
|
||||
return p->cnt;
|
||||
}
|
||||
void init()
|
||||
{
|
||||
root=(tree)malloc(sizeof(Trie));
|
||||
root->cnt=-1;
|
||||
for(int j=0;j<52;j++)
|
||||
root->next[j]=NULL;
|
||||
num=1;
|
||||
memset(in,0,sizeof(in));
|
||||
for(int i=0;i<=n;i++)
|
||||
g[i].clear();
|
||||
for(int i=0;i<=n;i++)
|
||||
for(int j=0;j<=n;j++)
|
||||
dp[i][j]=INT_MAX;
|
||||
}
|
||||
int dfs(int u)
|
||||
{
|
||||
int size=g[u].size();
|
||||
dp[u][0]=0;
|
||||
int t=0;
|
||||
for(int i=0;i<size;i++)
|
||||
{
|
||||
int v=g[u][i];
|
||||
int now=dfs(v);
|
||||
for(int j=t;j>=0;j--)
|
||||
{
|
||||
for(int k=0;k<=now;k++)
|
||||
dp[u][j+k]=min(dp[u][j+k],dp[u][j]+dp[v][k]);
|
||||
}
|
||||
t+=now;
|
||||
}
|
||||
for(int i=1;i<=t+1;i++)
|
||||
dp[u][i]=min(dp[u][i],w[u]);
|
||||
return t+1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
char str[100],c[110];
|
||||
while(scanf("%s",str)==1)
|
||||
{
|
||||
if(strcmp(str,"#")==0)
|
||||
break;
|
||||
n=atoi(str);
|
||||
scanf("%d",&m);
|
||||
getchar();
|
||||
init();
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
gets(s);
|
||||
int len=strlen(s),cc=0,t=0,a,b;
|
||||
for(int j=0;j<=len;j++)
|
||||
{
|
||||
if(s[j]!=' ' && s[j]!='\0')
|
||||
c[t++]=s[j];
|
||||
if(s[j]==' '||s[j]=='\0')
|
||||
{
|
||||
cc++;
|
||||
c[t]='\0';
|
||||
t=0;
|
||||
if(cc==1)
|
||||
a=GetNum(c);
|
||||
else if(cc==2)
|
||||
w[a]=atoi(c);
|
||||
else {
|
||||
b=GetNum(c);
|
||||
g[a].push_back(b);
|
||||
in[b]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
if(in[i]==0)
|
||||
g[0].push_back(i);
|
||||
}
|
||||
w[0]=INT_MAX;
|
||||
dfs(0);
|
||||
printf("%d\n",dp[0][m]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
const int N = 109;
|
||||
const int INF =0x3f3f3f3f;
|
||||
int n,m;
|
||||
int dp[N][N][30];
|
||||
bool visit[N][N][30];
|
||||
char map[N][N];
|
||||
bool init()
|
||||
{
|
||||
n=0;
|
||||
while(1)
|
||||
{
|
||||
gets(map[n]);
|
||||
if(map[n][0]=='-') return false;
|
||||
if(strlen(map[n])==0) break;
|
||||
n++;
|
||||
}
|
||||
m = strlen(map[0]);
|
||||
return true;
|
||||
}
|
||||
int dx[]={0,0,-1,1};
|
||||
int dy[]={-1,1,0,0};
|
||||
struct nod{
|
||||
int dis;
|
||||
int x,y,dep;
|
||||
bool operator <(const nod t) const
|
||||
{
|
||||
return dis>t.dis;
|
||||
}
|
||||
};
|
||||
priority_queue<nod> que;
|
||||
bool oor(int x,int y)
|
||||
{
|
||||
if(x<0||x>=n) return false;
|
||||
if(y<0||y>=m) return false;
|
||||
return true;
|
||||
}
|
||||
void deal()
|
||||
{
|
||||
memset(dp,INF,sizeof(dp));
|
||||
memset(visit,false,sizeof(visit));
|
||||
int enx,eny;
|
||||
for(int i=0;i<n;i++)
|
||||
for(int j=0;j<m;j++)
|
||||
if(map[i][j]=='$')
|
||||
{
|
||||
enx=i,eny=j;
|
||||
}
|
||||
while(!que.empty()) que.pop();
|
||||
nod e,t;
|
||||
e.dep=0;e.dis=0;e.x=enx,e.y=eny;
|
||||
que.push(e);
|
||||
while(!que.empty())
|
||||
{
|
||||
e = que.top(); que.pop();
|
||||
if(visit[e.x][e.y][e.dep]) continue;
|
||||
visit[e.x][e.y][e.dep] = true;
|
||||
dp[e.x][e.y][e.dep] = e.dis;
|
||||
if(e.x==0||e.y==0||e.x==n-1||e.y==m-1) continue;
|
||||
for(int i=0;i<4;i++)
|
||||
{
|
||||
int tx=e.x+dx[i],ty=e.y+dy[i];
|
||||
if(!oor(tx,ty)||map[tx][ty]=='*') continue;
|
||||
if(map[tx][ty]>='0'&&map[tx][ty]<='9')
|
||||
{
|
||||
t.dis = e.dis+map[tx][ty]-'0';
|
||||
t.dep = e.dep;
|
||||
t.x = tx,t.y = ty;
|
||||
if(!visit[tx][ty][t.dep]&&dp[tx][ty][t.dep]>t.dis)
|
||||
que.push(t);
|
||||
t.dis = e.dis;
|
||||
t.dep = e.dep+1;
|
||||
t.x = tx,t.y = ty;
|
||||
if(!visit[tx][ty][t.dep]&&dp[tx][ty][t.dep]>t.dis&&t.dep<=26)
|
||||
que.push(t);
|
||||
}
|
||||
else
|
||||
{
|
||||
t.dis = e.dis;
|
||||
t.dep = e.dep;
|
||||
t.x = tx,t.y = ty;
|
||||
if(dp[tx][ty][t.dep]>t.dis)
|
||||
que.push(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int fin(int x,int y)
|
||||
{
|
||||
int ans = INF;
|
||||
int d = map[x][y]=='#'?0:map[x][y]-'A'+1;
|
||||
for(int i=0;i<=d;i++)
|
||||
ans=min(dp[x][y][d],ans);
|
||||
return ans;
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
deal();
|
||||
int ans=INF;
|
||||
for(int i=1;i<n;i++)
|
||||
{
|
||||
if(map[i][0]!='*')
|
||||
ans=min(fin(i,0),ans);
|
||||
if(map[i][m-1]!='*')
|
||||
ans=min(fin(i,m-1),ans);
|
||||
}
|
||||
for(int i=1;i<m;i++)
|
||||
{
|
||||
if(map[0][i]!='*')
|
||||
ans=min(fin(0,i),ans);
|
||||
if(map[n-1][i]!='*')
|
||||
ans=min(fin(n-1,i),ans);
|
||||
}
|
||||
if(ans==INF) printf("IMPOSSIBLE\n");
|
||||
else printf("%d\n",ans);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
if(!init()) break;
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
#include<set>
|
||||
#include<map>
|
||||
using namespace std;
|
||||
struct course
|
||||
{
|
||||
int cap;
|
||||
set<int> times;
|
||||
}tempc;
|
||||
struct student
|
||||
{
|
||||
set<string> cou;
|
||||
set<int> times;
|
||||
}temps;
|
||||
map<string,student> maps;
|
||||
map<string,course> mapc;
|
||||
map<string,int> ct;
|
||||
struct request
|
||||
{
|
||||
string s,c;
|
||||
int idx;
|
||||
}re[500];
|
||||
bool check(int x)
|
||||
{
|
||||
map<string,student>::iterator its=maps.find(re[x].s);
|
||||
map<string,course>::iterator itc=mapc.find(re[x].c);
|
||||
if(itc->second.cap<=0) return false;
|
||||
if(its->second.cou.count(re[x].c)!=0) return false;
|
||||
for(set<int>::iterator it=itc->second.times.begin();it!=itc->second.times.end();++it)
|
||||
{
|
||||
if(its->second.times.count(*it)!=0) return false;
|
||||
}
|
||||
for(set<int>::iterator it=itc->second.times.begin();it!=itc->second.times.end();++it)
|
||||
{
|
||||
its->second.times.insert(*it);
|
||||
}
|
||||
(its->second.cou.insert(re[x].c));
|
||||
(itc->second.cap)--;
|
||||
return true;
|
||||
}
|
||||
bool cmp(const request &a,const request &b)
|
||||
{
|
||||
if(ct[a.c]<ct[b.c]) return true;
|
||||
else if(ct[a.c]>ct[b.c]) return false;
|
||||
else return a.idx<b.idx;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,m,r,t,x;
|
||||
char s[150],c[150];
|
||||
for(int cas=1;~scanf("%d%d%d",&n,&m,&r);++cas)
|
||||
{
|
||||
mapc.clear();
|
||||
maps.clear();
|
||||
ct.clear();
|
||||
temps.cou.clear();
|
||||
temps.times.clear();
|
||||
for(int i=0;i<n;++i)
|
||||
{
|
||||
scanf("%s",s);
|
||||
maps.insert(make_pair(string(s),temps));
|
||||
}
|
||||
for(int i=0;i<m;++i)
|
||||
{
|
||||
tempc.times.clear();
|
||||
scanf("%s",s);
|
||||
scanf("%d",&tempc.cap);
|
||||
scanf("%d",&t);
|
||||
for(;t--;)
|
||||
{
|
||||
scanf("%d",&x);
|
||||
tempc.times.insert(x);
|
||||
}
|
||||
mapc.insert(make_pair(string(s),tempc));
|
||||
ct.insert(make_pair(string(s),i));
|
||||
}
|
||||
for(int i=0;i<r;++i)
|
||||
{
|
||||
scanf("%s%s",s,c);
|
||||
re[i].s=string(s);
|
||||
re[i].c=string(c);
|
||||
re[i].idx=i;
|
||||
}
|
||||
sort(re,re+r,cmp);
|
||||
int ans=0;
|
||||
for(int i=0;i<r;++i)
|
||||
if(check(i))
|
||||
ans++;
|
||||
printf("Case %d: %d\n",cas,ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
using namespace std;
|
||||
typedef pair<int, int> PII;
|
||||
const int MAXN = 20010, MAXM = 60010, MAXQ = 300010;
|
||||
int value[MAXN], query[MAXQ][3];
|
||||
multiset<PII> e;
|
||||
multiset<int> v[MAXN];
|
||||
int pre[MAXN];
|
||||
int root(int a)
|
||||
{
|
||||
if(pre[a] != a)
|
||||
pre[a] = root(pre[a]);
|
||||
return pre[a];
|
||||
}
|
||||
int unionSet(int a, int b)
|
||||
{
|
||||
multiset<int>::iterator it;
|
||||
a = root(a), b = root(b);
|
||||
if(a == b)
|
||||
return 0;
|
||||
if(v[a].size() > v[b].size())
|
||||
swap(a, b);
|
||||
for (it = v[a].begin(); it != v[a].end(); it++)
|
||||
v[b].insert(*it);
|
||||
v[a].clear();
|
||||
pre[a] = b;
|
||||
return 0;
|
||||
}
|
||||
int update(int a, int x)
|
||||
{
|
||||
int ta = a;
|
||||
a = root(a);
|
||||
v[a].erase(v[a].find(value[ta]));
|
||||
v[a].insert(x);
|
||||
value[ta] = x;
|
||||
return 0;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n, m, q;
|
||||
int a, b;
|
||||
int i,u;
|
||||
int cas=0;
|
||||
char ch[3];
|
||||
double ct = 0, sum = 0;
|
||||
multiset<PII>::iterator it;
|
||||
multiset<int>::iterator tp;
|
||||
while(scanf("%d%d%d",&n,&m,&q)==3)
|
||||
{
|
||||
e.clear();
|
||||
for (i = 1; i <= n; i++)
|
||||
v[i].clear();
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
scanf("%d", &value[i]);
|
||||
pre[i] = i;
|
||||
}
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
scanf("%d%d", &a, &b);
|
||||
if(a > b)
|
||||
swap(a, b);
|
||||
e.insert(PII(a, b));
|
||||
}
|
||||
for (i = 0; i < q; i++)
|
||||
{
|
||||
scanf("%s", &ch);
|
||||
scanf("%d%d", &query[i][1], &query[i][2]);
|
||||
query[i][0] = ch[0];
|
||||
if(ch[0] == 'E')
|
||||
{
|
||||
if(query[i][1] > query[i][2])
|
||||
swap(query[i][1], query[i][2]);
|
||||
it = e.find(PII(query[i][1], query[i][2]));
|
||||
e.erase(it);
|
||||
}
|
||||
else if(ch[0] == 'U')
|
||||
swap(value[query[i][1]], query[i][2]);
|
||||
}
|
||||
for (i = 1; i <= n; i++)
|
||||
v[i].insert(value[i]);
|
||||
for (it = e.begin(); it != e.end(); it++)
|
||||
unionSet(it->first, it->second);
|
||||
ct=0;
|
||||
sum=0;
|
||||
for(int i = q - 1; i >= 0; i--)
|
||||
{
|
||||
if(query[i][0] == 'E')
|
||||
unionSet(query[i][1], query[i][2]);
|
||||
else if(query[i][0] == 'U')
|
||||
update(query[i][1], query[i][2]);
|
||||
else
|
||||
{
|
||||
ct += 1;
|
||||
u = root(query[i][1]);
|
||||
tp = v[u].lower_bound(query[i][2]);
|
||||
if(tp != v[u].end())
|
||||
sum += *tp;
|
||||
}
|
||||
}
|
||||
printf("Case %d: %0.3lf\n",++cas,(double)sum/ct);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
#include<iostream>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
#define N 1010
|
||||
bool is[N];
|
||||
__int64 res[N];
|
||||
short prim[N];
|
||||
int main()
|
||||
{
|
||||
memset(is,true,sizeof(is));
|
||||
__int64 i,j,a,b;
|
||||
for(i=2;(i<<1)<1001;i++)
|
||||
{
|
||||
for(j=2;i*j<1001;j++)
|
||||
is[j*i]=false;
|
||||
}
|
||||
int sum=0;
|
||||
for(i=2;i<=1000;i++)
|
||||
if(is[i])
|
||||
prim[sum++]=i;
|
||||
int cnt=0;
|
||||
while(scanf("%I64d%I64d",&a,&b)!=EOF)
|
||||
{
|
||||
b%=10007;
|
||||
printf("Case %d: ",++cnt);
|
||||
int total=0;
|
||||
memset(res,0,sizeof(res));
|
||||
for(i=0;a!=1&&i<sum;i++)
|
||||
{
|
||||
if(a%prim[i]==0)
|
||||
{
|
||||
while(a%prim[i]==0)
|
||||
{
|
||||
res[total]++;
|
||||
a/=prim[i];
|
||||
}
|
||||
total++;
|
||||
}
|
||||
}
|
||||
if(a!=1)
|
||||
res[total++]=1;
|
||||
__int64 ans=1;
|
||||
for(i=0;i<total;i++)
|
||||
{
|
||||
__int64 s;
|
||||
s=(((b*res[i]+1)*(b*res[i]+2))/2)%10007;
|
||||
s*=s;
|
||||
ans=(ans*s)%10007;
|
||||
}
|
||||
printf("%I64d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,203 @@
|
|||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
void mult(char a[],char b[],char s[])
|
||||
{
|
||||
int i,j,k=0,alen,blen,sum=0,res[165][165]={0},flag=0;
|
||||
char result[165];
|
||||
alen=strlen(a);blen=strlen(b);
|
||||
for (i=0;i<alen;i++)
|
||||
for (j=0;j<blen;j++) res[i][j]=(a[i]-'0')*(b[j]-'0');
|
||||
for (i=alen-1;i>=0;i--)
|
||||
{
|
||||
for (j=blen-1;j>=0;j--)
|
||||
sum=sum+res[i+blen-j-1][j];
|
||||
result[k]=sum%10;
|
||||
k=k+1;
|
||||
sum=sum/10;
|
||||
}
|
||||
for (i=blen-2;i>=0;i--)
|
||||
{
|
||||
for (j=0;j<=i;j++) sum=sum+res[i-j][j];
|
||||
result[k]=sum%10;
|
||||
k=k+1;
|
||||
sum=sum/10;
|
||||
}
|
||||
if (sum!=0) {result[k]=sum;k=k+1;}
|
||||
for (i=0;i<k;i++) result[i]+='0';
|
||||
for (i=k-1;i>=0;i--) s[i]=result[k-1-i];
|
||||
s[k]='\0';
|
||||
while(1)
|
||||
{
|
||||
if (strlen(s)!=strlen(a)&&s[0]=='0')
|
||||
strcpy(s,s+1);
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
void add(char a[],char b[],char back[])
|
||||
{
|
||||
int i,j,k,up,x,y,z,l;
|
||||
char *c;
|
||||
if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2;
|
||||
c=(char *) malloc(l*sizeof(char));
|
||||
i=strlen(a)-1;
|
||||
j=strlen(b)-1;
|
||||
k=0;up=0;
|
||||
while(i>=0||j>=0)
|
||||
{
|
||||
if(i<0) x='0'; else x=a[i];
|
||||
if(j<0) y='0'; else y=b[j];
|
||||
z=x-'0'+y-'0';
|
||||
if(up) z+=1;
|
||||
if(z>9) {up=1;z%=10;} else up=0;
|
||||
c[k++]=z+'0';
|
||||
i--;j--;
|
||||
}
|
||||
if(up) c[k++]='1';
|
||||
i=0;
|
||||
c[k]='\0';
|
||||
for(k-=1;k>=0;k--)
|
||||
back[i++]=c[k];
|
||||
back[i]='\0';
|
||||
}
|
||||
int flage[100],M,open=0;
|
||||
char num1[100][25],sum[100],temp[100];
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
int i;
|
||||
M=0;
|
||||
while(scanf("%d",&T)!=EOF)
|
||||
{
|
||||
memset(num1,0,sizeof(num1));
|
||||
memset(sum,0,sizeof(sum));
|
||||
memset(flage,0,sizeof(flage));
|
||||
memset(temp,0,sizeof(temp));
|
||||
for(int i=0;i<T;i++)
|
||||
{
|
||||
cin>>num1[i];
|
||||
}
|
||||
int dingshitao = 0;
|
||||
open=0;
|
||||
for( i=0;i<T;i++)
|
||||
{
|
||||
if(num1[i][0]!='*'&&num1[i][0]!='+')
|
||||
{ flage[i]=1; }
|
||||
if(num1[i][0]=='*')
|
||||
{flage[i]=2;}
|
||||
if(i%2==1&&strlen(num1[i])!=1)
|
||||
{
|
||||
dingshitao = 1;
|
||||
}
|
||||
}
|
||||
if(dingshitao == 1)
|
||||
{
|
||||
cout<<"Case "<<++M<<": Invalid Expression!"<<endl;
|
||||
continue;
|
||||
}
|
||||
if(T==0)
|
||||
{cout<<"Case "<<++M<<": "<<"0"<<endl;
|
||||
continue;
|
||||
}
|
||||
if(T==1)
|
||||
{
|
||||
if(flage[0]==1)
|
||||
cout<<"Case "<<++M<<": "<<num1[0]<<endl;
|
||||
else
|
||||
cout<<"Case "<<++M<<": Invalid Expression!"<<endl;
|
||||
continue;
|
||||
}
|
||||
if(T==2)
|
||||
{
|
||||
cout<<"Case "<<++M<<": Invalid Expression!"<<endl;
|
||||
continue;
|
||||
}
|
||||
if(T==3&&flage[1]==1)
|
||||
{
|
||||
cout<<"Case "<<++M<<": Invalid Expression!"<<endl;
|
||||
continue;
|
||||
}
|
||||
for(i=0;i<T;i++)
|
||||
{
|
||||
if(T%2==0)
|
||||
{
|
||||
cout<<"Case "<<++M<<": Invalid Expression!"<<endl;
|
||||
open=1;
|
||||
break;
|
||||
}
|
||||
if(i%2==0)
|
||||
{
|
||||
if(flage[i]!=1)
|
||||
{
|
||||
cout<<"Case "<<++M<<": Invalid Expression!"<<endl;
|
||||
open=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
if(flage[i]==1)
|
||||
{ cout<<"Case "<<++M<<": Invalid Expression!"<<endl;
|
||||
open=1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
int f=0;
|
||||
for(i=1;i<T-1;i=i+2)
|
||||
{
|
||||
if(flage[i]==2)
|
||||
{
|
||||
if(!f)
|
||||
{
|
||||
strcpy(temp,num1[i-1]);
|
||||
f=1;
|
||||
}
|
||||
if(flage[i-1]!=3||flage[i+1]!=3)
|
||||
{
|
||||
mult(temp,num1[i+1],temp);
|
||||
flage[i-1]=3;
|
||||
flage[i+1]=3;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
add(temp,sum,sum);
|
||||
memset(temp,0,sizeof(temp));
|
||||
f=0;
|
||||
}
|
||||
}
|
||||
if(flage[T-2]==2)
|
||||
{
|
||||
add(temp,sum,sum);
|
||||
}
|
||||
for(i=1;i<T-1;i=i+2)
|
||||
{
|
||||
if(flage[i]==0)
|
||||
{
|
||||
if((flage[i-1]!=3||flage[i+1]!=3))
|
||||
{
|
||||
if(flage[i-1]==1)
|
||||
{
|
||||
add(num1[i-1],sum,sum);
|
||||
flage[i-1]=3;
|
||||
}
|
||||
if(flage[i+1]==1)
|
||||
{
|
||||
add(num1[i+1],sum,sum);
|
||||
flage[i+1]=3;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!open)
|
||||
{
|
||||
if(sum[0]=='0')
|
||||
cout<<"Case "<<++M<<": "<<"0"<<endl;
|
||||
else
|
||||
cout<<"Case "<<++M<<": "<<sum<<endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
int map[30][30],vis[30][30],v[5];
|
||||
int sx,sy,ex,ey,m,n;
|
||||
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
|
||||
struct Node
|
||||
{
|
||||
int x,y,step;
|
||||
friend bool operator < (Node n1,Node n2)
|
||||
{
|
||||
return n1.step>n2.step;
|
||||
}
|
||||
};
|
||||
int check(int x,int y)
|
||||
{
|
||||
if(x<0||y<0||x>=m||y>=n||map[x][y]==-1||vis[x][y])
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
int bfs()
|
||||
{
|
||||
priority_queue<Node> qu;
|
||||
Node next,q;
|
||||
memset(vis,0,sizeof(vis));
|
||||
q.x=sx;q.y=sy;q.step=0;
|
||||
map[q.x][q.y]=-1;
|
||||
vis[q.x][q.y]=1;
|
||||
qu.push(q);
|
||||
while(!qu.empty())
|
||||
{
|
||||
q=qu.top();
|
||||
qu.pop();
|
||||
if(q.x==ex&&q.y==ey)
|
||||
return q.step;
|
||||
for(int i=0;i<4;i++)
|
||||
{
|
||||
next=q;
|
||||
next.x+=dir[i][0];next.y+=dir[i][1];
|
||||
if(check(next.x,next.y))
|
||||
continue;
|
||||
next.step+=v[map[next.x][next.y]];
|
||||
vis[next.x][next.y]=1;
|
||||
qu.push(next);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int cas=1,i,j;
|
||||
while(~scanf("%d%d",&m,&n))
|
||||
{
|
||||
scanf("%d%d%d",&v[3],&v[2],&v[1]);
|
||||
getchar();
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
{
|
||||
char temp=getchar();
|
||||
if(temp=='T')map[i][j]=1;
|
||||
else if(temp=='.')map[i][j]=2;
|
||||
else if(temp=='#')map[i][j]=3;
|
||||
else if(temp=='@')map[i][j]=-1;
|
||||
}
|
||||
getchar();
|
||||
}
|
||||
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
|
||||
printf("Case %d: %d\n",cas++,bfs());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<queue>
|
||||
#define N_node 2000
|
||||
#define N_eage 200090
|
||||
#define inf 1000000000
|
||||
using namespace std;
|
||||
typedef struct
|
||||
{
|
||||
int from ,to ,next ,cost ,flow;
|
||||
}STAR;
|
||||
STAR E[N_eage];
|
||||
int list[N_node] ,tot;
|
||||
int mer[N_eage];
|
||||
int s_x[N_node];
|
||||
void add(int a ,int b ,int c ,int d)
|
||||
{
|
||||
E[++tot].from = a;
|
||||
E[tot].to = b;
|
||||
E[tot].cost = c;
|
||||
E[tot].flow = d;
|
||||
E[tot].next = list[a];
|
||||
list[a] = tot;
|
||||
E[++tot].from = b;
|
||||
E[tot].to = a;
|
||||
E[tot].cost = -c;
|
||||
E[tot].flow = 0;
|
||||
E[tot].next = list[b];
|
||||
list[b] = tot;
|
||||
}
|
||||
bool spfa(int s ,int t ,int n)
|
||||
{
|
||||
for(int i = 0; i<= n ;i ++)
|
||||
s_x[i] = inf;
|
||||
s_x[s] = 0;
|
||||
bool mark[N_node] = {0};
|
||||
mark[s] = 1;
|
||||
queue<int>q;
|
||||
q.push(s);
|
||||
memset(mer ,255 ,sizeof(mer));
|
||||
while(!q.empty())
|
||||
{
|
||||
int xin ,tou;
|
||||
tou = q.front();
|
||||
q.pop();
|
||||
mark[tou] = 0;
|
||||
for(int k = list[tou] ;k ;k = E[k].next)
|
||||
{
|
||||
xin = E[k].to;
|
||||
if(s_x[xin] > s_x[tou] + E[k].cost && E[k].flow)
|
||||
{
|
||||
mer[xin] = k;
|
||||
s_x[xin] = s_x[tou] + E[k].cost;
|
||||
if(!mark[xin])
|
||||
{
|
||||
mark[xin] = 1;
|
||||
q.push(xin);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return mer[t] != -1;
|
||||
}
|
||||
int M_C_flow(int s ,int t ,int n ,int p_num)
|
||||
{
|
||||
int minflow ,mincost = 0 ,maxflow = 0;
|
||||
while(spfa(s ,t ,n))
|
||||
{
|
||||
minflow = inf;
|
||||
for(int i = mer[t] ;i + 1 ;i = mer[E[i].from])
|
||||
{
|
||||
if(minflow > E[i].flow)
|
||||
minflow = E[i].flow;
|
||||
}
|
||||
for(int i = mer[t] ;i + 1 ;i = mer[E[i].from])
|
||||
{
|
||||
E[i].flow -= minflow;
|
||||
E[i^1].flow += minflow;
|
||||
mincost += E[i].cost * minflow;
|
||||
}
|
||||
maxflow += minflow;
|
||||
}
|
||||
if(maxflow != p_num) mincost = 1;
|
||||
return mincost;
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
int i ,j ,n ,m ,num ,k ,a ,b ,c ,cc = 1;
|
||||
while(~scanf("%d %d %d" ,&n ,&m ,&k))
|
||||
{
|
||||
memset(list ,0 ,sizeof(list));
|
||||
tot = 1;
|
||||
for(i = 1 ;i <= k ;i ++)
|
||||
{
|
||||
scanf("%d %d %d" ,&a ,&b ,&c);
|
||||
a++ ,b ++;
|
||||
if(c < 0)continue;
|
||||
add(a ,b + n ,-c ,1);
|
||||
}
|
||||
int e = n + m + 1;
|
||||
for(i = 1 ;i <= n ;i ++)
|
||||
{
|
||||
add(0 ,i ,0 ,1);
|
||||
}
|
||||
for(i = 1 ;i <= m ;i ++)
|
||||
{
|
||||
add(i + n ,e ,0 ,1);
|
||||
}
|
||||
printf("Case %d: %d\n" ,cc++ ,-M_C_flow(0 ,e ,e ,n));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
const int N = 2005;
|
||||
struct pp
|
||||
{
|
||||
int x, y;
|
||||
friend bool operator < (pp A, pp B)
|
||||
{
|
||||
if(A.x != B.x)
|
||||
{
|
||||
return A.x < B.x;
|
||||
}
|
||||
else return A.y < B.y;
|
||||
}
|
||||
} p[N];
|
||||
bool mp[N][N];
|
||||
int n, a[N], has[N];
|
||||
vector<pp>v[N];
|
||||
map<int,int>g;
|
||||
void init()
|
||||
{
|
||||
int tp = 0;
|
||||
for(int i=1; i<=n; i++)
|
||||
{
|
||||
scanf("%d%d", &p[i].x, &p[i].y);
|
||||
a[++tp] = p[i].x;
|
||||
a[++tp] = p[i].y;
|
||||
}
|
||||
sort(a+1, a+tp+1);
|
||||
g.clear();
|
||||
tp = 0;
|
||||
for(int i=1; i<=2*n; i++)
|
||||
{
|
||||
if(g.find(a[i]) == g.end())
|
||||
{
|
||||
g[a[i]] = ++tp;
|
||||
has[tp] = a[i];
|
||||
}
|
||||
v[i].clear();
|
||||
}
|
||||
memset(mp,0,sizeof(mp));
|
||||
for(int i=1; i<=n; i++)
|
||||
{
|
||||
p[i].x = g[p[i].x];
|
||||
p[i].y = g[p[i].y];
|
||||
v[p[i].x].push_back(p[i]);
|
||||
mp[p[i].x][p[i].y] = 1;
|
||||
}
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int x1,y1, x2, y2,s,x;
|
||||
int ans = 0;
|
||||
for(int i=1; i<=2*n; ++i)
|
||||
{
|
||||
if(v[i].size() < 2) continue;
|
||||
sort(v[i].begin(),v[i].end());
|
||||
for(int j=0; j<v[i].size(); j++)
|
||||
{
|
||||
x1 = v[i][j].x;
|
||||
y1 = v[i][j].y;
|
||||
for(int k=j+1; k<v[i].size(); k++)
|
||||
{
|
||||
x2 = v[i][k].x;
|
||||
y2 = v[i][k].y;
|
||||
s = has[y2] - has[y1];
|
||||
x = has[x1] + s;
|
||||
x = g[x];
|
||||
if(mp[x][y1] && mp[x][y2])
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
scanf("%d", &t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d", &n);
|
||||
init();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,111 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int N = 65, P = 10001;
|
||||
int map[N][N], n, s, t, m, nn;
|
||||
char str[N][12], ss[12],st[12];
|
||||
void mul(int a[][N], int b[][N]);
|
||||
void init()
|
||||
{
|
||||
int len;
|
||||
scanf("%d", &n);
|
||||
for(int i=1; i<=n; i++)
|
||||
{
|
||||
scanf("%s", str[i]);
|
||||
}
|
||||
memset(map, 0, sizeof(map));
|
||||
for(int i=1; i<=n; i++)
|
||||
{
|
||||
len = strlen(str[i]);
|
||||
for(int j=1; j<=n; j++)
|
||||
{
|
||||
if(str[i][len-1] == str[j][0])
|
||||
map[i][j] = 1;
|
||||
}
|
||||
}
|
||||
for(int i=1; i<=n; i++)
|
||||
{
|
||||
for(int j=1; j<=n; j++)
|
||||
{
|
||||
map[i+n][j] = map[i][j];
|
||||
}
|
||||
map[i+n][i+n] = 1;
|
||||
}
|
||||
mul(map, map);
|
||||
scanf("%s", ss);
|
||||
scanf("%s", st);
|
||||
for(int i=1; i<=n; i++)
|
||||
{
|
||||
if(strcmp(ss, str[i]) == 0)
|
||||
s = i;
|
||||
if(strcmp(st, str[i]) == 0)
|
||||
t = i;
|
||||
}
|
||||
nn = n;
|
||||
n *= 2;
|
||||
scanf("%d", &m);
|
||||
}
|
||||
void mul(int a[][N], int b[][N])
|
||||
{
|
||||
int i,j,k;
|
||||
int c[N][N];
|
||||
for(i = 1; i <= n; i++)
|
||||
for(j = 1; j <= n; j++)
|
||||
{
|
||||
c[i][j] = 0;
|
||||
for(k = 1; k <= n; k++)
|
||||
{
|
||||
c[i][j] = c[i][j] + a[i][k]*b[k][j];
|
||||
c[i][j] = c[i][j] % P;
|
||||
}
|
||||
}
|
||||
for(i = 1; i <= n; i++)
|
||||
for(j = 1; j <= n; j++)
|
||||
a[i][j] = c[i][j];
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int i,j,k;
|
||||
int ans;
|
||||
int arraya[N][N],arrayb[N][N];
|
||||
memset(arraya,0,sizeof(arraya));
|
||||
for(i = 1; i <= n; i++)
|
||||
arraya[i][i] = 1;
|
||||
for(i = 1; i <= n; i++)
|
||||
for(j = 1; j <= n; j++)
|
||||
arrayb[i][j] = map[i][j];
|
||||
m = (m+1)/2;
|
||||
while(m)
|
||||
{
|
||||
if(1&m)
|
||||
mul(arraya,arrayb);
|
||||
mul(arrayb,arrayb);
|
||||
m = m>>1;
|
||||
}
|
||||
memset(arrayb, 0, sizeof(arrayb));
|
||||
for(int i=1; i<=nn; i++)
|
||||
{
|
||||
for(int j=1; j<=nn; j++)
|
||||
{
|
||||
arrayb[i][j+nn] = map[i+nn][j];
|
||||
}
|
||||
arrayb[i+nn][i+nn] = 1;
|
||||
}
|
||||
mul(arrayb,arraya);
|
||||
printf("%d\n",arrayb[s+nn][t]);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
scanf("%d", &t);
|
||||
while(t--)
|
||||
{
|
||||
init();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define N 1000002
|
||||
#define INF 10000000
|
||||
#define L(i) (i) << 1
|
||||
#define R(i) (i) << 1 | 1
|
||||
struct _st
|
||||
{
|
||||
int l,r,Min;
|
||||
int mid()
|
||||
{
|
||||
return (l + r) >> 1;
|
||||
}
|
||||
}st[N<<2];
|
||||
void build(int id,int l,int r)
|
||||
{
|
||||
st[id].l = l;
|
||||
st[id].r = r;
|
||||
st[id].Min = INF;
|
||||
if(l == r)
|
||||
return;
|
||||
int mid = st[id].mid();
|
||||
build(L(id),l,mid);
|
||||
build(R(id),mid+1,r);
|
||||
}
|
||||
int query(int id,int l,int r)
|
||||
{
|
||||
if(st[id].l >= l && st[id].r <= r)
|
||||
return st[id].Min;
|
||||
int mid = st[id].mid();
|
||||
if(mid >= r)
|
||||
return query(L(id),l,r);
|
||||
else if(mid < l)
|
||||
return query(R(id),l,r);
|
||||
else
|
||||
return min(query(L(id),l,mid),query(R(id),mid+1,r));
|
||||
}
|
||||
void update(int id,int p,int val)
|
||||
{
|
||||
if(st[id].l == st[id].r)
|
||||
{
|
||||
if(val < st[id].Min)
|
||||
st[id].Min = val;
|
||||
return ;
|
||||
}
|
||||
int mid = st[id].mid();
|
||||
if(mid >= p)
|
||||
update(L(id),p,val);
|
||||
else
|
||||
update(R(id),p,val);
|
||||
st[id].Min = min(st[L(id)].Min, st[R(id)].Min);
|
||||
}
|
||||
__int64 s[N];
|
||||
int main()
|
||||
{
|
||||
int cas = 0;
|
||||
int t,n,p,k,i,w;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d%d%d",&n,&p,&k);
|
||||
build(1,0,p);
|
||||
for(i = 1; i <= n; ++i)
|
||||
{
|
||||
scanf("%d",&w);
|
||||
s[i] = s[i-1] + w;
|
||||
}
|
||||
__int64 res = -1;
|
||||
int id;
|
||||
for(i = 1; i <= n; ++i)
|
||||
{
|
||||
if(s[i] % p <= k)
|
||||
id = 0;
|
||||
else
|
||||
id = query(1,s[i] % p - k ,s[i] % p);
|
||||
if(id != INF)
|
||||
res = max(res,(s[i] - s[id])/p);
|
||||
update(1,s[i] % p,i);
|
||||
}
|
||||
printf("Case %d: %I64d\n",++cas,res);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#define mod 1000007
|
||||
int p[501];
|
||||
int main (void){
|
||||
int t,n;
|
||||
memset (p, 0, sizeof(p));
|
||||
p[0] = 1;
|
||||
for (int i = 2; i <= 501; ++i){
|
||||
for (int j = i; j <= 501; ++j){
|
||||
p[j] += p[j - i];
|
||||
if (p[j] >= mod)
|
||||
p[j] -= mod;
|
||||
}
|
||||
}
|
||||
scanf("%d", &t);
|
||||
while(t--){
|
||||
scanf("%d", &n);
|
||||
printf("%d\n" ,p[n]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
#include <list>
|
||||
#include <functional>
|
||||
#include <numeric>
|
||||
#include <cctype>
|
||||
using namespace std;
|
||||
inline int getflen(int n) {
|
||||
int t = (int)ceil(n / 2.0);
|
||||
return n - t + 1;
|
||||
}
|
||||
inline int getslen(int n) {
|
||||
int s1 = n % 2 == 0 ? (n - 1) : n;
|
||||
int t1 = (int)floor(n/3.0 + 1/3.0) * 2 + 1;
|
||||
int ans = (s1 - t1) / 2 + 1;
|
||||
int s2 = (int)floor(n / 2.0 + 1 / 2.0) - 1;
|
||||
int t2 = (int)ceil(n/3.0);
|
||||
ans += s2 - t2 + 1;
|
||||
return ans;
|
||||
}
|
||||
inline int gettlen(int n) {
|
||||
int s1 = n;
|
||||
while(s1 % 3 == 0) {
|
||||
s1--;
|
||||
}
|
||||
int t1 = (int)floor(n/4.0 + 1/4.0) * 3 + 2;
|
||||
int s3 = s1 - 1;
|
||||
int t3 = (int)floor(n/4.0 + 1/4.0) * 3 + 1;
|
||||
int s2 = (int)floor(n/3.0 + 1/3.0) * 2 - 1;
|
||||
int t2 = (int)floor(n/4.0 + 1/4.0) * 2 + 1;
|
||||
int s4 = (int)ceil(n/3.0) - 1;
|
||||
int t4 = (int)ceil(n/4.0);
|
||||
int ans = 0;
|
||||
if(s1 % 3 == 1) {
|
||||
ans += 2;
|
||||
s1 -= 2;
|
||||
s3 -= 2;
|
||||
s4 -= 1;
|
||||
}
|
||||
ans += (s1 - t1) / 3 + 1;
|
||||
ans += (s3 - t3) / 3 + 1;
|
||||
ans += (s2 - t2) / 2 + 1;
|
||||
ans += s4 - t4 + 1;
|
||||
return ans;
|
||||
}
|
||||
inline void get_f(int n, int k, int &a, int &b) {
|
||||
a = 1;
|
||||
b = n - k + 1;
|
||||
}
|
||||
inline void get_s(int n, int k, int &a, int &b) {
|
||||
int s1 = n % 2 == 0 ? (n - 1) : n;
|
||||
int s2 = (int)floor(n / 2.0 + 1 / 2.0);
|
||||
if(k % 2 == 1) {
|
||||
a = 2;
|
||||
b = s1 - k + 1;
|
||||
}else {
|
||||
a = 1;
|
||||
b = s2 - k / 2;
|
||||
}
|
||||
}
|
||||
inline void get_t(int n, int k, int &a, int &b) {
|
||||
int s1 = n;
|
||||
while(s1 % 3 == 0) {
|
||||
s1--;
|
||||
}
|
||||
int s2 = (int)floor(n/3.0 + 1/3.0) * 2 - 1;
|
||||
int s3 = (s1 % 3 == 2) ? (s1 - 1) : (s1 - 2);
|
||||
int s4 = (int)ceil(n/3.0) - 1;
|
||||
if(k % 4 == 1) {
|
||||
a = 3;
|
||||
b = s1 - (k - 1) / 4 * 3;
|
||||
}else if(k % 4 == 3) {
|
||||
a = 3;
|
||||
b = s3 - (k - 3) / 4 * 3;
|
||||
}else if((k % 4 == 2) xor (s1 % 3 == 2)) {
|
||||
a = 1;
|
||||
if(k % 4 == 2) {
|
||||
b = s4 - (k - 2) / 4;
|
||||
}else {
|
||||
b = s4 - (k - 4) / 4;
|
||||
}
|
||||
}else {
|
||||
a = 2;
|
||||
if(k % 4 == 2) {
|
||||
b = s2 - (k - 2) / 4 * 2;
|
||||
}else {
|
||||
b = s2 - (k - 4) / 4 * 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
pair<int, int> get_farey(int n, int k) {
|
||||
pair<int, int> ret;
|
||||
int fl = getflen(n);
|
||||
int sl = getslen(n);
|
||||
if(k <= fl) {
|
||||
get_f(n, k, ret.first, ret.second);
|
||||
}else if(k <= sl + fl) {
|
||||
get_s(n, k - fl, ret.first, ret.second);
|
||||
}else {
|
||||
get_t(n, k - fl - sl, ret.first, ret.second);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
int main() {
|
||||
int T, n, k;
|
||||
scanf("%d", &T);
|
||||
while(T--) {
|
||||
scanf("%d%d", &n, &k);
|
||||
pair<int, int> ans = get_farey(n, k);
|
||||
printf("%d/%d\n", ans.first, ans.second);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstdlib>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<queue>
|
||||
#include<algorithm>
|
||||
#include<map>
|
||||
#include<iomanip>
|
||||
#define INF 99999999
|
||||
using namespace std;
|
||||
const int MAX=100+10;
|
||||
int dist[MAX],edgenum[MAX][MAX];
|
||||
bool mark[MAX],flag[MAX][MAX][MAX];
|
||||
int size,head[MAX],n,m,pos[3002],sum[MAX];
|
||||
struct Edge{
|
||||
int v,w,next;
|
||||
Edge(){}
|
||||
Edge(int V,int W,int Next):v(V),w(W),next(Next){}
|
||||
}edge[3002*2];
|
||||
inline void Init(int num){
|
||||
memset(head,-1,sizeof(int)*(num+2));
|
||||
memset(edgenum,0,sizeof edgenum);
|
||||
memset(flag,false,sizeof flag);
|
||||
size=0;
|
||||
}
|
||||
inline void InsertEdge(int u,int v,int w){
|
||||
edge[size]=Edge(v,w,head[u]);
|
||||
head[u]=size++;
|
||||
}
|
||||
int spfa(int s,bool p){
|
||||
int ans=0;
|
||||
queue<int>enqueue;
|
||||
for(int i=1;i<=n;++i)dist[i]=INF,mark[i]=false;
|
||||
dist[s]=0;
|
||||
mark[s]=true;
|
||||
enqueue.push(s);
|
||||
while(!enqueue.empty()){
|
||||
int u=enqueue.front();
|
||||
enqueue.pop();
|
||||
mark[u]=false;
|
||||
for(int i=head[u];i != -1;i=edge[i].next){
|
||||
int v=edge[i].v;
|
||||
if(dist[v]>dist[u]+edge[i].w){
|
||||
dist[v]=dist[u]+edge[i].w;
|
||||
if(!mark[v]){
|
||||
if(p)flag[s][u][v]=flag[s][v][u]=true;
|
||||
enqueue.push(v);
|
||||
mark[v]=true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(int i=1;i<=n;++i){
|
||||
if(dist[i] == INF)return INF;
|
||||
else ans+=dist[i];
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main(){
|
||||
int ans,u,v,temp;
|
||||
while(cin>>n>>m){
|
||||
Init(n);
|
||||
for(int i=0;i<m;++i){
|
||||
cin>>u>>v;
|
||||
pos[i]=size;
|
||||
InsertEdge(u,v,1);
|
||||
InsertEdge(v,u,1);
|
||||
++edgenum[u][v];
|
||||
++edgenum[v][u];
|
||||
}
|
||||
ans=temp=0;
|
||||
for(int i=1;i<=n;++i){
|
||||
sum[i]=spfa(i,1);
|
||||
if(sum[i] == INF){ans=INF;break;}
|
||||
else ans+=sum[i];
|
||||
}
|
||||
for(int i=0;i<m;++i){
|
||||
u=edge[pos[i]+1].v;
|
||||
v=edge[pos[i]].v;
|
||||
int s=ans;
|
||||
if(ans == INF)cout<<"INF"<<endl;
|
||||
else if(edgenum[u][v]-1>0)cout<<ans<<endl;
|
||||
else{
|
||||
edge[pos[i]+1].w=INF;
|
||||
edge[pos[i]].w=INF;
|
||||
for(int j=1;j<=n;++j){
|
||||
if(flag[j][u][v]){
|
||||
temp=spfa(j,0);
|
||||
if(temp == INF){cout<<"INF"<<endl;break;}
|
||||
else s+=temp-sum[j];
|
||||
}
|
||||
}
|
||||
if(temp != INF)cout<<s<<endl;
|
||||
edge[pos[i]+1].w=1;
|
||||
edge[pos[i]].w=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,230 @@
|
|||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
#define R 0
|
||||
#define G 1
|
||||
#define B 2
|
||||
#define Y 3
|
||||
#define MSIT multiset<Card>::iterator
|
||||
using namespace std;
|
||||
void re() {
|
||||
re();
|
||||
}
|
||||
struct Card {
|
||||
int score;
|
||||
string id;
|
||||
bool effect;
|
||||
bool operator<(Card o) const {
|
||||
if (score == o.score) {
|
||||
return id < o.id;
|
||||
} else {
|
||||
return score > o.score;
|
||||
}
|
||||
}
|
||||
bool fix(Card o) {
|
||||
if (o.id == "WC") {
|
||||
return true;
|
||||
}
|
||||
if (id[0] == o.id[0] || id[1] == o.id[1]) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
queue<Card> stock;
|
||||
Card top;
|
||||
int n;
|
||||
int curPlayer;
|
||||
int direction;
|
||||
struct Prople {
|
||||
multiset<Card> hand;
|
||||
int uno;
|
||||
int color;
|
||||
bool draw() {
|
||||
if (stock.empty()) {
|
||||
return false;
|
||||
} else {
|
||||
Card t = stock.front();
|
||||
stock.pop();
|
||||
hand.insert(t);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
bool isWin() {
|
||||
if (hand.size() == 0) {
|
||||
return true;
|
||||
} else if (hand.size() == 1) {
|
||||
uno++;
|
||||
return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
char getColor() {
|
||||
char ret;
|
||||
switch (color) {
|
||||
case R:
|
||||
ret = 'R';
|
||||
break;
|
||||
case G:
|
||||
ret = 'G';
|
||||
break;
|
||||
case B:
|
||||
ret = 'B';
|
||||
break;
|
||||
case Y:
|
||||
ret = 'Y';
|
||||
break;
|
||||
}
|
||||
color = (color + 1) % 4;
|
||||
return ret;
|
||||
}
|
||||
bool doIt() {
|
||||
MSIT wf = hand.end();
|
||||
for (MSIT it = hand.begin(); it != hand.end(); it++) {
|
||||
if (it->id == "WF") {
|
||||
wf = it;
|
||||
continue;
|
||||
}
|
||||
if (top.fix(*it)) {
|
||||
top = *it;
|
||||
if (top.id[0] == 'W') {
|
||||
top.id[0] = getColor();
|
||||
}
|
||||
hand.erase(it);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (wf != hand.end()) {
|
||||
top = *wf;
|
||||
top.id[0] = getColor();
|
||||
hand.erase(wf);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
int GetScore() {
|
||||
int sum = 0;
|
||||
for (MSIT it = hand.begin(); it != hand.end(); it++) {
|
||||
sum += it->score;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
} player[12];
|
||||
int nextPlayer(int cur, int direction) {
|
||||
return (cur + n + direction) % n;
|
||||
}
|
||||
void init() {
|
||||
while (stock.empty() == false) {
|
||||
stock.pop();
|
||||
}
|
||||
for (int i = 0; i < 12; i++) {
|
||||
player[i].hand.clear();
|
||||
player[i].uno = 0;
|
||||
player[i].color = 0;
|
||||
}
|
||||
}
|
||||
bool checkTop() {
|
||||
if (top.id[1] == 'F' && top.effect) {
|
||||
top.effect = false;
|
||||
if (player[curPlayer].draw() == false) {
|
||||
return false;
|
||||
}
|
||||
if (player[curPlayer].draw() == false) {
|
||||
return false;
|
||||
}
|
||||
if (player[curPlayer].draw() == false) {
|
||||
return false;
|
||||
}
|
||||
if (player[curPlayer].draw() == false) {
|
||||
return false;
|
||||
}
|
||||
curPlayer = nextPlayer(curPlayer, direction);
|
||||
} else if (top.id[1] == 'D' && top.effect) {
|
||||
top.effect = false;
|
||||
if (player[curPlayer].draw() == false) {
|
||||
return false;
|
||||
}
|
||||
if (player[curPlayer].draw() == false) {
|
||||
return false;
|
||||
}
|
||||
curPlayer = nextPlayer(curPlayer, direction);
|
||||
} else if (top.id[1] == 'S' && top.effect) {
|
||||
top.effect = false;
|
||||
curPlayer = nextPlayer(curPlayer, direction);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
int main() {
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
cin >> n;
|
||||
init();
|
||||
int i;
|
||||
for (i = 0; i < 108; i++) {
|
||||
Card temp;
|
||||
cin >> temp.id;
|
||||
temp.effect = true;
|
||||
if (temp.id[0] == 'W') {
|
||||
temp.score = 50;
|
||||
} else if ('0' <= temp.id[1] && temp.id[1] <= '9') {
|
||||
temp.score = temp.id[1] - '0';
|
||||
} else {
|
||||
temp.score = 20;
|
||||
}
|
||||
stock.push(temp);
|
||||
}
|
||||
for (int j = 0; j < n; j++) {
|
||||
for (i = 0; i < 7; i++) {
|
||||
player[j].draw();
|
||||
}
|
||||
}
|
||||
if (top.id[0] == 'W') {
|
||||
re();
|
||||
}
|
||||
top = stock.front();
|
||||
stock.pop();
|
||||
curPlayer = 1;
|
||||
direction = 1;
|
||||
if (top.id[1] == 'R' && top.effect) {
|
||||
curPlayer = n - 1;
|
||||
direction *= -1;
|
||||
top.effect = false;
|
||||
}
|
||||
while (true) {
|
||||
if (checkTop() == false) {
|
||||
break;
|
||||
}
|
||||
if (player[curPlayer].doIt() == false) {
|
||||
if (player[curPlayer].draw() == false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (top.id[1] == 'R' && top.effect) {
|
||||
top.effect = false;
|
||||
direction *= -1;
|
||||
}
|
||||
if (player[curPlayer].isWin()) {
|
||||
curPlayer = nextPlayer(curPlayer, direction);
|
||||
checkTop();
|
||||
break;
|
||||
}
|
||||
curPlayer = nextPlayer(curPlayer, direction);
|
||||
}
|
||||
for (i = 0; i < n - 1; i++) {
|
||||
printf("%d ", player[i].GetScore());
|
||||
}
|
||||
printf("%d\n", player[i].GetScore());
|
||||
for (i = 0; i < n - 1; i++) {
|
||||
printf("%d ", player[i].uno);
|
||||
}
|
||||
printf("%d\n", player[i].uno);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
#define N 110
|
||||
#define M 10100
|
||||
#define inf 10000000
|
||||
using namespace std;
|
||||
int n,k,dis[N],head[N],gap[N],f[N];
|
||||
int a[M*2];
|
||||
struct Edge{
|
||||
int v,c,next;
|
||||
}edge[M*2];
|
||||
void init(){
|
||||
memset(head,-1,sizeof(head));
|
||||
memset(dis,0,sizeof(dis));
|
||||
memset(gap,0,sizeof(gap));
|
||||
memset(f,0,sizeof(f));
|
||||
k=0;
|
||||
}
|
||||
void addedge(int u,int v,int c){
|
||||
edge[k].v=v;
|
||||
edge[k].c=c;
|
||||
edge[k].next=head[u];
|
||||
head[u]=k++;
|
||||
edge[k].v=u;
|
||||
edge[k].c=0;
|
||||
edge[k].next=head[v];
|
||||
head[v]=k++;
|
||||
}
|
||||
int dfs(int p,int s,int t,int limit){
|
||||
if(p==t)return limit;
|
||||
for(int i=head[p];i!=-1;i=edge[i].next){
|
||||
int v=edge[i].v;
|
||||
if(dis[p]==(dis[v]+1) && edge[i].c>0){
|
||||
int c=dfs(v,s,t,min(limit,edge[i].c));
|
||||
if(c<0)return c;
|
||||
if(c>0){
|
||||
edge[i].c-=c;
|
||||
edge[i^1].c+=c;
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
||||
int tmp=n;
|
||||
for(int i=head[p];i!=-1;i=edge[i].next){
|
||||
int v=edge[i].v;
|
||||
if(edge[i].c>0)
|
||||
tmp=min(tmp,dis[v]+1);
|
||||
}
|
||||
if(--gap[dis[p]]==0 || dis[s]>=n)return -1;
|
||||
++gap[dis[p]=tmp];
|
||||
return 0;
|
||||
}
|
||||
int SAP(int s,int t){
|
||||
gap[s]=n;
|
||||
int cap=0,now=0;
|
||||
while(~(now=dfs(s,s,t,inf))) cap+=now;
|
||||
return cap;
|
||||
}
|
||||
void dfs1(int u){
|
||||
f[u]=1;
|
||||
for(int i=head[u];i!=-1;i=edge[i].next){
|
||||
int v=edge[i].v;
|
||||
if(f[v]==0 && edge[i].c>0)
|
||||
dfs1(v);
|
||||
}
|
||||
}
|
||||
void dfs2(int u){
|
||||
f[u]=2;
|
||||
for(int i=head[u];i!=-1;i=edge[i].next){
|
||||
int v=edge[i].v;
|
||||
if(f[v]==0 && edge[i^1].c>0)
|
||||
dfs2(v);
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
int t,T,i,j;
|
||||
int V,E;
|
||||
int u,v,c;
|
||||
int sap,ans;
|
||||
int a1,a2;
|
||||
scanf("%d",&T);
|
||||
for(t=1;t<=T;t++){
|
||||
init();
|
||||
scanf("%d %d",&V,&E);
|
||||
for(i=1;i<=E;i++){
|
||||
scanf("%d %d %d",&u,&v,&c);
|
||||
addedge(u,v,c);
|
||||
}
|
||||
n=V;
|
||||
ans=SAP(1,V);
|
||||
dfs1(1);
|
||||
dfs2(V);
|
||||
for(i=0;i<k;i++)
|
||||
a[i]=edge[i].c;
|
||||
a1=0,a2=0;
|
||||
for(i=2;i<V;i++){
|
||||
if(f[i]==1){
|
||||
for(j=0;j<k;j++)
|
||||
edge[j].c=a[j];
|
||||
memset(dis,0,sizeof(dis));
|
||||
memset(gap,0,sizeof(gap));
|
||||
n=i;
|
||||
sap=SAP(1,i);
|
||||
a1=max(a1,sap);
|
||||
}
|
||||
if(f[i]==2){
|
||||
for(j=0;j<k;j++)
|
||||
edge[j].c=a[j];
|
||||
memset(dis,0,sizeof(dis));
|
||||
memset(gap,0,sizeof(gap));
|
||||
n=V-i+1;
|
||||
sap=SAP(i,V);
|
||||
a2=max(a2,sap);
|
||||
}
|
||||
}
|
||||
printf("%d\n",ans+min(a1,a2));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
struct point
|
||||
{
|
||||
double x,y,z;
|
||||
};
|
||||
int main()
|
||||
{
|
||||
int n,i;
|
||||
double xmin,xmax,ymin,ymax,zmin,zmax,xx,yy,zz,r;
|
||||
point points[10],ball;
|
||||
scanf("%d",&n);
|
||||
while(n--)
|
||||
{
|
||||
for(i=0;i<8;i++)
|
||||
scanf("%lf%lf%lf",&points[i].x,&points[i].y,&points[i].z);
|
||||
scanf("%lf%lf%lf%lf",&ball.x,&ball.y,&ball.z,&r);
|
||||
xmin=ymin=zmin=1000000;xmax=ymax=zmax=-1;
|
||||
for(i=0;i<8;i++)
|
||||
{
|
||||
if(xmin>points[i].x) xmin=points[i].x;
|
||||
if(xmax<points[i].x) xmax=points[i].x;
|
||||
if(ymin>points[i].y) ymin=points[i].y;
|
||||
if(ymax<points[i].y) ymax=points[i].y;
|
||||
if(zmin>points[i].z) zmin=points[i].z;
|
||||
if(zmax<points[i].z) zmax=points[i].z;
|
||||
}
|
||||
if(ball.x<xmin) xx=xmin;
|
||||
else if(ball.x>xmax) xx=xmax;
|
||||
else xx=ball.x;
|
||||
if(ball.y<ymin) yy=ymin;
|
||||
else if(ball.y>ymax) yy=ymax;
|
||||
else yy=ball.y;
|
||||
if(ball.z<xmin) zz=zmin;
|
||||
else if(ball.z>zmax) zz=zmax;
|
||||
else zz=ball.z;
|
||||
double diss=sqrt((xx-ball.x)*(xx-ball.x)+(yy-ball.y)*(yy-ball.y)+(zz-ball.z)*(zz-ball.z));
|
||||
if(diss>r) printf("No\n");
|
||||
else printf("Yes\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
# define MAX 1111
|
||||
# define INF 0x7FFFFFFF
|
||||
using namespace std;
|
||||
char a[MAX];
|
||||
struct node {
|
||||
int s,e,v,next;
|
||||
} ed[22222];
|
||||
int head[MAX],num = 0,n,m,k,st;
|
||||
int dp[MAX],vis[MAX][MAX];
|
||||
void add(int s,int e,int v) {
|
||||
ed[num].s = s;
|
||||
ed[num].e = e;
|
||||
ed[num].v = v;
|
||||
ed[num].next = head[s];
|
||||
head[s] = num++;
|
||||
}
|
||||
void init() {
|
||||
memset(vis,0,sizeof(vis));
|
||||
memset(head,-1,sizeof(head));
|
||||
num = 0;
|
||||
}
|
||||
int dfs(int v0,int u0,int cur) {
|
||||
if(a[v0 - 1] == 'P' && cur % k == 0) return dp[v0] = min(dp[v0],cur);
|
||||
if(dp[v0] != INF) return dp[v0];
|
||||
for(int i = head[v0]; i != -1; i = ed[i].next) {
|
||||
int e = ed[i].e;
|
||||
int v = ed[i].v + cur;
|
||||
if(vis[e][v%k] == 0 || v < vis[e][v % k]) {
|
||||
vis[e][v%k] = v;
|
||||
dfs(e,u0,v);
|
||||
}
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int T;
|
||||
cin >> T;
|
||||
int casee = 1;
|
||||
while(T --) {
|
||||
init();
|
||||
cin >> n >> m >> st >> k;
|
||||
for(int i=1; i<=n; i++)
|
||||
dp[i] = INF;
|
||||
getchar();
|
||||
int aa,bb,cc;
|
||||
scanf("%s",a);
|
||||
for(int i=1; i<=m; i++) {
|
||||
scanf("%d%d%d",&aa,&bb,&cc);
|
||||
add(aa,bb,cc);
|
||||
}
|
||||
printf("Case %d: ",casee++);
|
||||
dfs(st,st,0);
|
||||
int flag = 0,ind,ans = INF;
|
||||
for(int i=1; i<=n; i++) {
|
||||
if(dp[i] != INF) {
|
||||
flag = 1;
|
||||
if(ans > dp[i]) {
|
||||
ans = dp[i];
|
||||
ind = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(flag) printf("%d %d\n",ans,ind);
|
||||
else printf("-1 -1\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
#include <iostream>
|
||||
#include <math.h>
|
||||
#define PI 3.1415926535898
|
||||
using namespace std;
|
||||
double xx,yy,l,w;
|
||||
double d(double x){
|
||||
double s=sin(x),c=cos(x);
|
||||
return yy*c+xx*s-l*s*c;
|
||||
}
|
||||
int main(){
|
||||
double dl,dr,ml,mr;
|
||||
while(cin >> xx >> yy >> l >> w){
|
||||
dl=0;dr=PI/2;
|
||||
while(dr-dl>0.0000001){
|
||||
ml=dl/3*2+dr/3;
|
||||
mr=dl/3+dr/3*2;
|
||||
if(d(ml)<d(mr))dr=mr;else dl=ml;
|
||||
}
|
||||
if(w<d(dl))cout << "yes" << endl;
|
||||
else cout << "no" << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
#include<stdio.h>
|
||||
#include<math.h>
|
||||
#include<string>
|
||||
#include<vector>
|
||||
#include<queue>
|
||||
#include<list>
|
||||
using namespace std;
|
||||
typedef long long lld;
|
||||
typedef unsigned int ud;
|
||||
#define INF_MAX 0x3f3f3f3f
|
||||
#define eatline() char chch;while((chch=getchar())!='\n')continue;
|
||||
#define MemsetMax(a) memset(a,0x3f,sizeof a)
|
||||
#define MemsetZero(a) memset(a,0,sizeof a)
|
||||
#define MemsetMin(a) memset(a,-1,sizeof a)
|
||||
#define MemsetFalse(a) MemsetZero(a)
|
||||
#define PQ priority_queue
|
||||
#define Q queue
|
||||
#define maxn 100005
|
||||
#define maxm 1000005
|
||||
int c[maxn];
|
||||
int Rank[maxm];
|
||||
int main()
|
||||
{
|
||||
int n, m;
|
||||
while (scanf("%d%d", &n, &m) != EOF)
|
||||
{
|
||||
for (int i = 1; i <= n; i++)
|
||||
scanf("%d", &c[i]);
|
||||
for (int i = 1; i <= m; i++)
|
||||
scanf("%d", &Rank[i]);
|
||||
sort(Rank + 1, Rank + 1 + m);
|
||||
int ans = 0;
|
||||
for (int i = 1; i <= m; i++)
|
||||
{
|
||||
for (int j = 1; j <= Rank[i]; j++)
|
||||
{
|
||||
if (c[j] > 0)
|
||||
{
|
||||
c[j]--;
|
||||
ans++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,144 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<iostream>
|
||||
#include<utility>
|
||||
#include<string>
|
||||
#include<set>
|
||||
#include<vector>
|
||||
#include<stack>
|
||||
#include<algorithm>
|
||||
#include<queue>
|
||||
#include<cstdlib>
|
||||
#include<map>
|
||||
#include<cmath>
|
||||
using namespace std;
|
||||
#define FOR(i,a,b) for(int i(a);i<(b);i++)
|
||||
#define REP(i,n) FOR(i,0,n)
|
||||
#define CL(a,b) memset(a,b,sizeof(a))
|
||||
const int M=200;
|
||||
const int N=2000;
|
||||
const double inf=1e250;
|
||||
const double eps=1e-20;
|
||||
const double pi=acos(-1.0);
|
||||
struct point
|
||||
{
|
||||
double x,y;
|
||||
bool operator < (const point a)const
|
||||
{
|
||||
if(y==a.y)
|
||||
return x<a.x;
|
||||
return y<a.y;
|
||||
}
|
||||
}p[M],stac[M];
|
||||
int n,top;
|
||||
int xmult(double x1,double y1,double x2,double y2)
|
||||
{
|
||||
return x1*y2-x2*y1;
|
||||
}
|
||||
int cross(point p0,point p1,point p2)
|
||||
{
|
||||
double x1=p1.x-p0.x,y1=p1.y-p0.y;
|
||||
double x2=p2.x-p0.x,y2=p2.y-p0.y;
|
||||
return xmult(x1,y1,x2,y2);
|
||||
}
|
||||
void covex_hull()
|
||||
{
|
||||
sort(p,p+n);
|
||||
stac[0]=p[0];
|
||||
stac[1]=p[1];
|
||||
top=1;
|
||||
for(int i=2;i<n;i++)
|
||||
{
|
||||
while(top>0 && cross(stac[top],p[i],stac[top-1])<=0)
|
||||
top--;
|
||||
stac[++top]=p[i];
|
||||
}
|
||||
int temp=top;
|
||||
for(int i=n-2;i>=0;i--)
|
||||
{
|
||||
while(top>temp && cross(stac[top],p[i],stac[top-1])<=0)
|
||||
top--;
|
||||
stac[++top]=p[i];
|
||||
}
|
||||
--top;
|
||||
}
|
||||
point getpoint(double x,double y)
|
||||
{
|
||||
point temp;
|
||||
temp.x=x;temp.y=y;
|
||||
return temp;
|
||||
}
|
||||
double dist(point a,point b)
|
||||
{
|
||||
return sqrt((b.x-a.x)*(b.x-a.x)*1.0+(b.y-a.y)*(b.y-a.y));
|
||||
}
|
||||
double alldis(point a)
|
||||
{
|
||||
double dis=0;
|
||||
for(int i=0;i<=top;i++)
|
||||
{
|
||||
dis+=dist(a,stac[i]);
|
||||
}
|
||||
return dis;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(int i=0;i<n;i++)
|
||||
scanf("%lf%lf",&p[i].x,&p[i].y);
|
||||
covex_hull();
|
||||
point pp=stac[0];
|
||||
double ans=inf,step=100;
|
||||
ans=alldis(pp);
|
||||
while(step>0.2)
|
||||
{
|
||||
bool flag=true;
|
||||
while(flag)
|
||||
{
|
||||
flag=false;
|
||||
point qq=getpoint(pp.x,pp.y+step),tt=pp;
|
||||
double temp=alldis(qq);
|
||||
if(temp<ans)
|
||||
{
|
||||
ans=temp;
|
||||
tt=qq;
|
||||
flag=true;
|
||||
}
|
||||
qq=getpoint(pp.x,pp.y-step);
|
||||
temp=alldis(qq);
|
||||
if(temp<ans)
|
||||
{
|
||||
ans=temp;
|
||||
tt=qq;
|
||||
flag=true;
|
||||
}
|
||||
qq=getpoint(pp.x+step,pp.y);
|
||||
temp=alldis(qq);
|
||||
if(temp<ans)
|
||||
{
|
||||
ans=temp;
|
||||
tt=qq;
|
||||
flag=true;
|
||||
}
|
||||
qq=getpoint(pp.x-step,pp.y);
|
||||
temp=alldis(qq);
|
||||
if(temp<ans)
|
||||
{
|
||||
ans=temp;
|
||||
tt=qq;
|
||||
flag=true;
|
||||
}
|
||||
pp=tt;
|
||||
}
|
||||
step=step/2.0;
|
||||
}
|
||||
int dis=(int)(ans+0.5)*100/100;
|
||||
printf("%d\n",dis);
|
||||
if(T>0) puts("");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
#include<iostream>
|
||||
#include <CMATH>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
#define N 66000
|
||||
bool is[N]; __int64 prm[66000];
|
||||
int getprm(int n){
|
||||
int i, j, k = 0;
|
||||
int s, e = (int)(sqrt(0.0 + n) + 1);
|
||||
memset(is, 1, sizeof(is));
|
||||
prm[k++] = 2; is[0] = is[1] = 0;
|
||||
for (i = 4; i < n; i += 2) is[i] = 0;
|
||||
for (i = 3; i < e; i += 2) if (is[i]) {
|
||||
prm[k++] = i;
|
||||
for (s = i * 2, j = i * i; j < n; j += s)
|
||||
is[j] = 0;
|
||||
}
|
||||
for ( ; i < n; i += 2) if (is[i]) prm[k++] = i;
|
||||
return k;
|
||||
}
|
||||
bool div(char *p,int n)
|
||||
{
|
||||
char temp[1000];
|
||||
int i,sum=0,len=0;
|
||||
for(i=0;p[i]!=0;i++)
|
||||
{
|
||||
sum=sum*10+p[i]-'0';
|
||||
temp[len++]=sum/n+'0';
|
||||
sum%=n;
|
||||
}
|
||||
temp[len]=0;
|
||||
if(sum==0)
|
||||
{
|
||||
for(i=0;temp[i]=='0';i++);
|
||||
strcpy(p,temp+i);
|
||||
return 1;
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int cnt=getprm(66000);
|
||||
int i,n;
|
||||
char str[1010];
|
||||
while (scanf("%s",&str)&&str[0]!='0')
|
||||
{
|
||||
bool judge=true;
|
||||
if(strcmp(str,"1")==0)
|
||||
{
|
||||
printf("no\n");
|
||||
continue;
|
||||
}
|
||||
for(i=0;i<cnt;i++)
|
||||
{
|
||||
int sum=0;
|
||||
while(div(str,prm[i]))
|
||||
{
|
||||
sum++;
|
||||
if(sum>1)
|
||||
{
|
||||
judge=false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(judge)
|
||||
printf("yes\n");
|
||||
else
|
||||
printf("no\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
int dp[105][729],pow[10],stac1[10],stac2[10];
|
||||
int n,m,ans;
|
||||
int max(int a,int b)
|
||||
{
|
||||
return a>b?a:b;
|
||||
}
|
||||
void work()
|
||||
{
|
||||
pow[0]=1;
|
||||
for(int i=1;i<10;i++)
|
||||
pow[i]=3*pow[i-1];
|
||||
}
|
||||
int ctod(int three[10]){
|
||||
int state=0;
|
||||
for(int i=0;i<m;i++)
|
||||
state+=three[i]*pow[i];
|
||||
return state;
|
||||
}
|
||||
void dtoc(int state,int three[10]){
|
||||
for(int i=0;i<m;i++,state/=3)
|
||||
three[i]=state%3;
|
||||
}
|
||||
void init()
|
||||
{
|
||||
memset(dp,-1,sizeof(dp));
|
||||
for(int i=0;i<m;i++)
|
||||
{
|
||||
stac1[i]=1;
|
||||
stac2[i]=0;
|
||||
}
|
||||
dp[0][ctod(stac1)]=0;
|
||||
}
|
||||
void dfs(int i,int idx,int num){
|
||||
if(idx>=m){
|
||||
int j=ctod(stac1);
|
||||
dp[i][j]=max(dp[i][j],num);
|
||||
return ;
|
||||
}
|
||||
if(idx+1<m&&stac1[idx]==0&&stac1[idx+1]==0&&stac2[idx+1]==0){
|
||||
stac1[idx+1]=2;stac1[idx]=1;
|
||||
dfs(i,idx+2,num+4);
|
||||
stac1[idx+1]=stac1[idx]=0;
|
||||
}
|
||||
if(idx+2<m&&stac1[idx]==0&&stac1[idx+1]==0&&stac1[idx+2]==0&&stac2[idx+1]==0){
|
||||
stac1[idx]=stac1[idx+2]=1;stac1[idx+1]=2;
|
||||
dfs(i,idx+2,num+5);
|
||||
stac1[idx]=stac1[idx+2]=stac1[idx+1]=0;
|
||||
}
|
||||
if(idx+2<m&&stac1[idx]==0&&stac1[idx+1]==0&&stac1[idx+2]==0){
|
||||
stac1[idx]=stac1[idx+2]=1;stac1[idx+1]=2;
|
||||
dfs(i,idx+2,num+4);
|
||||
stac1[idx]=stac1[idx+2]=stac1[idx+1]=0;
|
||||
}
|
||||
if(idx+2<m&&stac1[idx]==0&&stac1[idx+1]==0&&stac1[idx+2]==0){
|
||||
stac1[idx]=stac1[idx+1]=1;stac1[idx+2]=2;
|
||||
dfs(i,idx+2,num+4);
|
||||
stac1[idx]=stac1[idx+2]=stac1[idx+1]=0;
|
||||
}
|
||||
if(idx+1<m&&stac1[idx+1]==0&&stac2[idx]==0&&stac2[idx+1]==0){
|
||||
stac1[idx+1]=2;
|
||||
dfs(i,idx+2,num+4);
|
||||
stac1[idx+1]=0;
|
||||
}
|
||||
dfs(i,idx+1,num);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,t,k,j;
|
||||
work();
|
||||
while(scanf("%d%d",&n,&m)!=EOF)
|
||||
{
|
||||
if(m>n)
|
||||
{
|
||||
t=m;
|
||||
m=n;
|
||||
n=t;
|
||||
}
|
||||
init();
|
||||
ans=0;
|
||||
for(i=1;i<=n-1;i++)
|
||||
for(j=0;j<pow[m];j++)
|
||||
if(dp[i-1][j]!=-1)
|
||||
{
|
||||
dtoc(j,stac2);
|
||||
for(k=0;k<m;k++)
|
||||
stac1[k]=max(0,stac2[k]-1);
|
||||
dfs(i,0,dp[i-1][j]);
|
||||
}
|
||||
ans=0;
|
||||
for(i=0;i<pow[m];i++)
|
||||
ans=max(ans,dp[n-1][i]);
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<memory.h>
|
||||
using namespace std;
|
||||
const int MAX=100005;
|
||||
int s[MAX],h[MAX];
|
||||
int n,a;
|
||||
__int64 ans;
|
||||
void merge(int l,int m,int r)
|
||||
{
|
||||
int i=l,j=m+1,k=l;
|
||||
while(i<=m&&j<=r)
|
||||
{
|
||||
if(s[i]<s[j])
|
||||
{
|
||||
ans+=r-j+1;
|
||||
h[k++]=s[i++];
|
||||
}
|
||||
else
|
||||
{
|
||||
h[k++]=s[j++];
|
||||
}
|
||||
}
|
||||
while(i<=m)
|
||||
h[k++]=s[i++];
|
||||
while(j<=r)
|
||||
h[k++]=s[j++];
|
||||
for(i=l;i<=r;i++)
|
||||
s[i]=h[i];
|
||||
}
|
||||
void mergesort(int l,int r)
|
||||
{
|
||||
int m=(l+r)/2;
|
||||
if(l<r)
|
||||
{
|
||||
mergesort(l,m);
|
||||
mergesort(m+1,r);
|
||||
merge(l,m,r);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,T;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
scanf("%d%d",&n,&a);
|
||||
s[0]=0;
|
||||
ans=0;
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
scanf("%d",&j);
|
||||
j-=a;
|
||||
s[i]=s[i-1]+j;
|
||||
}
|
||||
mergesort(0,n);
|
||||
cout<<ans<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
bool map[210][210];
|
||||
bool visit[210];
|
||||
int link[210];
|
||||
int judge[210];
|
||||
int queue[210];
|
||||
int n,m;
|
||||
bool BFS()
|
||||
{
|
||||
int v,start = 0,end = 1;
|
||||
queue[0] = 1;
|
||||
for(int i=0;i<=n;i++)
|
||||
judge[i] = -1;
|
||||
v = queue[start];
|
||||
judge[1] = 0;
|
||||
memset(visit,0,sizeof(visit));
|
||||
while(start<end)
|
||||
{
|
||||
v= queue[start];
|
||||
for(int i = 1;i <= n; i++)
|
||||
{
|
||||
if(map[v][i]){
|
||||
if(judge[i] == -1){
|
||||
judge[i] = (judge[v]+1)%2;
|
||||
queue[end++] = i;
|
||||
}
|
||||
else{
|
||||
if(judge[i] == judge[v])
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
start++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
int can(int r)
|
||||
{
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
if(map[r][i] && visit[i] == 0){
|
||||
visit[i] = 1;
|
||||
if(link[i]==0 || can(link[i])){
|
||||
link[i] = r;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d%d",&n,&m)!=EOF)
|
||||
{
|
||||
memset(map,0,sizeof(map));
|
||||
int a,b;
|
||||
for(int i=0;i<m;i++)
|
||||
{
|
||||
scanf("%d%d",&a,&b);
|
||||
map[a][b] = 1;
|
||||
map[b][a] = 1;
|
||||
}
|
||||
if(!BFS()) {
|
||||
printf("No\n");continue;
|
||||
}
|
||||
int num = 0;
|
||||
memset(link,0,sizeof(link));
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
memset(visit,0,sizeof(visit));
|
||||
if(can(i)) num++;
|
||||
}
|
||||
printf("%d\n",num/2);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
#define N 1000001
|
||||
long long num[N];
|
||||
int findLayer(long long n)
|
||||
{
|
||||
int head = 0, tail = N - 1;
|
||||
int middle= (int)((head + tail) / 2);
|
||||
while (head != tail)
|
||||
{
|
||||
if (num[middle] < n)
|
||||
head = middle + 1;
|
||||
else if (num[middle - 1] > n)
|
||||
tail = middle - 1;
|
||||
else
|
||||
break;
|
||||
middle = (int)((head + tail) / 2);
|
||||
}
|
||||
return middle+1;
|
||||
}
|
||||
long long getNum(long long i)
|
||||
{
|
||||
return (1 + i)*i / 2;
|
||||
}
|
||||
int findRow(long long n,int layer)
|
||||
{
|
||||
long long index = n - num[layer - 2];
|
||||
long long head = 1;
|
||||
long long tail = layer;
|
||||
long long middle = (int)((head + tail) / 2);
|
||||
while (head != tail)
|
||||
{
|
||||
if (getNum(middle) < index)
|
||||
head = middle + 1;
|
||||
else if (getNum(middle - 1) >= index)
|
||||
tail = middle - 1;
|
||||
else
|
||||
break;
|
||||
middle = (int)((head + tail) / 2);
|
||||
}
|
||||
return middle;
|
||||
}
|
||||
int findColumn(long long n,int row,int layer)
|
||||
{
|
||||
long long index = n - num[layer - 2]-getNum(row-1);
|
||||
return index;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
num[0] = 1;
|
||||
num[1] = 4;
|
||||
for (int i = 2; i < N; i++)
|
||||
num[i] = 2 * num[i - 1] - num[i - 2] + i+1;
|
||||
int T;
|
||||
cin >> T;
|
||||
long long n;
|
||||
int layer, row, column;
|
||||
while (T--)
|
||||
{
|
||||
cin >> n;
|
||||
layer = findLayer(n);
|
||||
row = findRow(n, layer);
|
||||
column = findColumn(n, row,layer);
|
||||
cout << layer << " " << row << " " << column << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,167 @@
|
|||
#include<iostream>
|
||||
#include<cmath>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
const int maxn=500000;
|
||||
const int mod=100000000;
|
||||
int prime[maxn];
|
||||
int flag[maxn];
|
||||
int px[10006];
|
||||
int n,K,mark;
|
||||
struct bign
|
||||
{
|
||||
__int64 val[1000];
|
||||
int len;
|
||||
bign(){memset(val,0,sizeof(val));len=1;}
|
||||
};
|
||||
bign a,b,c,d,dd,e;
|
||||
int get_pri()
|
||||
{
|
||||
int i,j,k;
|
||||
k=0;
|
||||
memset(flag,0,sizeof(flag));
|
||||
flag[0]=flag[1]=1;
|
||||
for(i=2;i<maxn;i++)
|
||||
{
|
||||
if(!flag[i])prime[k++]=i;
|
||||
for(j=0;j<k&&prime[j]*i<maxn;j++)
|
||||
{
|
||||
flag[prime[j]*i]=1;
|
||||
if(i%prime[j]==0)break;
|
||||
}
|
||||
}
|
||||
return k;
|
||||
}
|
||||
void Init(bign &cc,int t)
|
||||
{
|
||||
int j=0;
|
||||
while(t)
|
||||
{
|
||||
cc.val[j++]=t%mod;
|
||||
t/=mod;
|
||||
}
|
||||
cc.len=j;
|
||||
return ;
|
||||
}
|
||||
void multify(bign &aa,bign &bb)
|
||||
{
|
||||
int i,j;
|
||||
memset(dd.val,0,sizeof(dd.val));
|
||||
dd.len =1;
|
||||
for(i=0;i<aa.len;i++)
|
||||
for(j=0;j<bb.len;j++)
|
||||
{
|
||||
dd.val[i+j]+=aa.val[i]*bb.val[j];
|
||||
dd.val[i+j+1]+=dd.val[i+j]/mod;
|
||||
dd.val[i+j]%=mod;
|
||||
}
|
||||
dd.len=aa.len+bb.len;
|
||||
if(dd.val[dd.len-1]==0)dd.len--;
|
||||
aa=dd;
|
||||
return ;
|
||||
}
|
||||
void power(bign &temp,bign &x,int y)
|
||||
{
|
||||
temp.len=1;
|
||||
temp.val[0]=1;
|
||||
while(y)
|
||||
{
|
||||
if(y&1)multify(temp,x);
|
||||
multify(x,x);
|
||||
y>>=1;
|
||||
}
|
||||
return ;
|
||||
}
|
||||
void cal_xx()
|
||||
{
|
||||
int i,j,k=0,t;
|
||||
for(i=4;i<=100000;i++)
|
||||
{
|
||||
t=i;
|
||||
for(j=0;prime[j]*prime[j]<t;j++)
|
||||
{
|
||||
if(t%prime[j])continue;
|
||||
t=t/prime[j];
|
||||
if(!flag[t])
|
||||
px[k++]=i;
|
||||
else break;
|
||||
if(k==10000)break;
|
||||
}
|
||||
if(k==10000)break;
|
||||
}
|
||||
}
|
||||
int cmp(bign &x,bign &y)
|
||||
{
|
||||
if(x.len>y.len)return 1;
|
||||
if(x.len<y.len)return -1;
|
||||
int i;
|
||||
for(i=x.len-1;i>=0;i--)
|
||||
{
|
||||
if(x.val[i]>y.val[i])
|
||||
return 1;
|
||||
if(x.val[i]<y.val[i])
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void solve( bign &t)
|
||||
{
|
||||
int i=0,j=0,k;
|
||||
int f1=0,f2=0;
|
||||
for(k=0;k<n;k++)
|
||||
{
|
||||
if(!f1)
|
||||
{
|
||||
Init(a,px[i]);
|
||||
}
|
||||
if(!f2)
|
||||
{
|
||||
Init(d,prime[j]);
|
||||
power(b,d,mark+1);
|
||||
}
|
||||
if(cmp(a,b)==1)
|
||||
{
|
||||
if(k==n-1)e=b;
|
||||
f1=1;f2=0; j++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(k==n-1)e=a;
|
||||
f1=0;f2=1;i++;
|
||||
}
|
||||
}
|
||||
power(t,e,mark-1);
|
||||
}
|
||||
void output(bign &x)
|
||||
{
|
||||
int i=x.len;
|
||||
printf("%d",x.val[--i]);
|
||||
while(i)
|
||||
{
|
||||
printf("%08d",x.val[--i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
int main()
|
||||
{
|
||||
get_pri();
|
||||
cal_xx();
|
||||
while(EOF!=scanf("%d%d",&n,&K))
|
||||
{
|
||||
if(K==1){printf("1\n");continue;}
|
||||
mark=0;
|
||||
if(flag[K]){mark=(int)sqrt(K*1.0);}
|
||||
if(!mark)
|
||||
{
|
||||
Init(d,prime[n-1]);
|
||||
power(c,d,K-1);
|
||||
output(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
solve(c);
|
||||
output(c);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <queue>
|
||||
#define Max 305
|
||||
#define inf 99999999
|
||||
using namespace std;
|
||||
int n,m,k,p;
|
||||
bool visitedX[Max],visitedY[Max];
|
||||
int lx[Max],ly[Max];
|
||||
int Match[Max];
|
||||
int slack[Max];
|
||||
int Graph[Max][Max];
|
||||
int o[Max];
|
||||
struct node
|
||||
{
|
||||
int adj[Max],w[Max];
|
||||
int len;
|
||||
}list[Max];
|
||||
void AddAdj(int s,int e,int w)
|
||||
{
|
||||
list[s].adj[list[s].len] = e;
|
||||
list[s].w[list[s].len++] = w;
|
||||
}
|
||||
void SPFA(int locate)
|
||||
{
|
||||
int dist[Max],i,u,v,w;
|
||||
bool visited[Max];
|
||||
queue<int>q;
|
||||
for(i=1;i<=n+m;i++)
|
||||
visited[i] = 0,dist[i] = inf;
|
||||
visited[locate] = 1;
|
||||
dist[locate] = 0;
|
||||
q.push(locate);
|
||||
while (!q.empty())
|
||||
{
|
||||
u = q.front();
|
||||
q.pop();
|
||||
visited[u] = 0;
|
||||
for (i=0;i<list[u].len;i++)
|
||||
{
|
||||
v = list[u].adj[i];
|
||||
w = list[u].w[i];
|
||||
if (dist[v]>dist[u]+w)
|
||||
{
|
||||
dist[v] = dist[u]+w;
|
||||
if(!visited[v])
|
||||
q.push(v),visited[v] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i=m+1;i<=m+n;i++)
|
||||
{
|
||||
if(dist[i]!=0x7fffffff)
|
||||
Graph[locate][i] = -dist[i];
|
||||
else
|
||||
Graph[locate][i] = 0;
|
||||
}
|
||||
}
|
||||
bool DFS(int i)
|
||||
{
|
||||
int j,tem;
|
||||
visitedX[i] = true;
|
||||
for (j=m+1;j<=n+m;j++)
|
||||
{
|
||||
if (!visitedY[j])
|
||||
{
|
||||
tem = lx[i]+ly[j]-Graph[i][j];
|
||||
if (tem==0)
|
||||
{
|
||||
visitedY[j] = 1;
|
||||
if (Match[j]==-1||DFS(Match[j]))
|
||||
{
|
||||
Match[j] = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if(slack[j]>tem)
|
||||
slack[j] = tem;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void KM()
|
||||
{
|
||||
int i,j,k,min;
|
||||
for (i=1;i<=n;i++)
|
||||
{
|
||||
lx[o[i]] = -inf;
|
||||
ly[i+m] = 0;
|
||||
Match[i+m] = -1;
|
||||
for (j=m+1;j<=m+n;j++)
|
||||
if(lx[o[i]]<Graph[o[i]][j])
|
||||
lx[o[i]] = Graph[o[i]][j];
|
||||
}
|
||||
for (k=1;k<=n;k++)
|
||||
{
|
||||
for(i=m+1;i<=n+m;i++)
|
||||
slack[i] = inf;
|
||||
while (1)
|
||||
{
|
||||
min = inf;
|
||||
memset(visitedX,0,sizeof(visitedX));
|
||||
memset(visitedY,0,sizeof(visitedY));
|
||||
if(DFS(o[k]))
|
||||
break;
|
||||
for(i=1+m;i<=n+m;i++)
|
||||
{
|
||||
if(!visitedY[i]&&slack[i]<min)
|
||||
min = slack[i];
|
||||
}
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
if(visitedX[o[i]])
|
||||
lx[o[i]]-=min;
|
||||
if(visitedY[i+m])
|
||||
ly[i+m]+=min;
|
||||
else
|
||||
slack[i+m]-=min;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,val,sum;
|
||||
while (scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)
|
||||
{
|
||||
for(i=1;i<=n;i++)
|
||||
scanf("%d",&o[i]);
|
||||
for(i=1;i<=m;i++)
|
||||
list[i].len = 0;
|
||||
while (k--)
|
||||
{
|
||||
scanf("%d%d%d",&i,&j,&val);
|
||||
AddAdj(i,j,val);
|
||||
AddAdj(j,i,val);
|
||||
}
|
||||
while (p--)
|
||||
{
|
||||
scanf("%d%d%d",&i,&j,&val);
|
||||
AddAdj(j,i+m,val);
|
||||
}
|
||||
memset(Graph,0,sizeof(Graph));
|
||||
for(i=1;i<=n;i++)
|
||||
SPFA(o[i]);
|
||||
KM();
|
||||
for(sum = 0,i=m+1;i<=n+m;i++)
|
||||
sum+=lx[Match[i]]+ly[i];
|
||||
printf("%d\n",-sum);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int maxn=20;
|
||||
char a[20];
|
||||
int c[20];
|
||||
int main()
|
||||
{
|
||||
int i,j,k,n;
|
||||
c[1]=3;
|
||||
for(i=2;i<=10;i++)
|
||||
c[i]=c[i-1]*4;
|
||||
while(cin>>a)
|
||||
{
|
||||
n=strlen(a);
|
||||
int ans=0;
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
if(i==0&&i!=n-1)
|
||||
{
|
||||
if(a[i]>'3'){ans+=4*c[n-i-1];break;}
|
||||
ans+=(a[i]-'0')*c[n-i-1];
|
||||
}
|
||||
else if(i==n-1)
|
||||
{
|
||||
if(a[i]>'3'){ans+=3;break;}
|
||||
ans+=a[i]-'0';
|
||||
}
|
||||
else
|
||||
{
|
||||
if(a[i]>'3'){ans+=4*c[n-i-1];break;}
|
||||
ans+=(a[i]-'0')*c[n-i-1];
|
||||
}
|
||||
}
|
||||
cout<<ans<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
const int NM=10005;
|
||||
const int MAX=0xfffffff;
|
||||
int du[NM],dp[NM][2],a[NM];
|
||||
vector<int>v[NM];
|
||||
inline int max(int x,int y){
|
||||
return x>y?x:y;
|
||||
}
|
||||
inline int min(int x,int y){
|
||||
return x<y?x:y;
|
||||
}
|
||||
int DFS(int t,int s)
|
||||
{
|
||||
int u,ans;
|
||||
if(t&1==1){
|
||||
if(dp[s][1]!=-1) return dp[s][1];
|
||||
dp[s][1]=a[s];
|
||||
for(int i=0;i<v[s].size();i++){
|
||||
u=v[s][i];
|
||||
dp[s][1]=max(dp[s][1],a[s]+DFS(t+1,u));
|
||||
}
|
||||
return dp[s][1];
|
||||
}
|
||||
else{
|
||||
if(dp[s][0]!=-1) return dp[s][0];
|
||||
dp[s][0]=a[s];
|
||||
ans=MAX;
|
||||
for(int i=0;i<v[s].size();i++){
|
||||
u=v[s][i];
|
||||
ans=min(ans,a[s]+DFS(t+1,u));
|
||||
dp[s][0]=ans;
|
||||
}
|
||||
return dp[s][0];
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,m,f,i,x,y,u;
|
||||
while(~scanf("%d%d%d",&n,&m,&f)){
|
||||
for(i=1;i<NM;i++)
|
||||
v[i].clear();
|
||||
for(i=1;i<=n;i++) scanf("%d",&a[i]);
|
||||
memset(du,0,sizeof(du));
|
||||
for(i=0;i<m;i++){
|
||||
scanf("%d%d",&x,&y);
|
||||
du[y]++;
|
||||
v[x].push_back(y);
|
||||
}
|
||||
for(i=1;i<=n;i++){
|
||||
if(du[i]==0){
|
||||
u=i;break;
|
||||
}
|
||||
}
|
||||
memset(dp,-1,sizeof(dp));
|
||||
if(DFS(1,u)>=f) printf("Victory\n");
|
||||
else printf("Glory\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,137 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int x,y;
|
||||
int res;
|
||||
int da;
|
||||
int op;
|
||||
int A;
|
||||
int j;
|
||||
};
|
||||
bool operator < (node i,node j)
|
||||
{
|
||||
if(i.da==j.da)
|
||||
{
|
||||
if(i.A==j.A) return i.op>j.op;
|
||||
return i.A>j.A;
|
||||
}
|
||||
return i.da>j.da;
|
||||
}
|
||||
int t,n,m,d,e,c;
|
||||
char map[30][30];
|
||||
int dx[]={-1,0,0,1};
|
||||
int dy[]={0,1,-1,0};
|
||||
int da[30][30],A[30][30],jia[30][30];
|
||||
inline void bfs(node vs,node ve)
|
||||
{
|
||||
int flag=-1;
|
||||
for(int i=0;i<n;i++)
|
||||
for(int j=0;j<m;j++) da[i][j]=jia[i][j]=A[i][j]=410;
|
||||
priority_queue<node>q;
|
||||
node vn,vw;
|
||||
da[vs.x][vs.y]=A[vs.x][vs.y]=jia[vs.x][vs.y]=0;
|
||||
vn.x=vs.x,vn.y=vs.y;
|
||||
vn.res=e,vn.da=0,vn.op=0,vn.A=0;
|
||||
q.push(vn);
|
||||
while(!q.empty())
|
||||
{
|
||||
vn=q.top();q.pop();
|
||||
if(vn.x==ve.x&&vn.y==ve.y)
|
||||
{
|
||||
flag=vn.op;
|
||||
break;
|
||||
}
|
||||
for(int i=0;i<4;i++)
|
||||
{
|
||||
int a,b;
|
||||
if(vn.res>=c)
|
||||
{
|
||||
a=vn.x,b=vn.y;
|
||||
int tag=1;
|
||||
for(int j=0;j<d;j++)
|
||||
{
|
||||
a+=dx[i],b+=dy[i];
|
||||
if(a<0||a>=n||b<0||b>=m)
|
||||
{
|
||||
tag=0;break;
|
||||
}
|
||||
if(map[a][b]!=' '&&map[a][b]!='E')
|
||||
{
|
||||
tag=0;break;
|
||||
}
|
||||
}
|
||||
if(tag)
|
||||
{
|
||||
if(da[a][b]>vn.da||(da[a][b]==vn.da&&A[a][b]>vn.A+d)||((da[a][b]==vn.da&&A[a][b]==vn.A+d&&jia[a][b]>vn.j+1)))
|
||||
{
|
||||
da[a][b]=vn.da;
|
||||
A[a][b]=vn.A+d;
|
||||
jia[a][b]=vn.j+1;
|
||||
vw.x=a,vw.y=b;
|
||||
vw.res=vn.res-c;
|
||||
vw.da=vn.da;
|
||||
vw.op=vn.op+1;
|
||||
vw.A=vn.A+d;
|
||||
vw.j=vn.j+1;
|
||||
q.push(vw);
|
||||
}
|
||||
}
|
||||
}
|
||||
a=vn.x+dx[i],b=vn.y+dy[i];
|
||||
if(a>=0&&a<n&&b>=0&&b<m&&map[a][b]!='#')
|
||||
{
|
||||
if(map[a][b]!='*'&&((da[a][b]>vn.da||(da[a][b]==vn.da&&A[a][b]>vn.A+1)||((da[a][b]==vn.da&&A[a][b]==vn.A+1&&jia[a][b]>vn.j)))))
|
||||
{
|
||||
da[a][b]=vn.da;
|
||||
A[a][b]=vn.A+1;
|
||||
jia[a][b]=vn.j;
|
||||
vw.x=a,vw.y=b;
|
||||
vw.da=vn.da;
|
||||
vw.res=vn.res;
|
||||
vw.op=vn.op+1;
|
||||
vw.j=vn.j;
|
||||
vw.A=vn.A+1;
|
||||
q.push(vw);
|
||||
}
|
||||
else if(map[a][b]=='*'&&vn.res>=c&&((da[a][b]>vn.da||(da[a][b]==vn.da&&A[a][b]>vn.A+1)||((da[a][b]==vn.da&&A[a][b]==vn.A+1&&jia[a][b]>vn.j+1)))))
|
||||
{
|
||||
da[a][b]=vn.da+1;
|
||||
A[a][b]=vn.A+1;
|
||||
jia[a][b]=vn.j+1;
|
||||
vw.x=a,vw.y=b;
|
||||
vw.da=vn.da+1;
|
||||
vw.res=vn.res-c;
|
||||
vw.op=vn.op+1;
|
||||
vw.A=vn.A+1;
|
||||
vw.j=vn.j+1;
|
||||
q.push(vw);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(flag==-1) printf("can not reach!\n");
|
||||
else printf("%d\n",flag);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d%d",&n,&m);
|
||||
getchar();
|
||||
for(int i=0;i<n;i++) gets(map[i]);
|
||||
scanf("%d%d%d",&d,&e,&c);
|
||||
node vs,ve;
|
||||
for(int i=0;i<n;i++)
|
||||
for(int j=0;j<m;j++)
|
||||
{
|
||||
if(map[i][j]=='S') vs.x=i,vs.y=j;
|
||||
else if(map[i][j]=='E') ve.x=i,ve.y=j;
|
||||
}
|
||||
bfs(vs,ve);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
int cmp(int a,int b)
|
||||
{
|
||||
return a>b;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
scanf("%d",&t);
|
||||
while (t--)
|
||||
{
|
||||
int n,i,j,a[1005],sum=0,flag=1;
|
||||
scanf("%d",&n);
|
||||
for (i=0;i<n;i++){
|
||||
scanf("%d",&a[i]);
|
||||
sum+=a[i];
|
||||
}
|
||||
sort(a,a+n,cmp);
|
||||
if (sum%2!=0)
|
||||
{
|
||||
flag=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
while(a[0]>0)
|
||||
{
|
||||
for (j=1;j<=a[0];j++)
|
||||
a[j]--;
|
||||
a[0]=0;
|
||||
sort(a,a+n,cmp);
|
||||
}
|
||||
sort(a,a+n);
|
||||
if (a[0]<0)
|
||||
{
|
||||
flag=0;
|
||||
}
|
||||
}
|
||||
if (flag!=0)
|
||||
printf("yes\n");
|
||||
else
|
||||
printf("no\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <queue>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#define MAXN 111111
|
||||
#define MAXM 555555
|
||||
#define INF 100000011
|
||||
#define lch(x) x<<1
|
||||
#define rch(x) x<<1|1
|
||||
#define lson l,m,rt<<1
|
||||
#define rson m+1,r,rt<<1|1
|
||||
#define eps 1e-7
|
||||
using namespace std;
|
||||
long long h[1111][1111];
|
||||
int n, m, t, p, q;
|
||||
char mp[1111][1111], tmp[66];
|
||||
long long a[66];
|
||||
bool ok()
|
||||
{
|
||||
for(int i = 0; i + p - 1 < n; i++)
|
||||
for(int j = q - 1; j < m; j++)
|
||||
{
|
||||
int flag = 1;
|
||||
for(int k = 0; k < p; k++)
|
||||
if(h[i + k][j] != a[k])
|
||||
{
|
||||
flag = 0;
|
||||
break;
|
||||
}
|
||||
if(flag) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int cas = 0;
|
||||
while(scanf("%d %d %d %d %d", &n, &m, &t, &p, &q) != EOF)
|
||||
{
|
||||
if(!n && !m && !t && !q && !p) break;
|
||||
for(int i = 0; i < n; i++) scanf("%s", mp[i]);
|
||||
memset(h, 0, sizeof(h));
|
||||
for(int i = 0; i < n; i++)
|
||||
for(int j = 0; j < q; j++)
|
||||
{
|
||||
if(mp[i][j] == '*') h[i][q - 1] |= (1LL << j);
|
||||
}
|
||||
for(int i = 0; i < n; i++)
|
||||
for(int j = q; j < m; j++)
|
||||
{
|
||||
if(mp[i][j - q] == '*') h[i][j] = h[i][j - 1] - 1LL;
|
||||
else h[i][j] = h[i][j - 1];
|
||||
h[i][j] >>= 1LL;
|
||||
if(mp[i][j] == '*') h[i][j] |= (1LL << (q - 1));
|
||||
}
|
||||
int cnt = 0;
|
||||
while(t--)
|
||||
{
|
||||
for(int i = 0; i < p; i++)
|
||||
{
|
||||
scanf("%s", tmp);
|
||||
a[i] = 0;
|
||||
for(int j = 0; j < q; j++)
|
||||
if(tmp[j] == '*') a[i] |= (1LL << j);
|
||||
}
|
||||
if(ok()) cnt++;
|
||||
}
|
||||
printf("Case %d: %d\n", ++cas, cnt);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<cmath>
|
||||
#include<algorithm>
|
||||
#define N 100005
|
||||
#define MOD 100000
|
||||
#define inf 1<<29
|
||||
#define LL long long
|
||||
using namespace std;
|
||||
struct Trie{
|
||||
Trie *next[4];
|
||||
Trie *fail;
|
||||
int kind,isword;
|
||||
};
|
||||
Trie *que[N],s[N];
|
||||
int idx;
|
||||
int id(char ch){
|
||||
if(ch=='A') return 0;
|
||||
else if(ch=='T') return 1;
|
||||
else if(ch=='C') return 2;
|
||||
return 3;
|
||||
}
|
||||
Trie *NewNode(){
|
||||
Trie *tmp=&s[idx];
|
||||
for(int i=0;i<4;i++) tmp->next[i]=NULL;
|
||||
tmp->isword=0;
|
||||
tmp->kind=idx++;
|
||||
tmp->fail=NULL;
|
||||
return tmp;
|
||||
}
|
||||
void Insert(Trie *root,char *s,int len){
|
||||
Trie *p=root;
|
||||
for(int i=0;i<len;i++){
|
||||
if(p->next[id(s[i])]==NULL)
|
||||
p->next[id(s[i])]=NewNode();
|
||||
p=p->next[id(s[i])];
|
||||
}
|
||||
p->isword=1;
|
||||
}
|
||||
void Bulid_Fail(Trie *root){
|
||||
int head=0,tail=0;
|
||||
que[tail++]=root;
|
||||
root->fail=NULL;
|
||||
while(head<tail){
|
||||
Trie *tmp=que[head++];
|
||||
for(int i=0;i<4;i++){
|
||||
if(tmp->next[i]){
|
||||
if(tmp==root) tmp->next[i]->fail=root;
|
||||
else{
|
||||
Trie *p=tmp->fail;
|
||||
while(p!=NULL){
|
||||
if(p->next[i]){
|
||||
tmp->next[i]->fail=p->next[i];
|
||||
break;
|
||||
}
|
||||
p=p->fail;
|
||||
}
|
||||
if(p==NULL) tmp->next[i]->fail=root;
|
||||
}
|
||||
if(tmp->next[i]->fail->isword) tmp->next[i]->isword=1;
|
||||
que[tail++]=tmp->next[i];
|
||||
}
|
||||
else if(tmp==root) tmp->next[i]=root;
|
||||
else tmp->next[i]=tmp->fail->next[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
int dp[1005][2005];
|
||||
int slove(char *str,int len){
|
||||
for(int i=0;i<=len;i++) for(int j=0;j<idx;j++) dp[i][j]=inf;
|
||||
dp[0][0]=0;
|
||||
for(int i=1;i<=len;i++){
|
||||
for(int j=0;j<idx;j++){
|
||||
if(s[j].isword) continue;
|
||||
if(dp[i-1][j]==inf) continue;
|
||||
for(int k=0;k<4;k++){
|
||||
int r=s[j].next[k]->kind;
|
||||
if(s[r].isword) continue;
|
||||
dp[i][r]=min(dp[i][r],dp[i-1][j]+(id(str[i-1])!=k));
|
||||
}
|
||||
}
|
||||
}
|
||||
int ans=inf;
|
||||
for(int i=0;i<idx;i++) ans=min(ans,dp[len][i]);
|
||||
return ans==inf?-1:ans;
|
||||
}
|
||||
char str[1005];
|
||||
int main(){
|
||||
int n,cas=0;
|
||||
while(scanf("%d",&n)!=EOF&&n){
|
||||
idx=0;
|
||||
Trie *root=NewNode();
|
||||
for(int i=0;i<n;i++){
|
||||
scanf("%s",str);
|
||||
Insert(root,str,strlen(str));
|
||||
}
|
||||
Bulid_Fail(root);
|
||||
scanf("%s",str);
|
||||
printf("Case %d: %d\n",++cas,slove(str,strlen(str)));
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#define MAXN 1000000+100
|
||||
struct aa
|
||||
{
|
||||
int a,b,next;
|
||||
}D[MAXN];
|
||||
int list[MAXN],tot;
|
||||
int mkdfs[210];
|
||||
int mkgx[210];
|
||||
int mkmp[210][210];
|
||||
void add(int a,int b)
|
||||
{
|
||||
D[++tot].a=a;
|
||||
D[tot].b=b;
|
||||
D[tot].next=list[a];
|
||||
list[a]=tot;
|
||||
}
|
||||
int dfs(int a)
|
||||
{
|
||||
for(int k=list[a];k;k=D[k].next)
|
||||
{
|
||||
int to=D[k].b;
|
||||
if(mkdfs[to]) continue;
|
||||
mkdfs[to]=1;
|
||||
if(mkgx[to]==-1||dfs(mkgx[to]))
|
||||
{
|
||||
mkgx[to]=a;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int main ()
|
||||
{
|
||||
int G,B,n,i,j,k,t=1,a,b,ans;
|
||||
while(~scanf("%d%d%d",&G,&B,&k)&&G+B+k)
|
||||
{
|
||||
for(i=1;i<=200;i++)
|
||||
for(j=1;j<=200;j++)
|
||||
mkmp[i][j]=1;
|
||||
for(i=1;i<=k;i++)
|
||||
{
|
||||
scanf("%d%d",&a,&b);
|
||||
mkmp[a][b]=0;
|
||||
}
|
||||
memset(list,0,sizeof(list));
|
||||
tot=0;
|
||||
for(i=1;i<=G;i++)
|
||||
for(j=1;j<=B;j++)
|
||||
{
|
||||
if(mkmp[i][j])
|
||||
add(i,j);
|
||||
}
|
||||
memset(mkgx,255,sizeof(mkgx));
|
||||
ans=0;
|
||||
for(i=1;i<=G;i++)
|
||||
{
|
||||
memset(mkdfs,0,sizeof(mkgx));
|
||||
ans+=dfs(i);
|
||||
}
|
||||
printf("Case %d: %d\n",t++,G+B-ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,145 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<iostream>
|
||||
#include<cmath>
|
||||
using namespace std;
|
||||
#define N 100005
|
||||
int ws1[N],wv[N],wa[N],wb[N];
|
||||
int ran[N],height[N],sa[N],len;
|
||||
char str[N],xiao;
|
||||
int dp[N][25];
|
||||
int min(int x,int y)
|
||||
{
|
||||
return x<y?x:y;
|
||||
}
|
||||
int cmp(int *r,int a,int b,int l)
|
||||
{
|
||||
return r[a]==r[b] && r[a+l]==r[b+l];
|
||||
}
|
||||
void da(char *r,int *sa,int n,int m)
|
||||
{
|
||||
int i,j,p,*x=wa,*y=wb,*t;
|
||||
for(i=0;i<m;i++)
|
||||
ws1[i]=0;
|
||||
for(i=0;i<n;i++)
|
||||
ws1[x[i]=r[i]]++;
|
||||
for(i=1;i<m;i++)
|
||||
ws1[i]+=ws1[i-1];
|
||||
for(i=n-1;i>=0;i--)
|
||||
sa[--ws1[x[i]]]=i;
|
||||
for(j=1,p=1;p<n;j*=2,m=p)
|
||||
{
|
||||
for(p=0,i=n-j;i<n;i++)
|
||||
y[p++]=i;
|
||||
for(i=0;i<n;i++)
|
||||
if(sa[i]>=j)
|
||||
y[p++]=sa[i]-j;
|
||||
for(i=0;i<n;i++)
|
||||
wv[i]=x[y[i]];
|
||||
for(i=0;i<m;i++)
|
||||
ws1[i]=0;
|
||||
for(i=0;i<n;i++)
|
||||
ws1[wv[i]]++;
|
||||
for(i=1;i<m;i++)
|
||||
ws1[i]+=ws1[i-1];
|
||||
for(i=n-1;i>=0;i--)
|
||||
sa[--ws1[wv[i]]]=y[i];
|
||||
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
|
||||
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
|
||||
}
|
||||
}
|
||||
void calheight(char *r,int *sa,int n)
|
||||
{
|
||||
int i,j,k=0;
|
||||
for(i=1;i<=n;i++)
|
||||
ran[sa[i]]=i;
|
||||
for(i=0;i<n;height[ran[i++]]=k)
|
||||
for(k?k--:0,j=sa[ran[i]-1];r[i+k]==r[j+k];k++) ;
|
||||
}
|
||||
void RMQ()
|
||||
{
|
||||
int i,j,m;
|
||||
m=(int)(log((double)len)/log(2.00));
|
||||
for(i=1;i<=len;i++)
|
||||
dp[i][0]=height[i];
|
||||
for(j=1;j<=m;j++)
|
||||
for(i=1;i+(1<<j)-1<=len;i++)
|
||||
dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
|
||||
}
|
||||
int lcp(int x,int y)
|
||||
{
|
||||
int t;
|
||||
x=ran[x];y=ran[y];
|
||||
if(x>y)
|
||||
swap(x,y);
|
||||
x++;
|
||||
t=(int)(log(double(y-x+1))/log(2.00));
|
||||
return min(dp[x][t],dp[y-(1<<t)+1][t]);
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int i,j,max=1,f=0,l1,num=0,t,node=1,k,cnt,p;
|
||||
for(i=1;i<=len/2;i++)
|
||||
{
|
||||
for(j=0;j+i<len;j+=i)
|
||||
{
|
||||
if(str[j]!=str[j+i])
|
||||
continue;
|
||||
l1=lcp(j,j+i);
|
||||
num=l1/i+1;
|
||||
p=j;
|
||||
t=i-l1%i;
|
||||
cnt=0;
|
||||
for(k=j-1;k>=0&&k+i>j&&str[k]==str[k+i];k--)
|
||||
{
|
||||
cnt++;
|
||||
if(cnt==t)
|
||||
{
|
||||
num++;
|
||||
p=k;
|
||||
}
|
||||
else if(ran[k]<ran[p])
|
||||
p=k;
|
||||
}
|
||||
if(max<num)
|
||||
{
|
||||
f=p;
|
||||
max=num;
|
||||
node=i;
|
||||
}
|
||||
else if(max==num&&ran[f]>ran[p])
|
||||
{
|
||||
f=p;
|
||||
node=i;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(max==1)
|
||||
{
|
||||
printf("%c\n",xiao);
|
||||
return ;
|
||||
}
|
||||
for(i=f;i<=f+max*node-1;i++)
|
||||
printf("%c",str[i]);
|
||||
printf("\n");
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T=0,i;
|
||||
while(scanf("%s",str)!=EOF&&str[0]!='#')
|
||||
{
|
||||
T++;
|
||||
len=strlen(str);
|
||||
xiao='z'+1;
|
||||
for(i=0;i<len;i++)
|
||||
if(str[i]<xiao)
|
||||
xiao=str[i];
|
||||
str[len]='0';
|
||||
da(str,sa,len+1,'z'+1);
|
||||
calheight(str,sa,len);
|
||||
RMQ();
|
||||
printf("Case %d: ",T);
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
#define MAXN 111111
|
||||
#pragma comment(linker,"/STACk:10240000,10240000")
|
||||
struct Edge{
|
||||
int v,next;
|
||||
}edge[MAXN*4];
|
||||
int n,m,cnt,NE,BridgeNum;
|
||||
int head[MAXN];
|
||||
int parent[MAXN];
|
||||
int isbridge[MAXN];
|
||||
int low[MAXN],dfn[MAXN];
|
||||
bool mark[MAXN];
|
||||
void Insert(int u,int v)
|
||||
{
|
||||
edge[NE].v=v;
|
||||
edge[NE].next=head[u];
|
||||
head[u]=NE++;
|
||||
}
|
||||
void Tarjan(int u,int father)
|
||||
{
|
||||
int flag=0;
|
||||
low[u]=dfn[u]=++cnt;
|
||||
mark[u]=true;
|
||||
for(int i=head[u];i!=-1;i=edge[i].next){
|
||||
int v=edge[i].v;
|
||||
if(v==father&&!flag){ flag=1;continue; }
|
||||
if(dfn[v]==0){
|
||||
parent[v]=u;
|
||||
Tarjan(v,u);
|
||||
low[u]=min(low[u],low[v]);
|
||||
if(low[v]>dfn[u]){
|
||||
isbridge[v]=1;
|
||||
BridgeNum++;
|
||||
}
|
||||
}else if(mark[v]){
|
||||
low[u]=min(low[u],dfn[v]);
|
||||
}
|
||||
}
|
||||
}
|
||||
void LCA(int u,int v)
|
||||
{
|
||||
while(dfn[u]>dfn[v]){
|
||||
if(isbridge[u]){
|
||||
BridgeNum--;
|
||||
isbridge[u]=0;
|
||||
}
|
||||
u=parent[u];
|
||||
}
|
||||
while(dfn[v]>dfn[u]){
|
||||
if(isbridge[v]){
|
||||
BridgeNum--;
|
||||
isbridge[v]=0;
|
||||
}
|
||||
v=parent[v];
|
||||
}
|
||||
while(u!=v){
|
||||
if(isbridge[u]){ BridgeNum--;isbridge[u]=0; }
|
||||
if(isbridge[v]){ BridgeNum--;isbridge[v]=0; }
|
||||
u=parent[u],v=parent[v];
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int u,v,Q,_case=1;
|
||||
while(~scanf("%d%d",&n,&m)){
|
||||
if(n==0&&m==0)break;
|
||||
BridgeNum=NE=cnt=0;
|
||||
memset(head,-1,sizeof(head));
|
||||
while(m--){
|
||||
scanf("%d%d",&u,&v);
|
||||
Insert(u,v);
|
||||
Insert(v,u);
|
||||
}
|
||||
memset(isbridge,0,sizeof(isbridge));
|
||||
memset(dfn,0,sizeof(dfn));
|
||||
memset(mark,false,sizeof(mark));
|
||||
for(int i=1;i<=n+1;i++)parent[i]=i;
|
||||
Tarjan(1,-1);
|
||||
printf("Case %d:\n",_case++);
|
||||
scanf("%d",&Q);
|
||||
while(Q--){
|
||||
scanf("%d%d",&u,&v);
|
||||
LCA(u,v);
|
||||
printf("%d\n",BridgeNum);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
#include<cstdio>
|
||||
#include<cmath>
|
||||
#include<algorithm>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
struct Rectangle { int x1, y1, x2, y2;};
|
||||
Rectangle rec[30];
|
||||
int a[30], cnt;
|
||||
int s[1100000];
|
||||
int n, m;
|
||||
Rectangle Intersection ( Rectangle r1, Rectangle r2 )
|
||||
{
|
||||
Rectangle ret;
|
||||
if ( r1.x2 <= r2.x1 || r2.x2 <= r1.x1 || r1.y2 <= r2.y1 || r2.y2 <= r1.y1 )
|
||||
{
|
||||
ret.x1 = ret.y1 = ret.y1 = ret.y2 = 0;
|
||||
return ret;
|
||||
}
|
||||
ret.x1 = max ( r1.x1, r2.x1 );
|
||||
ret.y1 = max ( r1.y1, r2.y1 );
|
||||
ret.x2 = min ( r1.x2, r2.x2 );
|
||||
ret.y2 = min ( r1.y2, r2.y2 );
|
||||
return ret;
|
||||
}
|
||||
int Area ( Rectangle r )
|
||||
{
|
||||
if ( r.x1 >= r.x2 || r.y1 >= r.y2 )
|
||||
return 0;
|
||||
return (r.y2-r.y1) * (r.x2-r.x1);
|
||||
}
|
||||
int In_Exclusion ( int k, Rectangle r )
|
||||
{
|
||||
if ( Area(r) == 0 ) return 0;
|
||||
int ret = 0;
|
||||
Rectangle tmp;
|
||||
for ( int i = k; i < cnt; i++ )
|
||||
{
|
||||
tmp = Intersection(r,rec[a[i]]);
|
||||
ret += Area(tmp) - In_Exclusion ( i + 1, tmp );
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t = 0;
|
||||
Rectangle total;
|
||||
total.x1 = total.y1 = 0;
|
||||
total.x2 = total.y2 = 1000;
|
||||
while (scanf("%d%d",&n,&m) && (m||n))
|
||||
{
|
||||
int i, R, id, add;
|
||||
for ( i = 1; i <= n; i++ )
|
||||
scanf("%d%d%d%d",&rec[i].x1,&rec[i].y1,&rec[i].x2,&rec[i].y2);
|
||||
printf("Case %d:\n",++t);
|
||||
memset(s,0,sizeof(s));
|
||||
for ( i = 1; i <= m; i++ )
|
||||
{
|
||||
scanf("%d",&R);
|
||||
cnt = add = 0;
|
||||
while ( R-- )
|
||||
{
|
||||
scanf("%d",&id);
|
||||
if ( add & (1<<(id-1)) ) continue;
|
||||
add = (add | (1<<(id-1)));
|
||||
a[cnt++] = id;
|
||||
}
|
||||
if ( s[add] == 0 )
|
||||
s[add] = In_Exclusion (0,total);
|
||||
printf("Query %d: %d\n",i,s[add]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
#include <vector>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#define LL long long
|
||||
#define MAXN 400010
|
||||
using namespace std;
|
||||
bool vis[MAXN];
|
||||
vector<LL> hav;
|
||||
vector<int> prime;
|
||||
LL gcd(LL a, LL b) {
|
||||
return b==0 ? a : gcd(b, a%b);
|
||||
}
|
||||
void gen_primes() {
|
||||
for(int i=2; i<MAXN; ++i) {
|
||||
if(!vis[i]) {
|
||||
prime.push_back(i);
|
||||
if(i < 1111) {
|
||||
for(int j=i*i; j<MAXN; j+=i) {
|
||||
vis[j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ;
|
||||
}
|
||||
LL euler_phi(LL n) {
|
||||
LL ans = n;
|
||||
for(int i=0; (LL)(prime[i]*prime[i])<=n; ++i) {
|
||||
if(n%prime[i] == 0) {
|
||||
ans = ans/prime[i]*(prime[i]-1);
|
||||
n /= prime[i];
|
||||
while(n%prime[i] == 0)
|
||||
n /= prime[i];
|
||||
}
|
||||
}
|
||||
if(n > 1) {
|
||||
ans = ans/n*(n-1);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
LL Mul(LL a, LL b, LL c) {
|
||||
LL ans = 0;
|
||||
while(b) {
|
||||
if(b & 1)
|
||||
ans = (ans+a)%c;
|
||||
a = a*2%c;
|
||||
b >>= 1;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
LL Pow(LL a, LL b, LL c) {
|
||||
LL ans = 1;
|
||||
while(b) {
|
||||
if(b & 1)
|
||||
ans = Mul(ans, a, c);
|
||||
a = Mul(a, a, c);
|
||||
b >>= 1;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
void get_hav(LL n) {
|
||||
hav.clear();
|
||||
for(int i=0; i<prime.size()&&n>1; ++i) {
|
||||
while(n%(LL)prime[i] == 0) {
|
||||
n /= prime[i];
|
||||
hav.push_back(prime[i]);
|
||||
}
|
||||
}
|
||||
if(n > 1)
|
||||
hav.push_back(n);
|
||||
}
|
||||
int main(void) {
|
||||
LL n, m, x, cas = 1;
|
||||
gen_primes();
|
||||
while(cin >> n && n) {
|
||||
m = 9*n/gcd(n, 8LL);
|
||||
if(gcd(m, 10LL) != 1) {
|
||||
cout << "Case " << cas++ << ": 0" << endl;
|
||||
continue;
|
||||
}
|
||||
x = euler_phi(m);
|
||||
get_hav(x);
|
||||
for(int i=0; i<hav.size(); ++i) {
|
||||
if(Pow(10LL, x/hav[i], m) == 1)
|
||||
x /= hav[i];
|
||||
}
|
||||
cout << "Case " << cas++ << ": " << x << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <queue>
|
||||
#define maxn 10010
|
||||
using namespace std;
|
||||
int n,m,a,b;
|
||||
int pre[maxn];
|
||||
vector <int> map[maxn];
|
||||
bool vis[maxn];
|
||||
int solve(){
|
||||
queue <int> q;
|
||||
memset(vis,0,sizeof(vis));
|
||||
memset(pre,0,sizeof(pre));
|
||||
vis[1] = 1;
|
||||
q.push(1);
|
||||
int ans = 0;
|
||||
while(!q.empty()){
|
||||
int now = q.front();
|
||||
q.pop();
|
||||
for(int i = 0; i < map[now].size(); i ++)
|
||||
pre[map[now][i]] = now;
|
||||
for(int i = 1; i <= n;i ++)
|
||||
if(!vis[i] && pre[i] != now)
|
||||
vis[i] = true, q.push(i),ans ++;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main(){
|
||||
int ca = 1;
|
||||
while(~scanf("%d%d",&n,&m) && (n || m)){
|
||||
for(int i = 1; i <= n; i ++)
|
||||
map[i].clear();
|
||||
for(int i = 0; i < m; i ++){
|
||||
scanf("%d%d",&a,&b);
|
||||
map[a].push_back(b);
|
||||
map[b].push_back(a);
|
||||
}
|
||||
printf("Case %d: %d\n",ca ++ ,solve());
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
int A[10][10],B[10][10];
|
||||
int vary[10];
|
||||
bool flag[10];
|
||||
int small1,small2,min;
|
||||
void action(int n)
|
||||
{
|
||||
int i,j,val=0;
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
{
|
||||
if(A[vary[i]][vary[j]]==B[i][j]) continue;
|
||||
if(A[vary[i]][vary[j]])
|
||||
val+=small2;
|
||||
else
|
||||
val+=small1;
|
||||
}
|
||||
}
|
||||
if(val<min) min=val;
|
||||
}
|
||||
void DFS(int pos,int n)
|
||||
{
|
||||
int i;
|
||||
if(pos==n) {action(n); return ; }
|
||||
for(i=0;i<n;i++)
|
||||
{
|
||||
if(flag[i])
|
||||
{
|
||||
vary[pos]=i;
|
||||
flag[i]=false;
|
||||
DFS(pos+1,n);
|
||||
flag[i]=true;
|
||||
}
|
||||
}
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int n,Ma,Mb,Ia,Ib,Da,Db;
|
||||
int i,a,b,cas=0;
|
||||
while(scanf("%d %d %d",&n,&Ma,&Mb) && n)
|
||||
{
|
||||
scanf("%d %d %d %d",&Ia,&Ib,&Da,&Db);
|
||||
small1=(Ia<Db?Ia:Db);
|
||||
small2=(Ib<Da?Ib:Da);
|
||||
min=0xfffffff;
|
||||
memset(A,0,sizeof(A));
|
||||
memset(B,0,sizeof(B));
|
||||
for(i=0;i<Ma;i++)
|
||||
{
|
||||
scanf("%d %d",&a,&b);
|
||||
A[a][b]=A[b][a]=1;
|
||||
}
|
||||
for(i=0;i<Mb;i++)
|
||||
{
|
||||
scanf("%d %d",&a,&b);
|
||||
B[a][b]=B[b][a]=1;
|
||||
}
|
||||
for(i=0;i<n;i++) flag[i]=true;
|
||||
DFS(0,n);
|
||||
printf("Case #%d: %d\n",++cas,min/2);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
solve();
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int h[]={0,3,2,1,2,3,2,3,4,3,4,5,4,5,6,5,6,7,6,7,8};
|
||||
int p[2000000];
|
||||
int a[100];
|
||||
int bound,i,j,n,m,s,t;
|
||||
bool flag;
|
||||
int count(int x){
|
||||
x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
|
||||
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
|
||||
x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
|
||||
x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF);
|
||||
x = (x & 0x0000FFFF) + ((x >> 16) & 0x0000FFFF);
|
||||
return x;
|
||||
}
|
||||
int f(int x){
|
||||
return h[p[x]];
|
||||
}
|
||||
void IDA(int d,int x,int tmp){
|
||||
if(d==bound){
|
||||
if(tmp==t){
|
||||
flag=true;
|
||||
}
|
||||
return ;
|
||||
}
|
||||
if(f(tmp^t)+d>bound||(m-x)+d<bound) return ;
|
||||
for(int i=x+1;i<=m;i++){
|
||||
IDA(d+1,i,tmp^a[i]);
|
||||
if(flag) return ;
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
// freopen("e:\\in.txt","r",stdin);
|
||||
for(i=0;i<=1100000;i++)
|
||||
p[i]=count(i);
|
||||
int cas=0;
|
||||
while(1){
|
||||
cas++;
|
||||
scanf("%d%d",&n,&m);
|
||||
scanf("%d%d",&s,&t);
|
||||
if(!n&&!m) break;
|
||||
for(i=1;i<=m;i++)
|
||||
scanf("%d",&a[i]);
|
||||
flag=false;
|
||||
for(bound=0;bound<=m;bound++){
|
||||
IDA(0,0,s);
|
||||
if(flag)
|
||||
break;
|
||||
}
|
||||
printf("Case #%d: ",cas);
|
||||
if(!flag) printf("Impossible\n");
|
||||
else printf("%d\n",bound);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<string.h>
|
||||
#include<math.h>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int maxn = 5111;
|
||||
const double eps = 1e-12;
|
||||
const double pi = acos(-1.0);
|
||||
typedef struct{double x, y;}Point;
|
||||
int dcmp(double x) {return (x > eps) - (x < -eps);}
|
||||
inline double det(double x1, double y1, double x2, double y2)
|
||||
{return x1 * y2 - x2 * y1;}
|
||||
double cross(Point a, Point b, Point c)
|
||||
{return det(b.x -a.x, b.y - a.y, c.x - a.x, c.y - a.y);}
|
||||
bool EqualPoint(Point a, Point b)
|
||||
{return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;}
|
||||
typedef struct{Point s, e;double ang, d;}Line;
|
||||
Point MakePoint(double xx, double yy)
|
||||
{Point res;res.x = xx, res.y = yy;return res;}
|
||||
void SetLine(Point a, Point b, Line &l)
|
||||
{
|
||||
l.s = a;
|
||||
l.e = b;
|
||||
l.ang = atan2(b.y - a.y, b.x - a.x);
|
||||
if(dcmp(a.x - b.x)) l.d = (a.x * b.y - b.x * a.y) / fabs(a.x - b.x);
|
||||
else l.d = (a.x * b.y - b.x * a.y) / fabs(a.y - b.y);
|
||||
}
|
||||
bool Parallel(const Line &la, const Line &lb)
|
||||
{
|
||||
return !dcmp( (la.e.x - la.s.x) * (lb.e.y - lb.s.y) -
|
||||
(la.e.y - la.s.y) * (lb.e.x - lb.s.x) );
|
||||
}
|
||||
Point CrossPoint(const Line &la, const Line &lb)
|
||||
{
|
||||
Point res;
|
||||
double u = cross(la.s, la.e, lb.s), v = cross(la.e, la.s, lb.e);
|
||||
res.x = (lb.s.x * v + lb.e.x * u) / (u + v);
|
||||
res.y = (lb.s.y * v + lb.e.y * u) / (u + v);
|
||||
return res;
|
||||
}
|
||||
int CompL(const void *a, const void *b)
|
||||
{
|
||||
Line la = *(Line*)a, lb = *(Line*)b;
|
||||
if(dcmp(la.ang - lb.ang)) return la.ang > lb.ang ? 1 : -1;
|
||||
return la.d > lb.d ? 1 : -1;
|
||||
}
|
||||
Line deq[maxn];
|
||||
bool HalfPanelCross(Line l[], int n, Point cp[], int &m)
|
||||
{
|
||||
int i, tn;
|
||||
m = 0;
|
||||
qsort(l, n, sizeof(Line), CompL);
|
||||
for(i = tn = 1; i < n; ++ i)
|
||||
if(dcmp(l[i].ang - l[i - 1].ang))
|
||||
l[tn ++] = l[i];
|
||||
n = tn;
|
||||
int front = 0, rear = 1;
|
||||
deq[0] = l[0], deq[1] = l[1];
|
||||
for(i = 2; i < n; ++ i)
|
||||
{
|
||||
if(Parallel(deq[rear], deq[rear - 1]) ||
|
||||
Parallel(deq[front], deq[front + 1]))
|
||||
return false;
|
||||
while(front < rear && dcmp( cross(l[i].s, l[i].e,
|
||||
CrossPoint(deq[rear], deq[rear - 1])) ) < 0) -- rear;
|
||||
while(front < rear && dcmp( cross(l[i].s, l[i].e,
|
||||
CrossPoint(deq[front], deq[front + 1])) ) < 0) ++ front;
|
||||
deq[++ rear] = l[i];
|
||||
}
|
||||
while(front < rear && dcmp( cross(deq[front].s, deq[front].e,
|
||||
CrossPoint(deq[rear], deq[rear - 1])) ) < 0) -- rear;
|
||||
while(front < rear && dcmp( cross(deq[rear].s, deq[rear].e,
|
||||
CrossPoint(deq[front], deq[front + 1])) ) < 0) ++ front;
|
||||
if(rear <= front + 1) return false;
|
||||
for(i = front; i < rear; ++ i)
|
||||
cp[m ++] = CrossPoint(deq[i], deq[i + 1]);
|
||||
if(front < rear + 1)
|
||||
cp[m ++] = CrossPoint(deq[front], deq[rear]);
|
||||
m = unique(cp, cp + m, EqualPoint) - cp;
|
||||
for(i = 0; i < m; ++ i)
|
||||
{
|
||||
if(dcmp(cp[i].x) == 0) cp[i].x = 0;
|
||||
if(dcmp(cp[i].y) == 0) cp[i].y = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
double PolygonArea(Point p[], int n)
|
||||
{
|
||||
if(n < 3) return 0.0;
|
||||
double s = p[0].y * (p[n - 1].x - p[1].x);
|
||||
p[n] = p[0];
|
||||
for(int i = 1; i < n; ++ i)
|
||||
s += p[i].y * (p[i - 1].x - p[i + 1].x);
|
||||
return fabs(s * 0.5);
|
||||
}
|
||||
Line l[maxn << 2];
|
||||
Point p[maxn << 2];
|
||||
double len;
|
||||
int n, m, ltp;
|
||||
double maxv, minv, ti;
|
||||
double start, en, v;
|
||||
int main()
|
||||
{
|
||||
int ca = 0;
|
||||
while(scanf("%lf", &len), dcmp(len))
|
||||
{
|
||||
ltp = 0;
|
||||
scanf("%d", &n);
|
||||
while(n --)
|
||||
{
|
||||
scanf("%lf%lf%lf", &minv, &maxv, &ti);
|
||||
SetLine(MakePoint(ti, 0), MakePoint(ti + 1, minv), l[ltp ++]);
|
||||
SetLine(MakePoint(ti + 1, maxv), MakePoint(ti, 0), l[ltp ++]);
|
||||
}
|
||||
scanf("%d", &m);
|
||||
while(m --)
|
||||
{
|
||||
scanf("%lf%lf%lf", &minv, &maxv, &ti);
|
||||
SetLine(MakePoint(ti + 1, len - minv), MakePoint(ti, len), l[ltp ++]);
|
||||
SetLine(MakePoint(ti, len), MakePoint(ti + 1, len - maxv), l[ltp ++]);
|
||||
}
|
||||
scanf("%lf%lf%lf", &start, &en, &v);
|
||||
printf("Case #%d: ", ++ ca);
|
||||
SetLine(MakePoint(0, 0), MakePoint(1, 0), l[ltp ++]);
|
||||
SetLine(MakePoint(1, len), MakePoint(0, len), l[ltp ++]);
|
||||
SetLine(MakePoint(start + 1, v), MakePoint(start, 0), l[ltp ++]);
|
||||
SetLine(MakePoint(en, 0), MakePoint(en + 1, v), l[ltp ++]);
|
||||
if(!HalfPanelCross(l, ltp, p, n)){printf("0.00000\n"); continue;}
|
||||
printf("%.5f\n", PolygonArea(p, n) / (en - start) / len + eps);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<cmath>
|
||||
const int MAXNUM=20000;
|
||||
const double PI=atan2(0.0,-1.0);
|
||||
using namespace std;
|
||||
int N,K;
|
||||
struct Point
|
||||
{
|
||||
int x,y;
|
||||
double angle,dist;
|
||||
}ps[MAXNUM];
|
||||
double minangle;
|
||||
bool found[MAXNUM];
|
||||
int cmp(Point a,Point b)
|
||||
{
|
||||
if(a.angle==b.angle)
|
||||
return a.dist<b.dist;
|
||||
else
|
||||
return a.angle<b.angle;
|
||||
}
|
||||
void calMin()
|
||||
{
|
||||
if(K==0||K==1)
|
||||
{
|
||||
minangle=0.00;
|
||||
return;
|
||||
}
|
||||
minangle=1e100;
|
||||
int end,scount,start;
|
||||
int size=N+N;
|
||||
double newm;
|
||||
for(int i=0;i<N;i++)
|
||||
{
|
||||
end=-1; scount=0;start=0;
|
||||
for(int j=0;j<size;j++)
|
||||
if(ps[j].dist<=ps[i].dist)
|
||||
{
|
||||
scount++;
|
||||
if(scount==K)
|
||||
{
|
||||
end=j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(end!=-1)
|
||||
{
|
||||
scount--;end--;
|
||||
for(int q=end+1;q<size;q++)
|
||||
{
|
||||
if(ps[q].dist<=ps[i].dist)
|
||||
{
|
||||
scount++;
|
||||
while(ps[start].dist>ps[i].dist)start++;
|
||||
if(scount>K)
|
||||
{
|
||||
scount--;
|
||||
start++;
|
||||
while(ps[start].dist>ps[i].dist)start++;
|
||||
}
|
||||
}
|
||||
if((q-start+1)<=N)
|
||||
{
|
||||
newm=(ps[q].angle-ps[start].angle)*ps[i].dist;
|
||||
if(newm<minangle)
|
||||
minangle=newm;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int casenum=1;
|
||||
scanf("%d%d",&N,&K);
|
||||
while(N||K)
|
||||
{
|
||||
for(int i=0;i<N;i++)
|
||||
{
|
||||
scanf("%d%d",&ps[i].x,&ps[i].y);
|
||||
ps[i].angle=atan2((double)(ps[i].y),(double)(ps[i].x));
|
||||
ps[i].dist=ps[i].x*ps[i].x+ps[i].y*ps[i].y;
|
||||
}
|
||||
sort(ps,ps+N,cmp);
|
||||
for(int j=0;j<N;j++)
|
||||
{
|
||||
ps[N+j]=ps[j];
|
||||
ps[N+j].angle+=PI+PI;
|
||||
}
|
||||
calMin();
|
||||
printf("Case #%d: %.2lf\n",casenum++,minangle/2.0);
|
||||
scanf("%d%d",&N,&K);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
#include<iostream>
|
||||
#include<string.h>
|
||||
#include<stdio.h>
|
||||
#include<ctype.h>
|
||||
#include<algorithm>
|
||||
#include<stack>
|
||||
#include<queue>
|
||||
#include<set>
|
||||
#include<math.h>
|
||||
#include<vector>
|
||||
#include<deque>
|
||||
#include<list>
|
||||
#define INF 0x7fffffff
|
||||
#define inf 0x3f3f3f3f
|
||||
#define maxn 130
|
||||
using namespace std;
|
||||
bool map[maxn][maxn];
|
||||
int yes[maxn],no[maxn],mark[maxn];
|
||||
int n,m,a,b;
|
||||
bool check(int x)
|
||||
{
|
||||
if(yes[x]>=a&&no[x]>=b)
|
||||
return 1;
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
if(map[x][i])
|
||||
yes[i]--;
|
||||
else
|
||||
no[i]--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int x,y;
|
||||
int ww=1;
|
||||
while(scanf("%d%d%d%d",&n,&m,&a,&b)&&(n||m||a||b))
|
||||
{
|
||||
memset(map,0,sizeof(map));
|
||||
memset(yes,0,sizeof(yes));
|
||||
memset(no,0,sizeof(no));
|
||||
memset(mark,0,sizeof(mark));
|
||||
for(int i=0; i<m; i++)
|
||||
{
|
||||
scanf("%d%d",&x,&y);
|
||||
yes[x]++;
|
||||
yes[y]++;
|
||||
map[x][y]=map[y][x]=1;
|
||||
}
|
||||
for(int i=0; i<n; i++)
|
||||
no[i]=n-yes[i]-1;
|
||||
bool flag=1;
|
||||
while(flag)
|
||||
{
|
||||
flag=0;
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
if(mark[i])
|
||||
continue;
|
||||
if(!check(i))
|
||||
{
|
||||
mark[i]=1;
|
||||
flag=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
int c=0;
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
if(!mark[i])
|
||||
c++;
|
||||
}
|
||||
printf("Case #%d: %d\n",ww++,c);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
#include <queue>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
typedef pair <int, int> pt;
|
||||
const int MAX_T = 26, MAX_N = 2000, SEED = 99991;
|
||||
int n[2], t;
|
||||
bool vis[MAX_N][MAX_N], can_use[MAX_N], cal[MAX_N];
|
||||
queue <pt> q;
|
||||
struct node_t {
|
||||
int son[MAX_T], end;
|
||||
int mask;
|
||||
int calc_mask() {
|
||||
mask = 0;
|
||||
for (int i = 0; i < t; i ++)
|
||||
if (son[i] >= 0) mask |= 1 << i;
|
||||
mask = mask << 1 | end;
|
||||
return mask;
|
||||
}
|
||||
} dfa[2][MAX_N];
|
||||
bool dfs(int k, int u) {
|
||||
if (cal[u]) return can_use[u];
|
||||
cal[u] = 1;
|
||||
if (dfa[k][u].end) can_use[u] = 1;
|
||||
for (int i = 0; i < t; i ++)
|
||||
if (dfa[k][u].son[i] >= 0 && dfs(k, dfa[k][u].son[i])) can_use[u] = 1;
|
||||
return can_use[u];
|
||||
}
|
||||
void dfs_again(int k, int u) {
|
||||
if (cal[u]) return;
|
||||
cal[u] = 1;
|
||||
for (int i = 0; i < t; i ++)
|
||||
if (dfa[k][u].son[i] >= 0) {
|
||||
if (can_use[dfa[k][u].son[i]]) dfs(k, dfa[k][u].son[i]);
|
||||
else dfa[k][u].son[i] = -1;
|
||||
}
|
||||
}
|
||||
void init(int k) {
|
||||
memset(can_use, 0, sizeof(can_use));
|
||||
memset(cal, 0, sizeof(cal));
|
||||
dfs(k, 0);
|
||||
memset(cal, 0, sizeof(cal));
|
||||
dfs_again(k, 0);
|
||||
}
|
||||
bool solve() {
|
||||
memset(vis, 0, sizeof(vis));
|
||||
while (q.size()) q.pop();
|
||||
q.push(make_pair(0, 0));
|
||||
vis[0][0] = 1;
|
||||
while (q.size()) {
|
||||
pt u = q.front();
|
||||
q.pop();
|
||||
if (dfa[0][u.first].mask != dfa[1][u.second].mask) return 0;
|
||||
for (int i = 0; i < t; i ++)
|
||||
if (dfa[0][u.first].son[i] >= 0) {
|
||||
pt v = make_pair(dfa[0][u.first].son[i], dfa[1][u.second].son[i]);
|
||||
if (!vis[v.first][v.second]) {
|
||||
vis[v.first][v.second] = 1;
|
||||
q.push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int main() {
|
||||
int cas = 0;
|
||||
while (scanf("%d", &t) != EOF && t) {
|
||||
for (int k = 0; k < 2; k ++) {
|
||||
scanf("%d", &n[k]);
|
||||
for (int i = 0; i < n[k]; i ++) {
|
||||
scanf("%d", &dfa[k][i].end);
|
||||
for (int j = 0; j < t; j ++) scanf("%d", &dfa[k][i].son[j]);
|
||||
}
|
||||
init(k);
|
||||
for (int i = 0; i < n[k]; i ++) dfa[k][i].calc_mask();
|
||||
}
|
||||
printf("Case #%d: ", ++ cas);
|
||||
if (solve()) printf("Yes\n");
|
||||
else printf("No\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<memory.h>
|
||||
struct node
|
||||
{
|
||||
int need;
|
||||
int id;
|
||||
};
|
||||
struct node que[4][51000];
|
||||
int hash[51000],head[4],own[4];
|
||||
int now[4][51000];
|
||||
void chu()
|
||||
{
|
||||
memset(hash,0,sizeof(hash));
|
||||
}
|
||||
int cmp(const void *a,const void *b)
|
||||
{
|
||||
return (*(struct node*)a).need-(*(struct node*)b).need;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int flag,sum,n,m,i,f;
|
||||
while(scanf("%d%d",&n,&m)!=EOF)
|
||||
{
|
||||
chu();
|
||||
for(f=1;f<=m;f++)
|
||||
{
|
||||
head[f]=1;
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
que[f][i].id=i;
|
||||
scanf("%d",&now[f][i]);
|
||||
}
|
||||
}
|
||||
for(f=1;f<=m;f++)
|
||||
{
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
scanf("%d",&que[f][i].need);
|
||||
}
|
||||
}
|
||||
for(i=1;i<=m;i++)
|
||||
{
|
||||
scanf("%d",&own[i]);
|
||||
qsort(&que[i][1],n,sizeof(que[0][0]),cmp);
|
||||
}
|
||||
sum=0;
|
||||
flag=1;
|
||||
while(1 == flag)
|
||||
{
|
||||
flag=0;
|
||||
for(i=1;i<=m;i++)
|
||||
{
|
||||
for(;head[i]<=n;head[i]++)
|
||||
{
|
||||
if(own[i] < que[i][head[i]].need)
|
||||
{
|
||||
break;
|
||||
}
|
||||
flag=1;
|
||||
hash[que[i][head[i]].id]++;
|
||||
if(m == hash[que[i][head[i]].id])
|
||||
{
|
||||
sum++;
|
||||
for(f=1;f<=m;f++)
|
||||
{
|
||||
own[f]+=now[f][que[i][head[i]].id];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(n == sum)
|
||||
{
|
||||
printf("Yes\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("No\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,219 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<algorithm>
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
#define maxn 110000
|
||||
#define mem(a,b) memset(a,b,sizeof(a))
|
||||
#define root10 ch[ch[root][1]][0]
|
||||
#define root1 ch[root][1]
|
||||
#define root11 ch[ch[root][1]][1]
|
||||
#define lson ch[x][0]
|
||||
#define rson ch[x][1]
|
||||
int ch[maxn][2];
|
||||
int pre[maxn];
|
||||
int root,tot;
|
||||
int size[maxn];
|
||||
int val[maxn];
|
||||
//----------------------
|
||||
int l[maxn];
|
||||
int r[maxn];
|
||||
vector<int>vec[maxn];
|
||||
vector<int>ans;
|
||||
vector<int>tree;
|
||||
void push_down(int x)
|
||||
{
|
||||
}
|
||||
void push_up(int x)
|
||||
{
|
||||
}
|
||||
void rot(int x,int kind)
|
||||
{
|
||||
int y=pre[x];
|
||||
push_down(y);
|
||||
push_down(x);
|
||||
ch[y][!kind]=ch[x][kind];
|
||||
pre[ch[x][kind]]=y;
|
||||
if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
|
||||
pre[x]=pre[y];
|
||||
ch[x][kind]=y;
|
||||
pre[y]=x;
|
||||
push_up(y);
|
||||
push_up(x);
|
||||
}
|
||||
void splay(int x,int goal)
|
||||
{
|
||||
push_down(x);
|
||||
while(pre[x]!=goal)
|
||||
{
|
||||
if(pre[pre[x]]==goal)
|
||||
{
|
||||
push_down(pre[x]);
|
||||
push_down(x);
|
||||
rot(x,ch[pre[x]][0]==x);
|
||||
}
|
||||
else
|
||||
{
|
||||
int y=pre[x];
|
||||
push_down(pre[y]);
|
||||
push_down(y);
|
||||
push_down(x);
|
||||
int kind=ch[pre[y]][0]==y;
|
||||
if(ch[y][kind]==x)
|
||||
{
|
||||
rot(x,!kind);
|
||||
rot(x,kind);
|
||||
}
|
||||
else
|
||||
{
|
||||
rot(y,kind);
|
||||
rot(x,kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
push_up(x);
|
||||
if(goal==0)root=x;
|
||||
}
|
||||
void init()
|
||||
{
|
||||
root=tot=0;
|
||||
memset(ch,0,sizeof(ch));
|
||||
memset(pre,0,sizeof(pre));
|
||||
}
|
||||
void newnode(int &x,int k,int father)
|
||||
{
|
||||
x=++tot;
|
||||
pre[x]=father;
|
||||
size[x]=1;
|
||||
ch[x][0]=ch[x][1]=0;
|
||||
val[x]=k;
|
||||
if(k<0)r[-k]=x;
|
||||
else l[k]=x;
|
||||
}
|
||||
void buildtree(int &x,int l,int r,int father)
|
||||
{
|
||||
if(l>r)return;
|
||||
int mid=(l+r)/2;
|
||||
newnode(x,ans[mid],father);
|
||||
buildtree(ch[x][0],l,mid-1,x);
|
||||
buildtree(ch[x][1],mid+1,r,x);
|
||||
push_up(x);
|
||||
}
|
||||
int get_max(int r)
|
||||
{
|
||||
push_down(r);
|
||||
while(ch[r][1])
|
||||
{
|
||||
r=ch[r][1];
|
||||
push_down(r);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
int get_min(int r)
|
||||
{
|
||||
push_down(r);
|
||||
while(ch[r][0])
|
||||
{
|
||||
r=ch[r][0];
|
||||
push_down(r);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
int query(int x)
|
||||
{
|
||||
int ll=l[x];
|
||||
splay(ll,0);
|
||||
int y=get_min(ll);
|
||||
return val[y];
|
||||
}
|
||||
void dos(int a,int b)
|
||||
{
|
||||
int x,y,z;
|
||||
x=l[a];y=r[a];z=y;
|
||||
splay(x,0);
|
||||
splay(y,0);
|
||||
int ll,rr,conn;
|
||||
ll=ch[x][0];
|
||||
rr=ch[y][1];
|
||||
conn=get_max(ll);
|
||||
pre[ll]=pre[rr]=0;
|
||||
ch[x][0]=ch[y][1]=0;
|
||||
if(conn!=0)ch[conn][1]=rr;
|
||||
pre[rr]=conn;
|
||||
if(b==0)return;
|
||||
if(query(b)==a)
|
||||
{
|
||||
ch[conn][1]=0;pre[rr]=0;
|
||||
ch[x][0]=ll;ch[y][1]=rr;
|
||||
pre[ll]=x;
|
||||
pre[rr]=y;
|
||||
pre[0]=0;
|
||||
return;
|
||||
}
|
||||
if(rr!=0)splay(rr,0);
|
||||
x=l[b];
|
||||
splay(x,0);
|
||||
y=get_min(ch[x][1]);
|
||||
splay(y,x);
|
||||
ch[y][0]=z;pre[z]=y;
|
||||
}
|
||||
void dfs(int x)
|
||||
{
|
||||
ans.push_back(x);
|
||||
for(int i=0; i<vec[x].size(); i++)
|
||||
{
|
||||
dfs(vec[x][i]);
|
||||
}
|
||||
ans.push_back(-x);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n,x;
|
||||
int cas=0;
|
||||
while(~scanf("%d",&n))
|
||||
{
|
||||
if(cas)puts("");
|
||||
cas++;
|
||||
init();
|
||||
for(int i=0; i<=n; i++)vec[i].clear();
|
||||
ans.clear();
|
||||
for(int i=1; i<=n; i++)
|
||||
{
|
||||
scanf("%d",&x);
|
||||
vec[x].push_back(i);
|
||||
}
|
||||
dfs(0);
|
||||
int len=0;
|
||||
int st=1;
|
||||
for(int i=1; i<ans.size()-1; i++)
|
||||
{
|
||||
if(ans[i]>0)len++;
|
||||
else len--;
|
||||
if(len==0)
|
||||
{
|
||||
buildtree(root,st,i,0);
|
||||
st=i+1;
|
||||
}
|
||||
}
|
||||
int m,x,y;
|
||||
char str[1110];
|
||||
scanf("%d",&m);
|
||||
while(m--)
|
||||
{
|
||||
cas++;
|
||||
scanf("%s",str);
|
||||
if(str[0]=='M')
|
||||
{
|
||||
scanf("%d%d",&x,&y);
|
||||
dos(x,y);
|
||||
}
|
||||
if(str[0]=='Q')
|
||||
{
|
||||
scanf("%d",&x);
|
||||
printf("%d\n",query(x));
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
char s1[105],s2[105];
|
||||
int dp[105][105];
|
||||
int ans[105],i,j,k,len;
|
||||
int main()
|
||||
{
|
||||
while(~scanf("%s%s",s1,s2))
|
||||
{
|
||||
len = strlen(s1);
|
||||
memset(dp,0,sizeof(dp));
|
||||
for(j = 0; j<len; j++)
|
||||
{
|
||||
for(i = j; i>=0; i--)
|
||||
{
|
||||
dp[i][j] = dp[i+1][j]+1;
|
||||
for(k = i+1; k<=j; k++)
|
||||
{
|
||||
if(s2[i]==s2[k])
|
||||
dp[i][j] = min(dp[i][j],(dp[i+1][k]+dp[k+1][j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
for(i = 0; i<len; i++)
|
||||
ans[i] = dp[0][i];
|
||||
for(i = 0; i<len; i++)
|
||||
{
|
||||
if(s1[i] == s2[i])
|
||||
ans[i] = ans[i-1];
|
||||
else
|
||||
{
|
||||
for(j = 0; j<i; j++)
|
||||
ans[i] = min(ans[i],ans[j]+dp[j+1][i]);
|
||||
}
|
||||
}
|
||||
printf("%d\n",ans[len-1]);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,188 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int maxn=50500;
|
||||
const double zero=1e-8;
|
||||
const double inf=1e10;
|
||||
int n,K,m,L,tot,root,ans;
|
||||
int E[maxn],pre[maxn],fst[maxn];
|
||||
int V[maxn],ft[maxn],pl[maxn],pr[maxn],f[maxn][110],g[maxn][110],h[maxn],q[maxn];
|
||||
struct Event
|
||||
{
|
||||
double x,y,r,px; int id,v;
|
||||
Event(){};
|
||||
Event(double _x,double _y,double _r,double _px,int i,int _v) {x=_x; y=_y; r=_r; px=_px; id=i; v=_v;};
|
||||
bool operator <(const Event &rhs)const
|
||||
{
|
||||
return px+zero<rhs.px;
|
||||
}
|
||||
double gety(double x0,int v0)
|
||||
{
|
||||
if(fabs(x0-x)+zero>=r) return y;
|
||||
double tmp=sqrt(r*r-(x-x0)*(x-x0));
|
||||
if(v0==0) return y-tmp; else return y+tmp;
|
||||
}
|
||||
}Et[2*maxn];
|
||||
struct Tree
|
||||
{
|
||||
int ft,lc,rc,x,v;
|
||||
void clear() {ft=lc=rc=x=v=0;}
|
||||
}tr[4*maxn];
|
||||
struct Splay
|
||||
{
|
||||
void New(int i)
|
||||
{
|
||||
tr[++tot].clear(); tr[tot].x=i; tr[tot].v=0; tr[tot].rc=tot+1; pl[Et[i].id]=tot;
|
||||
tr[++tot].clear(); tr[tot].x=i; tr[tot].v=1; tr[tot].ft=tot-1; pr[Et[i].id]=tot;
|
||||
}
|
||||
void rotate(int x)
|
||||
{
|
||||
int y=tr[x].ft,z=tr[y].ft;
|
||||
if(tr[z].lc==y) tr[z].lc=x; else tr[z].rc=x;
|
||||
tr[x].ft=z; tr[y].ft=x;
|
||||
if(tr[y].lc==x) tr[y].lc=tr[x].rc,tr[tr[x].rc].ft=y,tr[x].rc=y;
|
||||
else tr[y].rc=tr[x].lc,tr[tr[x].lc].ft=y,tr[x].lc=y;
|
||||
}
|
||||
void splay(int x,int p)
|
||||
{
|
||||
int fa=tr[p].ft;
|
||||
while(tr[x].ft!=fa)
|
||||
{
|
||||
int y=tr[x].ft,z=tr[y].ft;
|
||||
if(z==fa) {rotate(x); continue;}
|
||||
if((tr[y].lc==x)^(tr[z].lc==y)) rotate(x); else rotate(y);
|
||||
rotate(x);
|
||||
}
|
||||
if(p==root) root=x;
|
||||
}
|
||||
int select(int i)
|
||||
{
|
||||
int p=root,ql=p,qr=p; double x=Et[i].px,y=Et[i].y;
|
||||
while(p>0)
|
||||
{
|
||||
int id=tr[p].x,v=tr[p].v; double y0;
|
||||
if(id==0) y0=v?inf:-inf; else y0=Et[id].gety(x,v);
|
||||
if(y0+zero<y) ql=p,p=tr[p].rc; else qr=p,p=tr[p].lc;
|
||||
}
|
||||
splay(ql,root); splay(qr,tr[root].rc);
|
||||
return qr;
|
||||
}
|
||||
int getpre(int x)
|
||||
{
|
||||
splay(x,root);
|
||||
int p=tr[x].lc; while(tr[p].rc) p=tr[p].rc;
|
||||
return p;
|
||||
}
|
||||
int getnext(int x)
|
||||
{
|
||||
splay(x,root);
|
||||
int p=tr[x].rc; while(tr[p].lc) p=tr[p].lc;
|
||||
return p;
|
||||
}
|
||||
}Tr;
|
||||
int get()
|
||||
{
|
||||
int f=0,v=0; char ch;
|
||||
while(!isdigit(ch=getchar())) if(ch=='-') break;
|
||||
if(ch=='-') f=1; else v=ch-48;
|
||||
while(isdigit(ch=getchar())) v=v*10+ch-48;
|
||||
if(f) return -v; else return v;
|
||||
}
|
||||
void init()
|
||||
{
|
||||
m=0; int x,y,r;
|
||||
n=get(); K=get()+1;
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
x=get(); y=get(); r=get(); V[i]=get();
|
||||
Et[++m]=Event(x,y,r,x-r,i,1);
|
||||
Et[++m]=Event(x,y,r,x+r,i,0);
|
||||
}
|
||||
}
|
||||
void add(int a,int b)
|
||||
{
|
||||
E[++L]=b; pre[L]=fst[a]; fst[a]=L;
|
||||
}
|
||||
void build()
|
||||
{
|
||||
tot=L=0; root=1;
|
||||
memset(fst,0,sizeof(fst));
|
||||
tr[0].clear(); Tr.New(0);
|
||||
sort(Et+1,Et+m+1);
|
||||
for(int i=1;i<=m;i++)
|
||||
if(Et[i].v)
|
||||
{
|
||||
Tr.New(i);
|
||||
int k=Tr.select(i); tr[tot-1].ft=k; tr[k].lc=tot-1;
|
||||
ft[Et[i].id]=(tr[k].v)?Et[tr[k].x].id:ft[Et[tr[k].x].id];
|
||||
add(ft[Et[i].id],Et[i].id);
|
||||
}else
|
||||
{
|
||||
int a=Tr.getpre(pl[Et[i].id]),b=Tr.getnext(pr[Et[i].id]);
|
||||
Tr.splay(a,root); Tr.splay(b,tr[root].rc);
|
||||
tr[tr[b].lc].ft=0; tr[b].lc=0;
|
||||
}
|
||||
}
|
||||
void bfs()
|
||||
{
|
||||
int head=1,tail=2; q[head]=0;
|
||||
while(head<tail)
|
||||
{
|
||||
int u=q[head];
|
||||
for(int k=fst[u];k;k=pre[k]) q[tail++]=E[k];
|
||||
head++;
|
||||
}
|
||||
}
|
||||
void dp()
|
||||
{
|
||||
for(int i=n+1;i>1;i--)
|
||||
{
|
||||
int u=q[i]; h[u]=0;
|
||||
for(int j=1;j<=K;j++) f[u][j]=g[u][j]=V[u];
|
||||
for(int k=fst[u];k;k=pre[k])
|
||||
{
|
||||
for(int j=1;j<K;j++)
|
||||
h[u]=max(h[u],max(abs(f[E[k]][j]-g[u][K-j]),abs(g[E[k]][j]-f[u][K-j])));
|
||||
for(int j=2;j<K;j++)
|
||||
{
|
||||
f[u][j]=min(f[u][j],f[E[k]][j-1]);
|
||||
g[u][j]=max(g[u][j],g[E[k]][j-1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
h[0]=0; int k=fst[0];
|
||||
for(int j=2;j<=K;j++) f[0][j]=f[E[k]][j-1],g[0][j]=g[E[k]][j-1];
|
||||
for(k=pre[k];k;k=pre[k])
|
||||
{
|
||||
for(int j=1;j<K-1;j++)
|
||||
h[0]=max(h[0],max(abs(f[E[k]][j]-g[0][K-j]),abs(g[E[k]][j]-f[0][K-j])));
|
||||
for(int j=2;j<K;j++)
|
||||
{
|
||||
f[0][j]=min(f[0][j],f[E[k]][j-1]);
|
||||
g[0][j]=max(g[0][j],g[E[k]][j-1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
void work()
|
||||
{
|
||||
build(); bfs(); dp();
|
||||
ans=0;
|
||||
for(int i=0;i<=n;i++) ans=max(ans,h[i]);
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int TT=get();
|
||||
for(int i=1;i<=TT;i++)
|
||||
{
|
||||
printf("Case %d: ",i);
|
||||
init();
|
||||
work();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int N=100010;
|
||||
const int L=-1000001;
|
||||
const int R=1000001;
|
||||
int x1[N], x2[N], y1[N], y2[N];
|
||||
int main()
|
||||
{
|
||||
int tcase;
|
||||
while (~scanf("%d",&tcase))
|
||||
{
|
||||
while (tcase--)
|
||||
{
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
int lx = L, ly = L, rx = R, ry = R;
|
||||
int lx1 = L, ly1 = L, rx1 = R, ry1 = R;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%d%d%d%d", &x1[i], &y1[i], &x2[i], &y2[i]);
|
||||
if (x1[i] > lx)
|
||||
{
|
||||
lx1 = lx;
|
||||
lx = x1[i];
|
||||
}
|
||||
else if (x1[i] > lx1)
|
||||
lx1 = x1[i];
|
||||
if (y1[i] > ly)
|
||||
{
|
||||
ly1 = ly;
|
||||
ly = y1[i];
|
||||
}
|
||||
else if (y1[i] > ly1)
|
||||
ly1 = y1[i];
|
||||
if (x2[i] < rx)
|
||||
{
|
||||
rx1 = rx;
|
||||
rx = x2[i];
|
||||
}
|
||||
else if (x2[i] < rx1)
|
||||
rx1 = x2[i];
|
||||
if (y2[i] < ry)
|
||||
{
|
||||
ry1 = ry;
|
||||
ry = y2[i];
|
||||
}
|
||||
else if (y2[i] < ry1)
|
||||
ry1 = y2[i];
|
||||
}
|
||||
if (n==1)
|
||||
{
|
||||
printf("0\n");
|
||||
continue;
|
||||
}
|
||||
int ans = 0;
|
||||
int x,y;
|
||||
int xx,yy;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (x1[i] == lx)
|
||||
x = lx1;
|
||||
else
|
||||
x = lx;
|
||||
if (y1[i] == ly)
|
||||
y = ly1;
|
||||
else
|
||||
y = ly;
|
||||
if (x2[i] == rx)
|
||||
xx = rx1;
|
||||
else
|
||||
xx = rx;
|
||||
if (y2[i] == ry)
|
||||
yy = ry1;
|
||||
else
|
||||
yy = ry;
|
||||
if (yy > y && xx > x)
|
||||
{
|
||||
int tmp = (yy - y)*(xx - x);
|
||||
if (tmp > ans)
|
||||
ans = tmp;
|
||||
}
|
||||
}
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <memory.h>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int maxn = 1002;
|
||||
const int maxm = 500002;
|
||||
struct node
|
||||
{
|
||||
int u,v,w,d;
|
||||
bool operator < (const node &other) const
|
||||
{
|
||||
return w > other.w;
|
||||
}
|
||||
}edge[maxm];
|
||||
int father[maxn];
|
||||
bool vis[maxn];
|
||||
int m,n;
|
||||
int find(int u)
|
||||
{
|
||||
return (u == father[u]) ? father[u] : (father[u] = find(father[u]));
|
||||
}
|
||||
void read()
|
||||
{
|
||||
memset(vis,false,sizeof(vis));
|
||||
for(int i=1;i<=n;i++) father[i] = i;
|
||||
for(int i=0;i<m;i++)
|
||||
{
|
||||
scanf("%d %d %d %d",&edge[i].u,&edge[i].v,&edge[i].d,&edge[i].w);
|
||||
}
|
||||
sort(edge , edge + m);
|
||||
return;
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int sum = 0;
|
||||
for(int i=0;i<m;i++)
|
||||
{
|
||||
int x = find(edge[i].u);
|
||||
int y = find(edge[i].v);
|
||||
if(vis[x] && vis[y]) continue;
|
||||
if(edge[i].d == 1 && vis[x]) continue;
|
||||
sum += edge[i].w;
|
||||
if(edge[i].d == 1) vis[x] = true;
|
||||
else
|
||||
{
|
||||
if(x == y) vis[x] = true;
|
||||
else if(vis[x]) vis[y] = true;
|
||||
else if(vis[y]) vis[x] = true;
|
||||
else father[y] = x;
|
||||
}
|
||||
}
|
||||
printf("%d\n",sum);
|
||||
return;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(~scanf("%d %d",&n,&m))
|
||||
{
|
||||
read();
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,140 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#define MAXD 40010
|
||||
int N, isprime[MAXD], prime[MAXD], P, p[MAXD], pn;
|
||||
long long M;
|
||||
struct Matrix
|
||||
{
|
||||
long long a[4][4];
|
||||
void init()
|
||||
{
|
||||
memset(a, 0, sizeof(a));
|
||||
}
|
||||
}unit, mat[40];
|
||||
long long mul(long long x, long long y)
|
||||
{
|
||||
long long ans = 0;
|
||||
x %= M;
|
||||
while(y)
|
||||
{
|
||||
if((y & 1) && (ans += x) >= M)
|
||||
ans -= M;
|
||||
y >>= 1;
|
||||
if((x <<= 1) >= M)
|
||||
x -= M;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
Matrix multiply(Matrix &x, Matrix &y)
|
||||
{
|
||||
int i, j, k;
|
||||
Matrix z;
|
||||
z.init();
|
||||
for(i = 0; i < 4; i ++)
|
||||
for(k = 0; k < 4; k ++)
|
||||
if(x.a[i][k])
|
||||
{
|
||||
for(j = 0; j < 4; j ++)
|
||||
if(y.a[k][j])
|
||||
z.a[i][j] = (z.a[i][j] + mul(x.a[i][k], y.a[k][j])) % M;
|
||||
}
|
||||
return z;
|
||||
}
|
||||
void prepare()
|
||||
{
|
||||
int i, j, k = 40000;
|
||||
memset(isprime, -1, sizeof(isprime));
|
||||
P = 0;
|
||||
for(i = 2; i <= k; i ++)
|
||||
if(isprime[i])
|
||||
{
|
||||
prime[P ++] = i;
|
||||
for(j = i * i; j <= k; j += i)
|
||||
isprime[j] = 0;
|
||||
}
|
||||
}
|
||||
int euler(int n)
|
||||
{
|
||||
int i, ans = n;
|
||||
for(i = 0; i < pn; i ++)
|
||||
if(n % p[i] == 0)
|
||||
ans = ans / p[i] * (p[i] - 1);
|
||||
return ans;
|
||||
}
|
||||
void divide(int n)
|
||||
{
|
||||
int i, j;
|
||||
pn = 0;
|
||||
for(i = 0; i < P && prime[i] * prime[i] <= n; i ++)
|
||||
if(n % prime[i] == 0)
|
||||
{
|
||||
p[pn ++] = prime[i];
|
||||
while(n % prime[i] == 0)
|
||||
n /= prime[i];
|
||||
}
|
||||
if(n > 1)
|
||||
p[pn ++] = n;
|
||||
}
|
||||
void initmat()
|
||||
{
|
||||
int i;
|
||||
mat[0].init();
|
||||
mat[0].a[0][0] = 2, mat[0].a[0][2] = 1, mat[0].a[0][3] = M - 1;
|
||||
mat[0].a[1][0] = mat[0].a[1][1] = mat[0].a[1][2] = 1;
|
||||
mat[0].a[2][0] = 1, mat[0].a[2][2] = 2, mat[0].a[2][3] = M - 1;
|
||||
mat[0].a[3][2] = 1;
|
||||
for(i = 1; i < 32; i ++)
|
||||
mat[i] = multiply(mat[i - 1], mat[i - 1]);
|
||||
}
|
||||
void powmod(Matrix &unit, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; n; i ++, n >>= 1)
|
||||
if(n & 1)
|
||||
unit = multiply(mat[i], unit);
|
||||
}
|
||||
void dfs(int cur, int R, int x, long long &ans)
|
||||
{
|
||||
int i, cnt = 0, t = 1;
|
||||
if(cur == pn)
|
||||
{
|
||||
int n = euler(N / R);
|
||||
if(R == 1)
|
||||
ans = (ans + n) % M;
|
||||
else
|
||||
{
|
||||
unit.init();
|
||||
unit.a[0][0] = 3, unit.a[1][0] = 2, unit.a[2][0] = 3, unit.a[3][0] = 1;
|
||||
powmod(unit, R - 2);
|
||||
ans = (ans + mul(n, unit.a[0][0] + unit.a[1][0])) % M;
|
||||
}
|
||||
return ;
|
||||
}
|
||||
while(x % p[cur] == 0)
|
||||
++ cnt, x /= p[cur];
|
||||
for(i = 0; i <= cnt; i ++)
|
||||
{
|
||||
dfs(cur + 1, R * t, x, ans);
|
||||
t *= p[cur];
|
||||
}
|
||||
}
|
||||
void solve()
|
||||
{
|
||||
int i;
|
||||
long long ans, x, y, n;
|
||||
ans = 0;
|
||||
divide(N);
|
||||
initmat();
|
||||
dfs(0, 1, N, ans);
|
||||
printf("%I64d\n", ans / N);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
prepare();
|
||||
while(scanf("%d%I64d", &N, &M) == 2)
|
||||
{
|
||||
M *= N;
|
||||
solve();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,155 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
struct node
|
||||
{
|
||||
int x,y;
|
||||
int step;
|
||||
int total;
|
||||
int mem[333];
|
||||
int flag;
|
||||
}E[5011];
|
||||
int x_s,y_s;
|
||||
int x_e,y_e;
|
||||
int queue[6066];
|
||||
int K,Key;
|
||||
struct dictree
|
||||
{
|
||||
struct dictree *child[26];
|
||||
int flag;
|
||||
};
|
||||
struct dictree *root;
|
||||
int key;
|
||||
void insert(char *S)
|
||||
{
|
||||
struct dictree *cur,*New;
|
||||
int i,j;
|
||||
cur=root;
|
||||
for(i=0;S[i];i++)
|
||||
{
|
||||
if(cur->child[S[i]-'a']) cur=cur->child[S[i]-'a'];
|
||||
else
|
||||
{
|
||||
New=(struct dictree *)malloc(sizeof(struct dictree));
|
||||
for(j=0;j<26;j++) New->child[j]=0;
|
||||
New->flag=-1;
|
||||
cur->child[S[i]-'a']=New;
|
||||
cur=New;
|
||||
}
|
||||
}
|
||||
cur->flag=key;
|
||||
}
|
||||
int find(char *S)
|
||||
{
|
||||
struct dictree *cur;
|
||||
int i;
|
||||
cur=root;
|
||||
for(i=0;S[i];i++)
|
||||
{
|
||||
if(cur->child[S[i]-'a']) cur=cur->child[S[i]-'a'];
|
||||
else return -1;
|
||||
}
|
||||
return cur->flag;
|
||||
}
|
||||
int BFS()
|
||||
{
|
||||
int i;
|
||||
K=0;
|
||||
while(K<Key)
|
||||
{
|
||||
if(E[queue[K]].flag==1) return E[queue[K]].step;
|
||||
for(i=0;i<E[queue[K]].total;i++)
|
||||
{
|
||||
if(E[queue[K]].step+1<E[E[queue[K]].mem[i]].step)
|
||||
{
|
||||
E[E[queue[K]].mem[i]].step=E[queue[K]].step+1;
|
||||
queue[Key++]=E[queue[K]].mem[i];
|
||||
}
|
||||
}
|
||||
K++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
int n,m,k;
|
||||
int f[33];
|
||||
char name_temp[33][25];
|
||||
int i,j;
|
||||
int ans;
|
||||
int a,b;
|
||||
char str[25];
|
||||
int base[9]={5120,2560,1280,640,320,160,80,40,20};
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
scanf("%s%d%d",str,&a,&b);
|
||||
x_s=y_s=0;
|
||||
for(i=0;i<8;i++)
|
||||
{
|
||||
if(str[i]=='1') y_s+=base[i];
|
||||
else if(str[i]=='2')x_s+=base[i];
|
||||
else if(str[i]=='3'){x_s+=base[i];y_s+=base[i];}
|
||||
}
|
||||
x_s+=a*8;
|
||||
y_s+=b*8;
|
||||
scanf("%s%d%d",str,&a,&b);
|
||||
x_e=y_e=0;
|
||||
for(i=0;i<8;i++)
|
||||
{
|
||||
if(str[i]=='1') y_e+=base[i];
|
||||
else if(str[i]=='2')x_e+=base[i];
|
||||
else if(str[i]=='3'){x_e+=base[i];y_e+=base[i];}
|
||||
}
|
||||
x_e+=a*8;
|
||||
y_e+=b*8;
|
||||
root=(struct dictree *)malloc(sizeof(struct dictree));
|
||||
for(j=0;j<26;j++) root->child[j]=0;
|
||||
root->flag=-1;
|
||||
Key=0;
|
||||
scanf("%d",&n);
|
||||
for(key=0;key<n;key++)
|
||||
{
|
||||
scanf("%s%d%d",str,&E[key].x,&E[key].y);
|
||||
insert(str);
|
||||
E[key].step=11111111;
|
||||
E[key].total=0;
|
||||
if((E[key].x-x_s)*(E[key].x-x_s)+(E[key].y-y_s)*(E[key].y-y_s)<=1000000)
|
||||
{
|
||||
E[key].step=0;
|
||||
E[key].flag=-1;
|
||||
queue[Key++]=key;
|
||||
}
|
||||
else if((E[key].x-x_e)*(E[key].x-x_e)+(E[key].y-y_e)*(E[key].y-y_e)<=1000000) E[key].flag=1;
|
||||
else E[key].flag=0;
|
||||
}
|
||||
scanf("%d",&m);
|
||||
while(m--)
|
||||
{
|
||||
scanf("%d",&k);
|
||||
for(i=0;i<k;i++)
|
||||
{
|
||||
scanf("%s",name_temp[i]);
|
||||
f[i]=find(name_temp[i]);
|
||||
}
|
||||
for(i=1;i<k;i++)
|
||||
{
|
||||
for(j=0;j<i;j++)
|
||||
{
|
||||
E[f[i]].mem[E[f[i]].total++]=f[j];
|
||||
E[f[j]].mem[E[f[j]].total++]=f[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
if((x_s-x_e)*(x_s-x_e)+(y_s-y_e)*(y_s-y_e)<=4000000)
|
||||
{
|
||||
printf("walk there\n");
|
||||
continue;
|
||||
}
|
||||
ans=BFS();
|
||||
if(ans==-1) printf("take a taxi\n");
|
||||
else printf("%d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
const int maxn=303;
|
||||
int sum[maxn][maxn],e[maxn][maxn];
|
||||
int find(int n,int m)
|
||||
{
|
||||
int i,j,k,ans=0,t=0,p,q;
|
||||
if(e[n][m]==0)return 0;
|
||||
for(i=n-1,j=m-1;i>=1&&j>=1;i--,j--,t++)
|
||||
{
|
||||
if(!e[i][m]||!e[n][j])return ans;
|
||||
for(p=i,q=j;p<n;p++,q++)
|
||||
if(!e[i][q]||!e[p][j])break;
|
||||
if(p<n)continue;
|
||||
p=sum[n-1][m-1]-sum[i][m-1]-sum[n-1][j]+sum[i][j];
|
||||
if(abs(t*t-2*p)<=1)ans++;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int T;
|
||||
scanf("%d",&T);
|
||||
while(T--)
|
||||
{
|
||||
int i,j,k,n,m,ans=0;
|
||||
scanf("%d%d",&n,&m);
|
||||
memset(sum,0,sizeof(sum));
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
for(j=1;j<=m;j++)
|
||||
{
|
||||
scanf("%d",&e[i][j]);
|
||||
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+e[i][j];
|
||||
}
|
||||
}
|
||||
for(i=2;i<=n;i++)
|
||||
for(j=2;j<=m;j++)
|
||||
ans+=find(i,j);
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#define N 1005
|
||||
#define inf 999999999
|
||||
#define min(a,b) ((a)<(b)?(a):(b))
|
||||
int n,m,k,s,t,num;
|
||||
int low[N],pre[N],adj[N],q[N];
|
||||
struct edge
|
||||
{
|
||||
int u,v,c,w,next;
|
||||
edge(){}
|
||||
edge(int uu,int vv,int ww,int cc,int n)
|
||||
{u=uu;v=vv;w=ww;c=cc;next=n;}
|
||||
}e[10005];
|
||||
void insert(int u,int v,int w,int c)
|
||||
{
|
||||
e[num]=edge(u,v,w,c,adj[u]);
|
||||
adj[u]=num++;
|
||||
e[num]=edge(v,u,-w,0,adj[v]);
|
||||
adj[v]=num++;
|
||||
}
|
||||
int spfa()
|
||||
{
|
||||
int i,x,f[N]={0},head=0,tail=0;
|
||||
q[++tail]=s;
|
||||
memset(low,0x3f,sizeof(low));
|
||||
pre[s]=-1;
|
||||
low[s]=0;
|
||||
while(head!=tail)
|
||||
{
|
||||
x=q[head=(head+1)%N];
|
||||
f[x]=0;
|
||||
for(i=adj[x];i!=-1;i=e[i].next)
|
||||
if(e[i].c&&low[e[i].v]>low[x]+e[i].w)
|
||||
{
|
||||
pre[e[i].v]=i;
|
||||
low[e[i].v]=low[x]+e[i].w;
|
||||
if(!f[e[i].v])
|
||||
{
|
||||
f[e[i].v]=1;
|
||||
q[tail=(tail+1)%N]=e[i].v;
|
||||
}
|
||||
}
|
||||
}
|
||||
return low[t]<inf;
|
||||
}
|
||||
int mincost()
|
||||
{
|
||||
int ans=0;
|
||||
while(spfa())
|
||||
{
|
||||
if(low[t]>k)
|
||||
break;
|
||||
int v;
|
||||
ans++;
|
||||
for(v=pre[t];v!=-1;v=pre[e[v].u])
|
||||
{
|
||||
e[v].c--;
|
||||
e[v^1].c++;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,u,v;
|
||||
while(~scanf("%d%d%d",&n,&m,&k),n)
|
||||
{
|
||||
num=0;
|
||||
memset(adj,-1,sizeof(adj));
|
||||
s=0;
|
||||
t=n+n-1;
|
||||
while(m--)
|
||||
{
|
||||
scanf("%d%d",&u,&v);
|
||||
u--;v--;
|
||||
insert(u*2+1,v*2,1,1);
|
||||
}
|
||||
insert(s,1,0,inf);
|
||||
insert(2*n-2,t,0,inf);
|
||||
for(i=1;i<n-1;i++)
|
||||
insert(i*2,i*2+1,0,1);
|
||||
printf("%d\n",mincost());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
using namespace std;
|
||||
const int maxn = 1000010 ;
|
||||
int a[maxn],b[maxn];
|
||||
int n,k;
|
||||
int solve(int n)
|
||||
{
|
||||
int i=0,j=0;
|
||||
a[0]=1,b[0]=1;
|
||||
while(a[i]<n)
|
||||
{
|
||||
i++;
|
||||
a[i]=b[i-1]+1;
|
||||
while(a[j+1]*k<a[i])
|
||||
j++;
|
||||
if(a[j]*k<a[i])
|
||||
b[i]=b[j]+a[i];
|
||||
else
|
||||
b[i]=a[i];
|
||||
}
|
||||
if(n==a[i])
|
||||
return -1;
|
||||
int ans;
|
||||
while(n)
|
||||
{
|
||||
if(n>=a[i])
|
||||
n-=a[i],ans=a[i];
|
||||
i--;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
scanf("%d",&t);
|
||||
for(int cas=1;cas<=t;cas++)
|
||||
{
|
||||
scanf("%d%d",&n,&k);
|
||||
int ans = solve(n);
|
||||
printf("Case %d: ",cas);
|
||||
if(ans==-1)
|
||||
printf("lose\n");
|
||||
else
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cctype>
|
||||
#include<algorithm>
|
||||
#include<string>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
string a[110];
|
||||
int vis[200];
|
||||
int n,m;
|
||||
struct Node
|
||||
{
|
||||
int x1,y1,x2,y2;
|
||||
};
|
||||
Node res;
|
||||
int check(int x,int y)
|
||||
{
|
||||
res.x1=x,res.y1=y;
|
||||
int i=x,j=y;
|
||||
while(j<m&&a[i][j]==a[x][y]) j++;j--;
|
||||
if(j-y<=1) return 0;
|
||||
while(i<n&&a[i][j]==a[x][y]) i++;i--;
|
||||
if(i-x<=1) return 0;
|
||||
res.x2=i,res.y2=j;
|
||||
while(j>=0&&a[i][j]==a[x][y]) j--;j++;
|
||||
while(i>=0&&a[i][j]==a[x][y]) i--;i++;
|
||||
if(i==x&&j==y) return 1;
|
||||
return 0;
|
||||
}
|
||||
int ok(int x,int y)
|
||||
{
|
||||
for(int i=res.x1+1;i<res.x2;i++)
|
||||
{
|
||||
for(int j=res.y1+1;j<res.y2;j++)
|
||||
{
|
||||
if(a[i][j]!='.') return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(scanf("%d%d",&n,&m)==2&&n)
|
||||
{
|
||||
string cnt;
|
||||
for(int i=0;i<n;i++) cin>>a[i];
|
||||
memset(vis,0,sizeof(vis));
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
for(int j=0;j<m;j++)
|
||||
{
|
||||
if(a[i][j]=='.') continue;
|
||||
if(vis[int(a[i][j])]) continue;
|
||||
if(isalpha(a[i][j]))
|
||||
{
|
||||
vis[int(a[i][j])]=1;
|
||||
if(check(i,j)&&ok(i,j))
|
||||
{
|
||||
cnt+=a[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sort(cnt.begin(),cnt.end());
|
||||
cout<<cnt<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <stack>
|
||||
#include <list>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
#define INF 0x7F7F7F7F
|
||||
#define eps 1e-8
|
||||
typedef long long LL;
|
||||
typedef vector<int> VI;
|
||||
typedef vector<string> VS;
|
||||
typedef pair <int , int> PII;
|
||||
#define PB push_back
|
||||
#define MP make_pair
|
||||
#define CL(a,b) memset ( a , b , sizeof ( a ) )
|
||||
#define CLN(a,n,b) memset ( a , b , n * sizeof ( size ) )
|
||||
#define FOR(a,b,i) for ( i = a ; i < b ; i++ )
|
||||
#define REP(i,n) FOR ( 0 , n , i )
|
||||
#define FORB(b,a,i) for ( i = b ; i > a ; i-- )
|
||||
#define FORE(a,b,i) for ( i = a ; i <= b ; i++ )
|
||||
#define FOREB(b,a,i) for ( i = b ; i >= a ; i-- )
|
||||
#define COPY(a,b,lt,rt,i) for ( i = lt ; i <= rt ; i++ ) a[i] = b[i];
|
||||
#define sqr(a,b) sqrt ( (double) (a) * (a) + (double) (b) * (b) )
|
||||
template <class T> inline T INT_LEN( T v ) { int len = 1 ; while ( v /= 10 ) ++len; return len; }
|
||||
template <class T> inline double DIS ( T a , T b ) { return sqr ( a.x - b.x , a.y - b.y ); }
|
||||
typedef struct {
|
||||
double x , y;
|
||||
}tc;
|
||||
tc w[2] , t[2];
|
||||
double vw , vt , mn , mx;
|
||||
bool input (){
|
||||
while ( ~scanf ( "%lf" , &w[0].x ) ){
|
||||
scanf ( "%lf%lf%lf%lf" , &w[0].y , &w[1].x , &w[1].y , &vw );
|
||||
scanf ( "%lf%lf%lf%lf%lf" , &t[0].x , &t[0].y , &t[1].x , &t[1].y , &vt );
|
||||
scanf ( "%lf%lf" , &mn , &mx );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
template < typename T >
|
||||
double dot ( T a , T b , T c ){
|
||||
return ( b.x - a.x ) * ( c.x - a.x ) + ( b.y - a.y ) * ( c.y - a.y );
|
||||
}
|
||||
template < typename T >
|
||||
double det ( T a , T b , T c ){
|
||||
return ( b.x - a.x ) * ( c.y - a.y ) - ( b.y - a.y ) * ( c.x - a.x );
|
||||
}
|
||||
double get_dis ( tc fro , tc to , tc o , double dx , double dy ){
|
||||
fro.x += dx , fro.y += dy;
|
||||
to.x += dx , to.y += dy;
|
||||
double dis1 = DIS ( o , fro );
|
||||
double dis2 = DIS ( o , to );
|
||||
double d = min ( dis1 , dis2 );
|
||||
double di = DIS ( fro , to );
|
||||
if ( di <= eps ){
|
||||
return d;
|
||||
}else if ( dot ( fro , o , to ) >= -eps && dot ( to , o , fro ) >= -eps ){
|
||||
return fabs ( det ( fro , to , o ) ) / di;
|
||||
}else {
|
||||
return d;
|
||||
}
|
||||
}
|
||||
double calc ( tc fro , tc to , tc o , double dx , double dy){
|
||||
int lt = 0 , rt = INF , mid;
|
||||
double dis1 , dis2;
|
||||
while ( lt <= rt ){
|
||||
mid = ( lt + rt ) >> 1;
|
||||
dis1 = get_dis ( fro , to , o , dx * mid , dy * mid );
|
||||
dis2 = get_dis ( fro , to , o , dx * -~mid , dy * -~mid );
|
||||
if ( dis1 <= dis2 + eps ) rt = ~-mid;
|
||||
else lt = -~mid;
|
||||
}
|
||||
return get_dis ( fro , to , o , dx * lt , dy * lt );
|
||||
}
|
||||
void solve (){
|
||||
tc tg , wg , p1 , p2;
|
||||
double L1 = DIS ( t[0] , t[1] );
|
||||
tg.x = ( t[1].x - t[0].x ) * vt / L1;
|
||||
tg.y = ( t[1].y - t[0].y ) * vt / L1;
|
||||
double L2 = DIS ( w[0] , w[1] );
|
||||
wg.x = ( w[1].x - w[0].x ) * vw / L2;
|
||||
wg.y = ( w[1].y - w[0].y ) * vw / L2;
|
||||
double time = L1 / vt;
|
||||
p1.x = t[0].x + ( tg.x - wg.x ) * time;
|
||||
p1.y = t[0].y + ( tg.y - wg.y ) * time;
|
||||
p2.x = p1.x + ( -tg.x - wg.x ) * time;
|
||||
p2.y = p1.y + ( -tg.y - wg.y ) * time;
|
||||
double d1 = calc ( t[0] , p1 , w[0] , p2.x - t[0].x , p2.y - t[0].y );
|
||||
double d2 = calc ( p1 , p2 , w[0] , p2.x - t[0].x , p2.y - t[0].y );
|
||||
double d = min ( d1 , d2 );
|
||||
if ( d + eps < mn ) puts ( "Dangerous" );
|
||||
else if ( d - eps > mx ) puts ( "Miss" );
|
||||
else puts ( "Perfect" );
|
||||
}
|
||||
int main (void){
|
||||
while ( input () ){
|
||||
solve ();
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#define INF 0x3f3f3f3f
|
||||
const double esp=1e-3;
|
||||
int n,m,node[20],edge[20][20],dis[20],visit[20],temp[20],su[20];
|
||||
double minn;
|
||||
int prim(int s)
|
||||
{
|
||||
int length=0;
|
||||
memset(visit,0,sizeof(visit));
|
||||
for(int i=1;i<=m;i++)
|
||||
dis[temp[i]]=edge[s][temp[i]];
|
||||
visit[s]=1;
|
||||
dis[s]=0;
|
||||
int point=s;
|
||||
for(int i=1;i<m;i++)
|
||||
{
|
||||
int mit=INF;
|
||||
for(int j=1;j<=m;j++)
|
||||
if(!visit[temp[j]]&&dis[temp[j]]<mit)
|
||||
{
|
||||
mit=dis[temp[j]];
|
||||
point=temp[j];
|
||||
}
|
||||
visit[point]=1;
|
||||
length+=mit;
|
||||
for(int j=1;j<=m;j++)
|
||||
if(!visit[temp[j]]&&dis[temp[j]]>edge[point][temp[j]])
|
||||
dis[temp[j]]=edge[point][temp[j]];
|
||||
}
|
||||
return length;
|
||||
}
|
||||
void dfs(int shi,int count)
|
||||
{
|
||||
if(count==m)
|
||||
{
|
||||
double sum_node=0,sum_edge=0;
|
||||
for(int i=1;i<=m;i++)
|
||||
sum_node+=node[temp[i]];
|
||||
sum_edge=prim(temp[1]);
|
||||
double ridio=sum_edge/sum_node;
|
||||
if(ridio-minn<-esp)
|
||||
{
|
||||
minn=ridio;
|
||||
for(int i=1;i<=m;i++)
|
||||
su[i]=temp[i];
|
||||
}
|
||||
return;
|
||||
}
|
||||
for(int i=shi+1;i<=n;i++)
|
||||
{
|
||||
temp[count+1]=i;
|
||||
dfs(i,count+1);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(~scanf("%d%d",&n,&m))
|
||||
{
|
||||
if(n==0 && m==0)
|
||||
break;
|
||||
minn=INF;
|
||||
for(int i=1;i<=n;i++)
|
||||
scanf("%d",&node[i]);
|
||||
for(int i=1;i<=n;i++)
|
||||
for(int j=1;j<=n;j++)
|
||||
scanf("%d",&edge[i][j]);
|
||||
for(int i=1;i<=n;i++)
|
||||
{
|
||||
temp[1]=i;
|
||||
dfs(i,1);
|
||||
}
|
||||
for(int i=1;i<m;i++)
|
||||
printf("%d ",su[i]);
|
||||
printf("%d",su[m]);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <deque>
|
||||
using namespace std;
|
||||
const int INF = 0x7fffffff;
|
||||
int N,M,K,hap[102][10020],len[102][10020],f[102][10020],sum[10300];
|
||||
struct node{
|
||||
int hap,dis;
|
||||
node() {}
|
||||
node(int h,int d):hap(h),dis(d) {}
|
||||
};
|
||||
int main(){
|
||||
while (scanf("%d%d%d",&N,&M,&K) != EOF && N+M+K){
|
||||
++N;
|
||||
memset(f,0,sizeof(f));
|
||||
memset(len,0,sizeof(len));
|
||||
memset(hap,0,sizeof(hap));
|
||||
for (int i = 1; i <= N; i++)
|
||||
for (int j = 1; j <= M; j++)
|
||||
scanf("%d",&hap[i][j]);
|
||||
for (int i = 1; i <= N; i++)
|
||||
for (int j = 1; j <= M; j++)
|
||||
scanf("%d",&len[i][j]);
|
||||
for (int i = 1; i <= N; i++){
|
||||
deque<node> deq;
|
||||
int dis = 0;
|
||||
for (int j = 1; j <= M; j++)
|
||||
sum[j] = sum[j-1]+hap[i][j];
|
||||
for (int j = 0; j <= M; j++){
|
||||
dis += len[i][j];
|
||||
while (!deq.empty() && deq.front().hap <= f[i-1][j]-sum[j])
|
||||
deq.pop_front();
|
||||
deq.push_front(node(f[i-1][j]-sum[j],dis));
|
||||
while (!deq.empty() && deq.front().dis-deq.back().dis > K)
|
||||
deq.pop_back();
|
||||
f[i][j] = deq.back().hap + sum[j];
|
||||
}
|
||||
deq.clear();
|
||||
dis = 0;
|
||||
len[i][M+1] = 0;
|
||||
for (int j = M; j >= 0; j--){
|
||||
dis += len[i][j+1];
|
||||
while (!deq.empty() && deq.front().hap <= f[i-1][j]+sum[j])
|
||||
deq.pop_front();
|
||||
deq.push_front(node(f[i-1][j]+sum[j],dis));
|
||||
while (!deq.empty() && deq.front().dis - deq.back().dis > K)
|
||||
deq.pop_back();
|
||||
f[i][j] = max(f[i][j],deq.back().hap - sum[j]);
|
||||
}
|
||||
}
|
||||
int ans = 0;
|
||||
for (int j = 0; j <= M; j++)
|
||||
ans = max(ans,f[N][j]);
|
||||
printf("%d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
#include<iostream>
|
||||
#include<algorithm>
|
||||
#include<cstdio>
|
||||
using namespace std;
|
||||
const int N=100010;
|
||||
struct darling
|
||||
{
|
||||
int s,t,m,mt;
|
||||
} a[N];
|
||||
int n;
|
||||
bool cmp(darling x,darling y)
|
||||
{
|
||||
if(x.m==y.m)
|
||||
return x.s<y.s;
|
||||
return x.m<y.m;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
while(~scanf("%d",&n))
|
||||
{
|
||||
if(!n)
|
||||
break;
|
||||
for(int i=0; i<n; i++)
|
||||
{
|
||||
scanf("%d%d",&a[i].s,&a[i].t);
|
||||
a[i].mt=(a[i].t-a[i].s)/2+1;
|
||||
a[i].m=a[i].s+a[i].mt;
|
||||
}
|
||||
sort(a,a+n,cmp);
|
||||
int i=0;
|
||||
for(int t=0; i<n; i++)
|
||||
{
|
||||
if(t>a[i].t-a[i].mt)
|
||||
break;
|
||||
if(t>=a[i].s)
|
||||
t+=a[i].mt;
|
||||
else
|
||||
t=a[i].m;
|
||||
}
|
||||
if(i==n)
|
||||
printf("YES\n");
|
||||
else
|
||||
printf("NO\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
#include<iostream>
|
||||
#include<cstdio>
|
||||
#include<cstring>
|
||||
#include<string>
|
||||
#include<cstdlib>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int top=100100;
|
||||
struct Num
|
||||
{
|
||||
int val,pos;
|
||||
}num[top];
|
||||
bool operator<(Num nl,Num nr)
|
||||
{
|
||||
return nl.val<nr.val;
|
||||
}
|
||||
int order[top],tree[top];
|
||||
int n,i,leftLess,leftMuch,rightLess,rightMuch;
|
||||
int lowbit(int x)
|
||||
{
|
||||
return x&(-x);
|
||||
}
|
||||
void update(int i,int x)
|
||||
{
|
||||
while(i<=n)
|
||||
{
|
||||
tree[i]+=x;
|
||||
i+=lowbit(i);
|
||||
}
|
||||
}
|
||||
int query(int i)
|
||||
{
|
||||
int ans;
|
||||
for(ans=0;i>0;i-=lowbit(i))
|
||||
{
|
||||
ans+=tree[i];
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
__int64 ans;
|
||||
scanf("%d",&t);
|
||||
while(t--)
|
||||
{
|
||||
scanf("%d",&n);
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
scanf("%d",&num[i].val);
|
||||
num[i].pos=i;
|
||||
}
|
||||
sort(num+1,num+n+1);
|
||||
order[num[1].pos]=1;
|
||||
for(i=2;i<=n;i++)
|
||||
{
|
||||
if(num[i].val==num[i-1].val)
|
||||
order[num[i].pos]=order[num[i-1].pos];
|
||||
else
|
||||
order[num[i].pos]=i;
|
||||
}
|
||||
memset(tree,0,sizeof(tree));
|
||||
ans=0;
|
||||
for(i=1;i<=n;i++)
|
||||
{
|
||||
update(order[i],1);
|
||||
leftLess=query(order[i])-1;
|
||||
leftMuch=i-leftLess-1;
|
||||
rightLess=order[i]-leftLess-1;
|
||||
rightMuch=n-order[i]-leftMuch;
|
||||
ans+=leftLess*rightMuch+leftMuch*rightLess;
|
||||
}
|
||||
printf("%I64d\n",ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <deque>
|
||||
#include <queue>
|
||||
#include <iterator>
|
||||
#include <stack>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
using namespace std;
|
||||
#define si1(a) scanf("%d",&a)
|
||||
#define si2(a,b) scanf("%d%d",&a,&b)
|
||||
#define sd1(a) scanf("%lf",&a)
|
||||
#define sd2(a,b) scanf("%lf%lf",&a,&b)
|
||||
#define ss1(s) scanf("%s",s)
|
||||
#define pi1(a) printf("%d\n",a)
|
||||
#define pi2(a,b) printf("%d %d\n",a,b)
|
||||
#define mset(a,b) memset(a,b,sizeof(a))
|
||||
#define forb(i,a,b) for(int i=a;i<b;i++)
|
||||
#define ford(i,a,b) for(int i=a;i<=b;i++)
|
||||
typedef long long LL;
|
||||
const int N=100005;
|
||||
const int INF=0x3f3f3f3f;
|
||||
const double PI=acos(-1.0);
|
||||
const double eps=1e-8;
|
||||
double H,D,V,R;
|
||||
double xiaohao(double r)
|
||||
{
|
||||
double xh=0;
|
||||
xh+=H*R*R*acos((R-r)/R)/3.0;
|
||||
xh-=2.0/3.0*(R-r)*H*sqrt(R*R-(R-r)*(R-r));
|
||||
xh+=1.0/3.0*(R-r)*(R-r)*(R-r)*H/R*log((R+sqrt(R*R-(R-r)*(R-r)))/(R-r));
|
||||
return xh;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i,j,T;
|
||||
si1(T);
|
||||
while(T--)
|
||||
{
|
||||
sd1(H);sd2(D,V);
|
||||
R=D/2;
|
||||
double low=0,up=R,m,tmp;
|
||||
while((up-low)>eps)
|
||||
{
|
||||
m=(up+low)/2;
|
||||
double tmp=xiaohao(m);
|
||||
if(tmp>V)
|
||||
up=m;
|
||||
else
|
||||
low=m;
|
||||
}
|
||||
printf("%.5f\n",D-m);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,510 @@
|
|||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <bitset>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <list>
|
||||
#include <set>
|
||||
#include <map>
|
||||
using namespace std;
|
||||
#define REP(i, n) for (int i=0;i<int(n);++i)
|
||||
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
|
||||
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
|
||||
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
|
||||
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
|
||||
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
|
||||
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
|
||||
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
|
||||
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
|
||||
#define REP_N(i, n) for (i=0;i<int(n);++i)
|
||||
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
|
||||
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
|
||||
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
|
||||
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
|
||||
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
|
||||
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
|
||||
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
|
||||
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
|
||||
#define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)
|
||||
#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)
|
||||
#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)
|
||||
#define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)
|
||||
#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)
|
||||
#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)
|
||||
//#define ECH(it, A) for (typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
|
||||
#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
|
||||
#define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; )
|
||||
#define _TO(i, a, b) int s_=a<b?1:-1,b_=b+s_;for(int i=a+s_;i!=b_;i+=s_)
|
||||
#define TO(i, a, b) int s_=a<b?1:-1,b_=b+s_;for(int i=a;i!=b_;i+=s_)
|
||||
#define TO_1(i, a, b) int s_=a<b?1:-1,b_=b;for(int i=a;i!=b_;i+=s_)
|
||||
#define SQZ(i, j, a, b) for (int i=int(a),j=int(b)-1;i<j;++i,--j)
|
||||
#define SQZ_1(i, j, a, b) for (int i=int(a),j=int(b);i<=j;++i,--j)
|
||||
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
|
||||
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
|
||||
#define ALL(A) A.begin(), A.end()
|
||||
#define LLA(A) A.rbegin(), A.rend()
|
||||
#define CPY(A, B) memcpy(A, B, sizeof(A))
|
||||
#define INS(A, P, B) A.insert(A.begin() + P, B)
|
||||
#define ERS(A, P) A.erase(A.begin() + P)
|
||||
#define BSC(A, X) find(ALL(A), X) // != A.end()
|
||||
#define CTN(T, x) (T.find(x) != T.end())
|
||||
#define SZ(A) int(A.size())
|
||||
#define PB push_back
|
||||
#define MP(A, B) make_pair(A, B)
|
||||
#define Rush int T____; RD(T____); DO(T____)
|
||||
#define Display(A, n, m) { \
|
||||
REP(i, n){ \
|
||||
REP(j, m) cout << A[i][j] << " ";\
|
||||
cout << endl; \
|
||||
} \
|
||||
}
|
||||
#define Display_1(A, n, m) { \
|
||||
REP_1(i, n){ \
|
||||
REP_1(j, m) cout << A[i][j] << " ";\
|
||||
cout << endl; \
|
||||
} \
|
||||
}
|
||||
#pragma comment(linker, "/STACK:36777216")
|
||||
#define fi first
|
||||
#define se second
|
||||
typedef long long LL;
|
||||
typedef double DB;
|
||||
typedef unsigned UINT;
|
||||
typedef unsigned long long ULL;
|
||||
typedef vector<int> VI;
|
||||
typedef vector<char> VC;
|
||||
typedef vector<string> VS;
|
||||
typedef vector<LL> VL;
|
||||
typedef vector<DB> VD;
|
||||
typedef set<int> SI;
|
||||
typedef set<string> SS;
|
||||
typedef set<LL> SL;
|
||||
typedef set<DB> SD;
|
||||
typedef map<int, int> MII;
|
||||
typedef map<string, int> MSI;
|
||||
typedef map<LL, int> MLI;
|
||||
typedef map<DB, int> MDI;
|
||||
typedef map<int, bool> MIB;
|
||||
typedef map<string, bool> MSB;
|
||||
typedef map<LL, bool> MLB;
|
||||
typedef map<DB, bool> MDB;
|
||||
typedef pair<int, int> PII;
|
||||
typedef pair<int, bool> PIB;
|
||||
typedef vector<PII> VII;
|
||||
typedef vector<VI> VVI;
|
||||
typedef vector<VII> VVII;
|
||||
typedef set<PII> SII;
|
||||
typedef map<PII, int> MPIII;
|
||||
typedef map<PII, bool> MPIIB;
|
||||
template<class T> inline int RD(T &);
|
||||
template<class T> inline void OT(const T &);
|
||||
inline LL RD(){LL x; return RD(x);}
|
||||
inline char RC(char &c){scanf(" %c", &c); return c;}
|
||||
inline char RC(){char c; return RC(c);}
|
||||
inline DB RF(DB &x){scanf("%lf", &x); return x;}
|
||||
inline DB RF(){DB x; return RF(x);}
|
||||
inline char* RS(char *s){scanf("%s", s); return s;}
|
||||
template<class T0, class T1> inline void RD(T0 &x0, T1 &x1){RD(x0), RD(x1);}
|
||||
template<class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2);}
|
||||
template<class T0, class T1, class T2, class T3> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3);}
|
||||
template<class T0, class T1, class T2, class T3, class T4> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);}
|
||||
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);}
|
||||
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);}
|
||||
template<class T0, class T1> inline void OT(T0 &x0, T1 &x1){OT(x0), OT(x1);}
|
||||
template<class T0, class T1, class T2> inline void OT(T0 &x0, T1 &x1, T2 &x2){OT(x0), OT(x1), OT(x2);}
|
||||
template<class T0, class T1, class T2, class T3> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
|
||||
template<class T0, class T1, class T2, class T3, class T4> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
|
||||
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
|
||||
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}
|
||||
inline void RC(char &a, char &b){RC(a), RC(b);}
|
||||
inline void RC(char &a, char &b, char &c){RC(a), RC(b), RC(c);}
|
||||
inline void RF(DB &a, DB &b){RF(a), RF(b);}
|
||||
inline void RF(DB &a, DB &b, DB &c){RF(a), RF(b), RF(c);}
|
||||
inline void RS(char *s1, char *s2){RS(s1), RS(s2);}
|
||||
inline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}
|
||||
template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
|
||||
template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
|
||||
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
|
||||
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
|
||||
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
|
||||
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
|
||||
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}
|
||||
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
|
||||
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
|
||||
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2){FLC(A0), FLC(A1), FLC(A2);}
|
||||
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3){FLC(A0), FLC(A1), FLC(A2), FLC(A3);}
|
||||
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4);}
|
||||
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5);}
|
||||
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6);}
|
||||
template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){while (!Q.empty()) Q.pop();}
|
||||
template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){while (!Q.empty()) Q.pop();}
|
||||
template<class T> inline void CLR(T &A){A.clear();}
|
||||
template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
|
||||
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
|
||||
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
|
||||
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
|
||||
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
|
||||
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
|
||||
template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}
|
||||
template<class T> inline void SRT(T &A){sort(ALL(A));}
|
||||
template<class T, class C> inline void SRT(T &A, C B){sort(ALL(A), B);}
|
||||
const int MOD = 1000000007;
|
||||
const int INF = 0x3f3f3f3f;
|
||||
const DB EPS = 1e-6;
|
||||
const DB OO = 1e15;
|
||||
const DB PI = acos(-1.0); //M_PI;
|
||||
template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
|
||||
template<class T> inline void checkMax(T &a,const T b){if (b>a) a=b;}
|
||||
template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
|
||||
template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
|
||||
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
|
||||
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
|
||||
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
|
||||
template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}
|
||||
template<class T> inline T sqr(T a){return a*a;}
|
||||
template<class T> inline T cub(T a){return a*a*a;}
|
||||
inline int Ceil(int x, int y){return (x - 1) / y + 1;}
|
||||
inline bool _1(int x, int i){return x&1<<i;}
|
||||
inline bool _1(LL x, int i){return x&1LL<<i;}
|
||||
inline LL _1(int i){return 1LL<<i;}
|
||||
inline LL _U(int i){return _1(i) - 1;};
|
||||
template<class T> inline T low_bit(T x) {return x & -x;}
|
||||
template<class T> inline T high_bit(T x) {T p = low_bit(x);while (p != x) x -= p, p = low_bit(x);return p;}
|
||||
template<class T> inline T cover_bit(T x){T p = 1; while (p < x) p <<= 1;return p;}
|
||||
inline int count_bits(int x){
|
||||
x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
|
||||
x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
|
||||
x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
|
||||
x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
|
||||
x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
|
||||
return x;
|
||||
}
|
||||
inline int count_bits(LL x){
|
||||
x = (x & 0x5555555555555555LL) + ((x & 0xaaaaaaaaaaaaaaaaLL) >> 1);
|
||||
x = (x & 0x3333333333333333LL) + ((x & 0xccccccccccccccccLL) >> 2);
|
||||
x = (x & 0x0f0f0f0f0f0f0f0fLL) + ((x & 0xf0f0f0f0f0f0f0f0LL) >> 4);
|
||||
x = (x & 0x00ff00ff00ff00ffLL) + ((x & 0xff00ff00ff00ff00LL) >> 8);
|
||||
x = (x & 0x0000ffff0000ffffLL) + ((x & 0xffff0000ffff0000LL) >> 16);
|
||||
x = (x & 0x00000000ffffffffLL) + ((x & 0xffffffff00000000LL) >> 32);
|
||||
return x;
|
||||
}
|
||||
inline int reverse_bits(int x){
|
||||
x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
|
||||
x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
|
||||
x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
|
||||
x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
|
||||
x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);
|
||||
return x;
|
||||
}
|
||||
inline LL reverse_bits(LL x){
|
||||
x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
|
||||
x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
|
||||
x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
|
||||
x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
|
||||
x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);
|
||||
x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);
|
||||
return x;
|
||||
}
|
||||
inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}
|
||||
inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}
|
||||
inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}
|
||||
inline int dff(int a, int b){a -= b; if (a < 0) a += MOD; return a;}
|
||||
inline void MUL(int &a, int b){a = (LL)a * b % MOD;}
|
||||
inline int pdt(int a, int b){return (LL)a * b % MOD;}
|
||||
inline int sum(int a, int b, int c){return sum(sum(a, b), c);}
|
||||
inline int sum(int a, int b, int c, int d){return sum(sum(a, b), sum(c, d));}
|
||||
inline int pdt(int a, int b, int c){return pdt(pdt(a, b), c);}
|
||||
inline int pdt(int a, int b, int c, int d){return pdt(pdt(pdt(a, b), c), d);}
|
||||
inline int pow(int a, LL b){
|
||||
int c(1); while (b){
|
||||
if (b&1) MUL(c, a);
|
||||
MUL(a, a), b >>= 1;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
template<class T> inline T pow(T a, LL b){
|
||||
T c(1); while (b){
|
||||
if (b&1) c *= a;
|
||||
a *= a, b >>= 1;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
inline int _I(int b){
|
||||
int a = MOD, x1 = 0, x2 = 1, q;
|
||||
while (true){
|
||||
q = a / b, a %= b;
|
||||
if (!a) return (x2 + MOD) % MOD;
|
||||
DEC(x1, pdt(q, x2));
|
||||
q = b / a, b %= a;
|
||||
if (!b) return (x1 + MOD) % MOD;
|
||||
DEC(x2, pdt(q, x1));
|
||||
}
|
||||
}
|
||||
inline void DIA(int &a, int b){MUL(a, _I(b));}
|
||||
inline int qtt(int a, int b){return pdt(a, _I(b));}
|
||||
inline int phi(int n){
|
||||
int res = n; for (int i=2;sqr(i)<=n;++i) if (!(n%i)){
|
||||
DEC(res, qtt(res, i));
|
||||
do{n /= i;} while(!(n%i));
|
||||
}
|
||||
if (n != 1)
|
||||
DEC(res, qtt(res, n));
|
||||
return res;
|
||||
}
|
||||
struct Po; struct Line; struct Seg;
|
||||
inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}
|
||||
inline int sgn(DB x, DB y){return sgn(x - y);}
|
||||
struct Po{
|
||||
DB x, y;
|
||||
Po(DB _x = 0, DB _y = 0):x(_x), y(_y){}
|
||||
friend istream& operator >>(istream& in, Po &p){return in >> p.x >> p.y;}
|
||||
friend ostream& operator <<(ostream& out, Po p){return out << "(" << p.x << ", " << p.y << ")";}
|
||||
friend bool operator ==(Po, Po);
|
||||
friend bool operator !=(Po, Po);
|
||||
friend Po operator +(Po, Po);
|
||||
friend Po operator -(Po, Po);
|
||||
friend Po operator *(Po, DB);
|
||||
friend Po operator /(Po, DB);
|
||||
bool operator < (const Po &rhs) const{return sgn(x, rhs.x) < 0 || sgn(x, rhs.x) == 0 && sgn(y, rhs.y) < 0;}
|
||||
Po operator-() const{return Po(-x, -y);}
|
||||
Po& operator +=(Po rhs){x += rhs.x, y += rhs.y; return *this;}
|
||||
Po& operator -=(Po rhs){x -= rhs.x, y -= rhs.y; return *this;}
|
||||
Po& operator *=(DB k){x *= k, y *= k; return *this;}
|
||||
Po& operator /=(DB k){x /= k, y /= k; return *this;}
|
||||
DB length_sqr(){return sqr(x) + sqr(y);}
|
||||
DB length(){return sqrt(length_sqr());}
|
||||
DB atan(){
|
||||
return atan2(y, x);
|
||||
}
|
||||
void input(){
|
||||
scanf("%lf %lf", &x, &y);
|
||||
}
|
||||
};
|
||||
bool operator ==(Po a, Po b){return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;}
|
||||
bool operator !=(Po a, Po b){return sgn(a.x - b.x) != 0 || sgn(a.y - b.y) != 0;}
|
||||
Po operator +(Po a, Po b){return Po(a.x + b.x, a.y + b.y);}
|
||||
Po operator -(Po a, Po b){return Po(a.x - b.x, a.y - b.y);}
|
||||
Po operator *(Po a, DB k){return Po(a.x * k, a.y * k);}
|
||||
Po operator *(DB k, Po a){return a * k;}
|
||||
Po operator /(Po a, DB k){return Po(a.x / k, a.y / k);}
|
||||
struct Line{
|
||||
Po a, b;
|
||||
Line(Po _a = Po(), Po _b = Po()):a(_a), b(_b){}
|
||||
Line(DB x0, DB y0, DB x1, DB y1):a(Po(x0, y0)), b(Po(x1, y1)){}
|
||||
Line(Seg);
|
||||
friend ostream& operator <<(ostream& out, Line p){return out << p.a << "-" << p.b;}
|
||||
};
|
||||
struct Seg{
|
||||
Po a, b;
|
||||
Seg(Po _a = Po(), Po _b = Po()):a(_a), b(_b){}
|
||||
Seg(DB x0, DB y0, DB x1, DB y1):a(Po(x0, y0)), b(Po(x1, y1)){}
|
||||
Seg(Line l);
|
||||
friend ostream& operator <<(ostream& out, Seg p){return out << p.a << "-" << p.b;}
|
||||
DB length(){return (b - a).length();}
|
||||
};
|
||||
Line::Line(Seg l):a(l.a), b(l.b){}
|
||||
Seg::Seg(Line l):a(l.a), b(l.b){}
|
||||
#define innerProduct dot
|
||||
#define scalarProduct dot
|
||||
#define dotProduct dot
|
||||
#define outerProduct det
|
||||
#define crossProduct det
|
||||
inline DB dot(DB x1, DB y1, DB x2, DB y2){return x1 * x2 + y1 * y2;}
|
||||
inline DB dot(Po a, Po b){return dot(a.x, a.y, b.x, b.y);}
|
||||
inline DB dot(Po p0, Po p1, Po p2){return dot(p1 - p0, p2 - p0);}
|
||||
inline DB dot(Line l1, Line l2){return dot(l1.b - l1.a, l2.b - l2.a);}
|
||||
inline DB det(DB x1, DB y1, DB x2, DB y2){return x1 * y2 - x2 * y1;}
|
||||
inline DB det(Po a, Po b){return det(a.x, a.y, b.x, b.y);}
|
||||
inline DB det(Po p0, Po p1, Po p2){return det(p1 - p0, p2 - p0);}
|
||||
inline DB det(Line l1, Line l2){return det(l1.b - l1.a, l2.b - l2.a);}
|
||||
template<class T1, class T2> inline DB dist(T1 x, T2 y){return sqrt(dist_sqr(x, y));}
|
||||
inline DB dist_sqr(Po a, Po b){return sqr(a.x - b.x) + sqr(a.y - b.y);}
|
||||
inline DB dist_sqr(Po p, Line l){Po v0 = l.b - l.a, v1 = p - l.a; return sqr(fabs(det(v0, v1))) / v0.length_sqr();}
|
||||
inline DB dist_sqr(Po p, Seg l){
|
||||
Po v0 = l.b - l.a, v1 = p - l.a, v2 = p - l.b;
|
||||
if (sgn(dot(v0, v1)) * sgn(dot(v0, v2)) <= 0) return dist_sqr(p, Line(l));
|
||||
else return min(v1.length_sqr(), v2.length_sqr());
|
||||
}
|
||||
inline DB dist_sqr(Line l, Po p){return dist_sqr(p, l);}
|
||||
inline DB dist_sqr(Seg l, Po p){return dist_sqr(p, l);}
|
||||
inline DB dist_sqr(Line l1, Line l2){
|
||||
if (sgn(det(l1, l2)) != 0) return 0;
|
||||
return dist_sqr(l1.a, l2);
|
||||
}
|
||||
inline DB dist_sqr(Line l1, Seg l2){
|
||||
Po v0 = l1.b - l1.a, v1 = l2.a - l1.a, v2 = l2.b - l1.a; DB c1 = det(v0, v1), c2 = det(v0, v2);
|
||||
return sgn(c1) != sgn(c2) ? 0 : sqr(min(fabs(c1), fabs(c2))) / v0.length_sqr();
|
||||
}
|
||||
bool isIntersect(Seg l1, Seg l2){
|
||||
return
|
||||
min(l1.a.x, l1.b.x) <= max(l2.a.x, l2.b.x) &&
|
||||
min(l2.a.x, l2.b.x) <= max(l1.a.x, l1.b.x) &&
|
||||
min(l1.a.y, l1.b.y) <= max(l2.a.y, l2.b.y) &&
|
||||
min(l2.a.y, l2.b.y) <= max(l1.a.y, l1.b.y) &&
|
||||
sgn( det(l1.a, l2.a, l2.b) ) * sgn( det(l1.b, l2.a, l2.b) ) <= 0 &&
|
||||
sgn( det(l2.a, l1.a, l1.b) ) * sgn( det(l2.b, l1.a, l1.b) ) <= 0;
|
||||
}
|
||||
inline DB dist_sqr(Seg l1, Seg l2){
|
||||
if (isIntersect(l1, l2)) return 0;
|
||||
else return min(dist_sqr(l1.a, l2), dist_sqr(l1.b, l2), dist_sqr(l2.a, l1), dist_sqr(l2.b, l1));
|
||||
}
|
||||
inline bool isOnExtremePoint(const Po &p, const Seg &l){
|
||||
return p == l.a || p == l.b;
|
||||
}
|
||||
inline bool isOnseg(const Po &p, const Seg &l){
|
||||
return sgn(det(p, l.a, l.b)) == 0 &&
|
||||
sgn(l.a.x, p.x) * sgn(l.b.x, p.x) <= 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) <= 0;
|
||||
}
|
||||
inline Po intersect(const Line &l1, const Line &l2){
|
||||
return l1.a + (l1.b - l1.a) * (det(l2.a, l1.a, l2.b) / det(l2, l1));
|
||||
}
|
||||
inline Po intersect(const Po & p, const Line &l){
|
||||
return intersect(Line(p, p + Po(l.a.y - l.b.y, l.b.x - l.a.x)), l);
|
||||
}
|
||||
inline Po rotate(Po p, DB alpha, Po o = Po()){
|
||||
p.x -= o.x, p.y -= o .y;
|
||||
return Po(p.x * cos(alpha) - p.y * sin(alpha), p.y * cos(alpha) + p.x * sin(alpha)) + o;
|
||||
}
|
||||
inline int rand32(){return (bool(rand() & 1) << 30) | (rand() << 15) + rand();}
|
||||
inline int random32(int l, int r){return rand32() % (r - l + 1) + l;}
|
||||
inline int random(int l, int r){return rand() % (r - l + 1) + l;}
|
||||
int dice(){return rand() % 6;}
|
||||
bool coin(){return rand() % 2;}
|
||||
template<class T> inline int RD(T &x){
|
||||
char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
|
||||
return x;
|
||||
}
|
||||
int ____Case; template<class T> inline void OT(const T &x){
|
||||
printf("%d\n", x);
|
||||
}
|
||||
const int N = 51;
|
||||
map<int, VII> R;
|
||||
int end_time;
|
||||
struct Elevator{
|
||||
VI req[N]; int cnt[N];
|
||||
int pos, t; bool idle, dir, anchor, open;
|
||||
void init(int _pos){RST(cnt); CLR(req, N); pos = _pos, idle = true, t = -1;}
|
||||
inline bool c0(){
|
||||
FOR(i, pos + 1, N) if (cnt[i]) return true;
|
||||
return false;
|
||||
}
|
||||
inline bool c1(){
|
||||
DWN(i, pos, 0) if (cnt[i]) return true;
|
||||
return false;
|
||||
}
|
||||
inline bool __c(const VI& req){
|
||||
return !req.empty();
|
||||
}
|
||||
inline bool _c(const VI& req){
|
||||
if (!__c(req)) return false;
|
||||
REP(i, SZ(req)) if ((req[i] > pos) ^ dir) return true;
|
||||
return false;
|
||||
}
|
||||
inline bool c2(){
|
||||
FOR(i, pos + 1, N) if (__c(req[i])) return true;
|
||||
return false;
|
||||
}
|
||||
inline bool c3(){
|
||||
DWN(i, pos, 0) if (__c(req[i])) return true;
|
||||
return false;
|
||||
}
|
||||
inline void pc(){
|
||||
++t; REP(i, SZ(R[t])) req[R[t][i].fi].PB(R[t][i].se);
|
||||
}
|
||||
inline void pt(){
|
||||
printf("%02d:%02d ", t / 60, t % 60);
|
||||
}
|
||||
void op(){
|
||||
pt(); puts("The elevator door is opening."); open = true;
|
||||
pc();
|
||||
}
|
||||
void cz(){
|
||||
pt(); puts("The elevator door is closing."); open = false;
|
||||
pc();
|
||||
}
|
||||
void mv(){
|
||||
if (anchor){
|
||||
pt(); if (!dir) printf("The elevator starts to move up from floor %d.\n", pos);
|
||||
else printf("The elevator starts to move down from floor %d.\n", pos);
|
||||
anchor = false;
|
||||
}
|
||||
if (dir) --pos; else ++pos;
|
||||
pc();
|
||||
}
|
||||
inline void rt(bool _dir){
|
||||
if (idle) idle = false, anchor = true;
|
||||
dir = _dir, mv();
|
||||
}
|
||||
void stop(){
|
||||
pt(); printf("The elevator stops at floor %d.\n", pos);
|
||||
anchor = true;
|
||||
}
|
||||
void enter(){
|
||||
if (!open) op(); if (idle == true) idle = false, dir = false, dir = !_c(req[pos]), anchor = true; pt(); int t = 0;
|
||||
ECH(it, req[pos]) if ((*it > pos) ^ dir) ++cnt[*it], req[pos].erase(it--), ++t;
|
||||
printf("%d people enter the elevator.\n", t); pc();
|
||||
}
|
||||
void leave(){
|
||||
if (!open) op();
|
||||
pt(); printf("%d people leave the elevator.\n", cnt[pos]);
|
||||
cnt[pos] = 0; pc();
|
||||
if (!dir && !c0() && !c2()) dir = true;
|
||||
else if (dir && !c1() && !c3()) dir = false;
|
||||
}
|
||||
void run(){
|
||||
pc(); while (true){
|
||||
if (idle){
|
||||
if (__c(req[pos])) enter();
|
||||
else if (c2()) rt(0);
|
||||
else if (c3()) rt(1);
|
||||
else if (t <= end_time) pc();
|
||||
else break;
|
||||
}else if (anchor){
|
||||
if (cnt[pos]) leave();
|
||||
else if (_c(req[pos])) enter();
|
||||
else if (open) cz();
|
||||
else{
|
||||
if (dir){
|
||||
if (c1() || c3()) rt(1);
|
||||
else if (c0() || c2()) rt(0);
|
||||
}
|
||||
else {
|
||||
if (c0() || c2()) rt(0);
|
||||
else if (c1() || c3()) rt(1);
|
||||
}
|
||||
if (anchor) idle = true;
|
||||
}
|
||||
}else{
|
||||
if (cnt[pos] || _c(req[pos])) stop();
|
||||
else {
|
||||
if (dir && !c1() && !c3()) stop(), idle = true;
|
||||
else if (!dir && !c0() && !c2()) stop(), idle = true;
|
||||
else mv();
|
||||
}
|
||||
}
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
} E;
|
||||
int main(){
|
||||
Rush{
|
||||
printf("Case %d:\n", ++____Case);
|
||||
E.init(RD()); CLR(R); end_time = 0; Rush{
|
||||
int time, from, to; RD(time, from, to);
|
||||
R[time].PB(MP(from, to));
|
||||
checkMax(end_time, time);
|
||||
}
|
||||
E.run();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
0 0
|
||||
0 1
|
||||
1 0
|
|
@ -0,0 +1,37 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#define len 1000005
|
||||
int ans[len],head[len],next[len],val[len],degree[len],n,m;
|
||||
bool toposort(){
|
||||
int i,front,rear,u;
|
||||
for(i=rear=1;i<=n;i++)
|
||||
if(degree[i]==0)
|
||||
ans[rear++]=i;
|
||||
for(front=1;front<rear;front++){
|
||||
for(u=head[ans[front]];u;u=next[u])
|
||||
if(--degree[val[u]]==0)
|
||||
ans[rear++]=val[u];
|
||||
}
|
||||
if(rear==n+1) return true;
|
||||
else return false;
|
||||
}
|
||||
int main(void){
|
||||
int i,x,y,k;
|
||||
while(scanf("%d%d",&n,&m)==2&&m+n){
|
||||
memset(head,0,sizeof(head));
|
||||
memset(degree,0,sizeof(degree));
|
||||
for(i=k=1;i<=m;i++){
|
||||
scanf("%d%d",&x,&y);
|
||||
degree[y]++;
|
||||
val[k]=y;
|
||||
next[k]=head[x]; head[x]=k++;
|
||||
}
|
||||
if(toposort())
|
||||
for(i=1;i<=n;i++)
|
||||
printf("%d\n",ans[i]);
|
||||
else
|
||||
puts("IMPOSSIBLE");
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#include<iostream>
|
||||
using namespace std;
|
||||
int f(int x)
|
||||
{
|
||||
int sum=0;
|
||||
while(x)
|
||||
{
|
||||
x=x/10;
|
||||
sum++;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
string a;
|
||||
while(cin>>a)
|
||||
{
|
||||
if(a[0]=='E'){break;}
|
||||
int l=a.size();
|
||||
if(l==1&&a[0]=='1'){cout<<1<<endl;continue;}
|
||||
for(int i=1;;i++)
|
||||
{
|
||||
if(l==f(l))
|
||||
{
|
||||
cout<<i+1<<endl;break;
|
||||
}
|
||||
else
|
||||
{
|
||||
l=f(l);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
#include <cstdio>
|
||||
#include <cstring>
|
||||
const int N = 100010;
|
||||
int n, root, f[N], l[N], r[N], d[N], w[N];
|
||||
int max(int x, int y)
|
||||
{
|
||||
return x > y ? x : y;
|
||||
}
|
||||
void init(int u)
|
||||
{
|
||||
if(u == 0) return;
|
||||
init(l[u]);
|
||||
init(r[u]);
|
||||
d[u] = max(d[l[u]], d[r[u]])+1;
|
||||
}
|
||||
void dp(int x, int <, int &rt)
|
||||
{
|
||||
int p;
|
||||
p = f[x];
|
||||
if(x == root) return;
|
||||
if(l[p] == x)
|
||||
{
|
||||
w[p] = max(lt+1, max(rt, d[r[p]])+2);
|
||||
rt = max(rt, d[r[p]])+1;
|
||||
}
|
||||
else
|
||||
{
|
||||
w[p] = max(rt+1, max(d[l[p]], lt)+2);
|
||||
lt = max(d[l[p]], lt)+1;
|
||||
}
|
||||
dp(p, lt, rt);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i, a, b;
|
||||
while(scanf("%d", &n) != EOF)
|
||||
{
|
||||
if(n == 0) break;
|
||||
for(i = 1; i <= n; i++) f[i] = i;
|
||||
for(i = 1; i <= n; i++)
|
||||
{
|
||||
scanf("%d%d", &a, &b);
|
||||
f[a] = f[b] = i;
|
||||
l[i] = a;
|
||||
r[i] = b;
|
||||
}
|
||||
root = 1;
|
||||
while(f[root] != root) root = f[root];
|
||||
d[0] = 0;
|
||||
init(root);
|
||||
for(i = 1; i <= n; i++)
|
||||
{
|
||||
w[0] = 0;
|
||||
a = d[l[i]];
|
||||
b = d[r[i]];
|
||||
if(i == root) w[root] = max(d[l[root]], d[r[root]])+1;
|
||||
else dp(i, a, b);
|
||||
printf("%d\n", w[root]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue