URAL
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 int main(){ 5 int x,y,c; 6 while(~scanf("%d%d%d",&x,&y,&c)){ 7 if(x+y<c){ 8 puts("Impossible"); 9 continue; 10 } 11 int a,b; 12 bool s=false; 13 if(x>y){ 14 s=true; 15 swap(x,y); 16 } 17 if(x>=c){ 18 a=c; 19 b=0; 20 } 21 else{ 22 a=x; 23 b=c-x; 24 } 25 if(s) swap(a,b); 26 printf("%d %d\n",a,b); 27 } 28 return 0; 29 }
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 const int M=1e5+10; 7 struct G { 8 struct E { 9 int v,next; 10 } e[M<<1]; 11 int le,head[M]; 12 void init() { 13 le=0; 14 mt(head,-1); 15 } 16 void add(int u,int v) { 17 e[le].v=v; 18 e[le].next=head[u]; 19 head[u]=le++; 20 } 21 } g; 22 bool vis[M]; 23 void bfs(int s,int d[]) { 24 mt(vis,0); 25 vis[s]=true; 26 queue<int> q; 27 while(!q.empty()) q.pop(); 28 q.push(s); 29 while(!q.empty()) { 30 int u=q.front(); 31 q.pop(); 32 for(int i=g.head[u]; ~i; i=g.e[i].next) { 33 int v=g.e[i].v; 34 if(!vis[v]) { 35 vis[v]=true; 36 d[v]=d[u]+1; 37 q.push(v); 38 } 39 } 40 } 41 } 42 int sd[M],rd[M],d[M],ans; 43 struct Q { 44 int step,val,id; 45 } now,pre; 46 queue<Q> q; 47 void solve(int s,int f) { 48 ans=0; 49 mt(d,0); 50 now.step=0; 51 now.id=s; 52 now.val=rd[s]; 53 while(!q.empty()) q.pop(); 54 q.push(now); 55 while(!q.empty()) { 56 pre=q.front(); 57 q.pop(); 58 if(pre.id==f) { 59 ans=max(ans,pre.val); 60 continue; 61 } 62 int u=pre.id; 63 for(int i=g.head[u]; ~i; i=g.e[i].next) { 64 int v=g.e[i].v; 65 now.val=min(pre.val,rd[v]); 66 now.step=pre.step+1; 67 if(now.step==sd[v]&&d[v]<now.val) { 68 d[v]=now.val; 69 now.id=v; 70 q.push(now); 71 } 72 } 73 } 74 } 75 int main() { 76 int n,m,u,v,s,f,r; 77 while(~scanf("%d%d",&n,&m)) { 78 g.init(); 79 while(m--) { 80 scanf("%d%d",&u,&v); 81 g.add(u,v); 82 g.add(v,u); 83 } 84 scanf("%d%d%d",&s,&f,&r); 85 bfs(s,sd); 86 bfs(r,rd); 87 solve(s,f); 88 printf("%d\n",ans); 89 } 90 return 0; 91 }
vector存图会慢一些
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<vector> 5 #define mt(a,b) memset(a,b,sizeof(a)) 6 using namespace std; 7 const int M=1e5+10; 8 vector<int> g[M]; 9 bool vis[M]; 10 void bfs(int s,int d[]) { 11 mt(vis,0); 12 vis[s]=true; 13 queue<int> q; 14 while(!q.empty()) q.pop(); 15 q.push(s); 16 while(!q.empty()) { 17 int u=q.front(); 18 q.pop(); 19 int len=g[u].size(); 20 for(int i=0; i<len; i++) { 21 int v=g[u][i]; 22 if(!vis[v]) { 23 vis[v]=true; 24 d[v]=d[u]+1; 25 q.push(v); 26 } 27 } 28 } 29 } 30 int sd[M],rd[M],d[M],ans; 31 struct Q { 32 int step,val,id; 33 } now,pre; 34 queue<Q> q; 35 void solve(int s,int f) { 36 ans=0; 37 mt(d,0); 38 now.step=0; 39 now.id=s; 40 now.val=rd[s]; 41 while(!q.empty()) q.pop(); 42 q.push(now); 43 while(!q.empty()) { 44 pre=q.front(); 45 q.pop(); 46 if(pre.id==f) { 47 ans=max(ans,pre.val); 48 continue; 49 } 50 int u=pre.id; 51 int len=g[u].size(); 52 for(int i=0; i<len; i++) { 53 int v=g[u][i]; 54 now.val=min(pre.val,rd[v]); 55 now.step=pre.step+1; 56 if(now.step==sd[v]&&d[v]<now.val) { 57 d[v]=now.val; 58 now.id=v; 59 q.push(now); 60 } 61 } 62 } 63 } 64 int main() { 65 int n,m,u,v,s,f,r; 66 while(~scanf("%d%d",&n,&m)) { 67 for(int i=1;i<=n;i++) g[i].clear(); 68 while(m--) { 69 scanf("%d%d",&u,&v); 70 g[u].push_back(v); 71 g[v].push_back(u); 72 } 73 scanf("%d%d%d",&s,&f,&r); 74 bfs(s,sd); 75 bfs(r,rd); 76 solve(s,f); 77 printf("%d\n",ans); 78 } 79 return 0; 80 }
手机排名按照出现次数,次数相同按照最低价格
1 #include<iostream> 2 #include<algorithm> 3 #include<vector> 4 #include<map> 5 using namespace std; 6 string a,b; 7 map<string,int> num,val; 8 map<string,int>::iterator it; 9 struct G{ 10 string a; 11 int num,val; 12 friend bool operator <(const G &a,const G &b){ 13 return a.num>b.num||(a.num==b.num&&a.val<b.val); 14 } 15 }now; 16 vector<G> g; 17 int cost; 18 int main(){ 19 num.clear(); 20 val.clear(); 21 for(int i=0;i<6;i++){ 22 cin>>a>>b>>cost; 23 num[b]++; 24 if(val[b]){ 25 val[b]=min(val[b],cost); 26 } 27 else{ 28 val[b]=cost; 29 } 30 } 31 g.clear(); 32 for(it=num.begin();it!=num.end();it++){ 33 now.a=it->first; 34 now.num=it->second; 35 now.val=val[now.a]; 36 g.push_back(now); 37 } 38 sort(g.begin(),g.end()); 39 cout<<g[0].a<<endl; 40 return 0; 41 }
最多4个连续的能满足的 88 89 90 91
1 #include<cstdio> 2 int main(){ 3 int n,ans[]={16,06,68,88}; 4 while(~scanf("%d",&n)){ 5 if(n<5){ 6 for(int i=0;i<n;i++){ 7 printf("%02d ",ans[i]); 8 } 9 } 10 else{ 11 printf("Glupenky Pierre"); 12 } 13 puts(""); 14 } 15 return 0; 16 }
汗诺塔,一开始都在A ,要移动成输入的状态,需要几步,每次都是考虑最下面的一个,然后相当于把n-1个移开,然后把第n个移动到目标处,移动n个需要2^n-1,把最后一个放到目标需要1
1 #include<cstdio> 2 typedef long long LL; 3 const int M=55; 4 char a[M]; 5 LL p[M]; 6 int main(){ 7 p[0]=1; 8 for(int i=1;i<M;i++){ 9 p[i]=p[i-1]*2; 10 } 11 int n; 12 while(~scanf("%d%s",&n,a)){ 13 LL ans=0; 14 int id=0; 15 for(int i=n;i>=1;i--){ 16 int his=a[i-1]-'A'; 17 if(his==id) continue; 18 ans+=p[i-1]; 19 id=0^1^2^id^his; 20 } 21 printf("%lld\n",ans); 22 } 23 return 0; 24 }
1 #include<cstdio> 2 int main(){ 3 int t,n,k,ans; 4 while(~scanf("%d",&t)){ 5 while(t--){ 6 scanf("%d%d",&n,&k); 7 int x=n/k; 8 int y=n%k; 9 ans=y*(x+1)*(n-x-1)+(k-y)*x*(n-x); 10 printf("%d\n",ans/2); 11 } 12 } 13 return 0; 14 }
最多存在k种不同字母,问最多几个,有几种方法达到最多
最多几个就是把前k种加起来
方法数就是有几种个数和第k种一样,那么从中选几种
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define mt(a,b) memset(a,b,sizeof(a)) 5 using namespace std; 6 const int M=1e5+10; 7 char a[M]; 8 int num[32]; 9 int C[32][32]; 10 int main(){ 11 for(int i=0;i<32;i++){ 12 C[i][i]=1; 13 C[i][0]=1; 14 } 15 for(int i=1;i<32;i++){ 16 for(int j=1;j<i;j++){ 17 C[i][j]=C[i-1][j]+C[i-1][j-1]; 18 } 19 } 20 int k; 21 while(~scanf("%s%d",a,&k)){ 22 mt(num,0); 23 for(int i=0;a[i];i++){ 24 num[a[i]-'a']++; 25 } 26 sort(num,num+26); 27 int sum=0; 28 int kk=0; 29 for(int i=25;i>=0;i--){ 30 sum+=num[i]; 31 kk++; 32 if(kk==k) break; 33 } 34 printf("%d ",sum); 35 if(k==26){ 36 puts("1"); 37 continue; 38 } 39 if(num[26-k]==0){ 40 puts("1"); 41 continue; 42 } 43 if(num[26-k-1]!=num[26-k]){ 44 puts("1"); 45 continue; 46 } 47 int cn=0,cm=0; 48 for(int i=0;i<26;i++){ 49 if(num[i]==num[26-k]){ 50 cn++; 51 } 52 } 53 for(int i=26-k;i<26;i++){ 54 if(num[i]==num[26-k]){ 55 cm++; 56 } 57 } 58 printf("%d\n",C[cn][cm]); 59 } 60 return 0; 61 }
end