2018 ACM 网络选拔赛 沈阳赛区
B. Call of Accepted
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define nl 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e2+10; 25 26 /** 27 + - 3 28 * / 2 29 d 1 30 ( ) 0 31 **/ 32 33 int xl[maxn],xr[maxn],y[maxn]; 34 char s[maxn],z[maxn]; 35 36 int main() 37 { 38 int len,i,g1,g2,a,b,c,d,sym; 39 while (~scanf("%s",s)) 40 { 41 len=strlen(s); 42 g1=0; 43 g2=0; 44 y[0]=inf; 45 for (i=0;i<len;i++) 46 if (s[i]>='0' && s[i]<='9') 47 { 48 g1++; 49 xl[g1]=0; 50 while (s[i]>='0' && s[i]<='9') 51 { 52 xl[g1]=xl[g1]*10+s[i]-48; 53 i++; 54 } 55 i--; 56 xr[g1]=xl[g1]; 57 } 58 else 59 { 60 switch(s[i]) 61 { 62 case '(': 63 sym=0; 64 break; 65 case ')': 66 sym=0; 67 break; 68 case 'd': 69 sym=1; 70 break; 71 case '*': 72 sym=2; 73 break; 74 case '/': 75 sym=2; 76 break; 77 case '+': 78 sym=3; 79 break; 80 case '-': 81 sym=3; 82 break; 83 } 84 while (y[g2]<=sym || s[i]==')') 85 { 86 switch(z[g2]) 87 { 88 case 'd': 89 ///ldr l>=0,r>=1,else illegal 90 ///正数,负数 91 xl[g1-1]=max(0,xl[g1-1]); 92 xl[g1]=max(1,xl[g1]); 93 xr[g1-1]*=xr[g1]; 94 break; 95 case '+': 96 xl[g1-1]+=xl[g1]; 97 xr[g1-1]+=xr[g1]; 98 break; 99 case '-': 100 xl[g1-1]-=xr[g1]; 101 xr[g1-1]-=xl[g1]; 102 break; 103 case '*': 104 a=xl[g1-1]*xl[g1]; 105 b=xl[g1-1]*xr[g1]; 106 c=xr[g1-1]*xl[g1]; 107 d=xr[g1-1]*xr[g1]; 108 xl[g1-1]=min(min(a,b),min(c,d)); 109 xr[g1-1]=max(max(a,b),max(c,d)); 110 break; 111 case '/': 112 a=xl[g1-1]/xl[g1]; 113 b=xl[g1-1]/xr[g1]; 114 c=xr[g1-1]/xl[g1]; 115 d=xr[g1-1]/xr[g1]; 116 xl[g1-1]=min(min(a,b),min(c,d)); 117 xr[g1-1]=max(max(a,b),max(c,d)); 118 break; 119 case '(': 120 break; 121 } 122 if (z[g2]=='(') 123 break; 124 g2--; 125 g1--; 126 } 127 if (s[i]==')') 128 g2--; 129 else 130 { 131 y[++g2]=sym; 132 z[g2]=s[i]; 133 } 134 } 135 136 while (g2!=0) 137 { 138 switch(z[g2]) 139 { 140 case 'd': 141 xr[g1-1]*=xr[g1]; 142 break; 143 case '+': 144 xl[g1-1]+=xl[g1]; 145 xr[g1-1]+=xr[g1]; 146 break; 147 case '-': 148 xl[g1-1]-=xr[g1]; 149 xr[g1-1]-=xl[g1]; 150 break; 151 case '*': 152 a=xl[g1-1]*xl[g1]; 153 b=xl[g1-1]*xr[g1]; 154 c=xr[g1-1]*xl[g1]; 155 d=xr[g1-1]*xr[g1]; 156 xl[g1-1]=min(min(a,b),min(c,d)); 157 xr[g1-1]=max(max(a,b),max(c,d)); 158 break; 159 case '/': 160 a=xl[g1-1]/xl[g1]; 161 b=xl[g1-1]/xr[g1]; 162 c=xr[g1-1]/xl[g1]; 163 d=xr[g1-1]/xr[g1]; 164 xl[g1-1]=min(min(a,b),min(c,d)); 165 xr[g1-1]=max(max(a,b),max(c,d)); 166 break; 167 case '(': 168 break; 169 } 170 g2--; 171 g1--; 172 } 173 174 printf("%d %d\n",xl[1],xr[1]); 175 } 176 177 return 0; 178 } 179 /* 180 3*(2d3) 181 1+1d2d3d4d5+2 182 183 (2d3)*(1+3*3d4)d5 184 185 (2+(3*4+5d1d3)d2)d3 186 187 188 189 (10-30)*3d4 190 191 (10-3d4)*2d3 192 193 (10-3d4)*(10-3d4) 194 195 (10-5d5)*(10-5d5) 196 197 3d(10-3d4) 198 3d(10-3d4)d(10-3d4) 199 */
D. Made In Heaven
F. Fantastic Graph
详细题解:https://blog.csdn.net/qq_40993793/article/details/82626562
判断是否存在可行流(满足下界条件):
汇点T -> 源点S inf(如果有流,则源源不断增加)
超级源点SS -> 超级汇点TT 判断是否满流,值为点TT的入度
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <ctime> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <set> 9 #include <list> 10 #include <queue> 11 #include <stack> 12 #include <vector> 13 #include <bitset> 14 #include <algorithm> 15 #include <iostream> 16 using namespace std; 17 #define ll long long 18 const int maxn=4e3+10; 19 const int inf=1e9; 20 21 struct node 22 { 23 int d,len; 24 node *next,*opp; 25 }*e[maxn]; 26 27 int sum=0,s,t; 28 int q[maxn],dep[maxn]; 29 bool vis[maxn]; 30 31 void add_edge(int x,int y,int len) 32 { 33 node *p1=(node*) malloc (sizeof(node)); 34 node *p2=(node*) malloc (sizeof(node)); 35 36 p1->d=y; 37 p1->len=len; 38 p1->next=e[x]; 39 p1->opp=p2; 40 e[x]=p1; 41 42 p2->d=x; 43 p2->len=0; 44 p2->next=e[y]; 45 p2->opp=p1; 46 e[y]=p2; 47 } 48 49 bool bfs() 50 { 51 int head=0,tail=1,d,dd; 52 node *p; 53 memset(vis,0,sizeof(vis)); 54 vis[s]=1; 55 dep[s]=1; 56 q[1]=s; 57 58 while (head<tail) 59 { 60 head++; 61 d=q[head]; 62 p=e[d]; 63 while (p) 64 { 65 dd=p->d; 66 if (p->len>0 && !vis[dd]) 67 { 68 tail++; 69 q[tail]=dd; 70 vis[dd]=1; 71 dep[dd]=dep[d]+1; 72 } 73 p=p->next; 74 } 75 } 76 if (vis[t]) 77 return 1; 78 return 0; 79 } 80 81 int dfs(int d,int add) 82 { 83 if (!add || d==t) 84 return add; 85 int totf=0,f,dd; 86 node *p=e[d]; 87 while (p) 88 { 89 dd=p->d; 90 if (dep[dd]==dep[d]+1 && (f=dfs(dd,min(add,p->len)))>0) 91 { 92 totf+=f; 93 add-=f;///зЂвт 94 p->len-=f; 95 p->opp->len+=f; 96 } 97 p=p->next; 98 } 99 return totf; 100 } 101 102 int main() 103 { 104 int n,m,k,l,r,x,y,i,T=0; 105 106 while (~scanf("%d%d%d",&n,&m,&k)) 107 { 108 scanf("%d%d",&l,&r); 109 s=n+m+2,t=n+m+3; 110 for (i=1;i<=t;i++) 111 e[i]=NULL; 112 113 for (i=1;i<=n;i++) 114 add_edge(n+m+2,i,l); 115 add_edge(0,n+m+3,l*n); 116 for (i=1;i<=n;i++) 117 add_edge(0,i,r-l); 118 119 add_edge(n+m+2,n+m+1,l*m); 120 for (i=n+1;i<=n+m;i++) 121 add_edge(i,n+m+3,l); 122 for (i=n+1;i<=n+m;i++) 123 add_edge(i,n+m+1,r-l); 124 125 add_edge(n+m+1,0,inf); 126 127 while (k--) 128 { 129 scanf("%d%d",&x,&y); 130 add_edge(x,n+y,1); 131 } 132 133 sum=0; 134 while (bfs()) 135 sum+=dfs(s,inf); 136 printf("Case %d: ",++T); 137 if (sum==(n+m)*l) 138 printf("Yes\n"); 139 else 140 printf("No\n"); 141 } 142 return 0; 143 }
G. Spare Tire
I. Lattice's basics in digital electronics
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define nl 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e5+10; 25 26 int tr[1000000]; 27 char s1[200010*10],s2[200010*10],s3[200010*10],s4[200010*10]; 28 char s[200010*10]; 29 30 int main() 31 { 32 int t,m,n,a,i,j,k,len,now,g,value; 33 scanf("%d",&t); 34 while (t--) 35 { 36 memset(tr,0,sizeof(tr)); 37 scanf("%d%d",&m,&n); 38 for (i=1;i<=n;i++) 39 { 40 scanf("%d%s",&a,s); 41 len=strlen(s); 42 k=1; 43 for (j=0;j<len;j++) 44 k=(k<<1)+s[j]-48; 45 tr[k]=a; 46 } 47 48 scanf("%s",s1); 49 len=strlen(s1); 50 j=0; 51 for (i=0;i<len;i++) 52 { 53 if (s1[i]>='0' && s1[i]<='9') 54 value=s1[i]-48; 55 else if (s1[i]>='A' && s1[i]<='F') 56 value=s1[i]-55; 57 else 58 value=s1[i]-87; 59 60 s2[j++]=value/8+48; 61 value%=8; 62 s2[j++]=value/4+48; 63 value%=4; 64 s2[j++]=value/2+48; 65 value%=2; 66 s2[j++]=value+48; 67 } 68 s2[j]='\0'; 69 len=j; 70 71 now=0; 72 for (i=0;i<len/9;i++) 73 { 74 g=0; 75 j=i*9; 76 for (k=j;k<j+8;k++) 77 g+=s2[k]-48; 78 if ((g+1)%2==s2[k]-48) 79 { 80 for (k=j;k<j+8;k++) 81 s3[now++]=s2[k]; 82 } 83 } 84 s3[now]='\0'; 85 86 j=1; 87 len=0; 88 for (i=0;i<now;i++) 89 { 90 j=j*2+s3[i]-48; 91 if (j>10000) 92 continue; 93 if (tr[j]) 94 { 95 s4[len++]=tr[j]; 96 if (len==m) 97 break; 98 j=1; 99 } 100 } 101 s4[len]='\0'; 102 printf("%s\n",s4); 103 } 104 return 0; 105 } 106 /* 107 10 108 8 3 109 49 0001 110 50 01001 111 51 011 112 14DB24722698 113 114 115 2 116 15 9 117 32 0100 118 33 11 119 100 1011 120 101 0110 121 104 1010 122 108 00 123 111 100 124 114 0111 125 119 0101 126 908 127 128 100 129 100 2 130 65 010 131 66 1011 132 AAAAAA 133 */
K. Supreme Number
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define nl 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e5+10; 25 26 typedef pair<int,int> P; 27 28 char str[maxn]; 29 int g=0,b,a[maxn]; 30 int c[5]={1,2,3,5,7}; 31 int shi[10]; 32 33 bool prime(int t) 34 { 35 int i; 36 for (i=2;i<=sqrt(t);i++) 37 if (t%i==0) 38 return 0; 39 return 1; 40 } 41 42 void dfs(int s,int w,vector<P>v) 43 { 44 vector<P>::iterator j; 45 vector<P>vv; 46 int t,i; 47 48 for (i=0;i<5;i++) 49 { 50 51 // if (shi[w]*c[i]+s==711) 52 // printf("z"); 53 54 for (j=v.begin();j!=v.end();j++) 55 { 56 if (!prime(j->first+shi[j->second]*c[i])) 57 break; 58 } 59 60 if (j==v.end()) 61 { 62 t=shi[w]*c[i]+s; 63 vv.clear(); 64 for (j=v.begin();j!=v.end();j++) 65 { 66 vv.push_back(make_pair(j->first,j->second)); 67 vv.push_back(make_pair(j->first+shi[j->second]*c[i],j->second+1)); 68 } 69 vv.push_back(make_pair(c[i],1)); 70 71 // printf("%d:\n",t); 72 // for (j=vv.begin();j!=vv.end();j++) 73 // printf("%d\n",j->first); 74 75 g++; 76 a[g]=t; 77 dfs(t,w+1,vv); 78 } 79 } 80 } 81 82 int main() 83 { 84 vector<P>v; 85 int t,T,i; 86 // cout<<pow(10,0)<<endl; 87 // cout<<pow(10,1)<<endl; 88 // cout<<pow(10,2)<<endl; 89 // 90 // printf("\n"); 91 // 92 // printf("%f\n",pow(10,0)); 93 // printf("%f\n",pow(10,1)); 94 // printf("%f\n",pow(10,2)); 95 // 96 // printf("\n"); 97 // 98 // printf("%d\n",pow(10,0)); 99 // printf("%d\n",pow(10,1)); 100 // printf("%d\n",pow(10,2)); 101 // 102 // printf("\n"); 103 // 104 // printf("%d\n",(int)pow(10,0)); 105 // printf("%d\n",(int)pow(10,1)); 106 // printf("%d\n",(int)pow(10,2)); 107 // 108 // printf("\n"); 109 // 110 // printf("\n"); 111 // 112 // printf("%f\n",log(2)/log(2)); 113 // printf("%f\n",log(4)/log(2)); 114 // printf("%f\n",log(8)/log(2)); 115 // printf("%f\n",log(16)/log(2)); 116 // 117 // printf("%d\n",(int)(log(2)/log(2))); 118 // printf("%d\n",(int)(log(4)/log(2))); 119 // printf("%d\n",(int)(log(8)/log(2))); 120 // printf("%d\n",(int)(log(16)/log(2))); 121 // 122 // printf("\n"); 123 // 124 // printf("%d\n",(int)log(2)/log(2)); 125 // printf("%d\n",(int)log(4)/log(2)); 126 // printf("%d\n",(int)log(8)/log(2)); 127 // printf("%d\n",(int)log(16)/log(2)); 128 // return 0; 129 130 shi[0]=1; 131 for (i=1;i<=9;i++) 132 shi[i]=shi[i-1]*10; 133 134 v.push_back(make_pair(0,0)); 135 dfs(0,0,v); 136 sort(a+1,a+g+1); 137 138 // for (i=1;i<=g;i++) 139 // printf("%d ",a[i]); 140 141 scanf("%d",&t); 142 for (T=1;T<=t;T++) 143 { 144 scanf("%str",str); 145 if (strlen(str)>9) 146 { 147 printf("Case #%d: %d\n",T,a[g]); 148 continue; 149 } 150 b=atoi(str); 151 for (i=g;i>=1;i--) 152 if (b>=a[i]) 153 break; 154 printf("Case #%d: %d\n",T,a[i]); 155 } 156 return 0; 157 }