Codeforces Beta Round #3
http://codeforces.com/contest/3
A
找规律题。但我懒得找,直接暴搜
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*#ifndef ONLINE_JUDGE 8 freopen("1.txt","r",stdin); 9 #endif */ 10 11 int book[15][15]; 12 struct sair{ 13 string str[105]; 14 int co; 15 int x,y; 16 }; 17 18 int dir[8][2]={0,1,1,0,0,-1,-1,0,1,1,-1,-1,1,-1,-1,1}; 19 string D[8]={"R","D","L","U","RD","LU","LD","RU"}; 20 21 sair bfs(sair s){ 22 queue<sair>Q; 23 Q.push(s); 24 sair e; 25 while(!Q.empty()){ 26 s=Q.front(); 27 Q.pop(); 28 for(int i=0;i<8;i++){ 29 e=s; 30 e.x=s.x+dir[i][0]; 31 e.y=s.y+dir[i][1]; 32 if(e.x>=0&&e.x<8&&e.y>=0&&e.y<8&&book[e.x][e.y]!=1){ 33 e.str[e.co]=D[i]; 34 e.co++; 35 Q.push(e); 36 if(book[e.x][e.y]==2){ 37 return e; 38 } 39 book[e.x][e.y]=1; 40 } 41 } 42 } 43 } 44 45 int main(){ 46 #ifndef ONLINE_JUDGE 47 freopen("1.txt","r",stdin); 48 #endif 49 string str,str1; 50 cin>>str; 51 sair s; 52 book[7-(str[1]-‘1‘)][str[0]-‘a‘]=1; 53 s.y=str[0]-‘a‘,s.x=7-(str[1]-‘1‘),s.co=0; 54 cin>>str1; 55 book[7-(str1[1]-‘1‘)][str1[0]-‘a‘]=2; 56 if(str==str1) {cout<<0<<endl;return 0;} 57 s=bfs(s); 58 cout<<s.co<<endl; 59 for(int i=0;i<s.co;i++){ 60 cout<<s.str[i]<<endl; 61 } 62 }
B
贪心题
因为体积只有1和2两种,所以每次选单位价值最大的物品。需要注意最后容量剩一个的情况,这时候需要对比是选一个好还是把容量为1的物品删去一个再添加一个容量为2的物品好
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*#ifndef ONLINE_JUDGE 8 freopen("1.txt","r",stdin); 9 #endif */ 10 11 string str; 12 vector<pair<ll,int> >one,two; 13 14 bool cmp(pair<ll,int>a,pair<ll,int>b){ 15 if(a.first==b.first) return a.second<b.second; 16 return a.first>b.first; 17 } 18 19 int main(){ 20 #ifndef ONLINE_JUDGE 21 freopen("1.txt","r",stdin); 22 #endif 23 ll n,m; 24 cin>>n>>m; 25 int v; 26 ll val; 27 for(int i=1;i<=n;i++){ 28 cin>>v>>val; 29 if(v==1){ 30 one.push_back(make_pair(val,i)); 31 } 32 else{ 33 two.push_back(make_pair(val,i)); 34 } 35 } 36 sort(one.begin(),one.end(),cmp); 37 sort(two.begin(),two.end(),cmp); 38 int pos1=0,pos2=0; 39 ll ans=0; 40 while(pos1<one.size()||pos2<two.size()){ 41 // cout<<pos1<<" "<<pos2<<endl; 42 if(m>=2){ 43 if((two[pos2].first/2)>=one[pos1].first&&pos2<two.size()){ 44 ans+=two[pos2].first; 45 pos2++; 46 m-=2; 47 } 48 else if(one.size()>pos1){ 49 ans+=one[pos1].first; 50 pos1++; 51 m-=1; 52 } 53 else{ 54 ans+=two[pos2].first; 55 pos2++; 56 m-=2; 57 } 58 } 59 else if(m==1){ 60 if(pos2==two.size()){ans+=one[pos1].first;pos1++;} 61 else if(pos1==one.size()){ 62 if(pos1==0) 63 break; 64 else{ 65 pos1--; 66 ans-=one[pos1].first; 67 ans+=two[pos2].first; 68 pos2++; 69 } 70 } 71 else if(two[pos2].first>one[pos1-1].first+one[pos1].first){ 72 ans-=one[pos1-1].first; 73 pos1--; 74 ans+=two[pos2].first; 75 pos2++; 76 } 77 else{ 78 ans+=one[pos1].first; 79 pos1++; 80 } 81 m--; 82 } 83 else if(m==0) break; 84 } 85 cout<<ans<<endl; 86 vector<int>pos; 87 for(int i=0;i<pos1;i++) pos.push_back(one[i].second); 88 for(int i=0;i<pos2;i++) pos.push_back(two[i].second); 89 sort(pos.begin(),pos.end()); 90 for(int i=0;i<pos.size();i++){ 91 cout<<pos[i]<<" "; 92 } 93 cout<<endl; 94 }
C
井子棋
模拟判断各种情况就好
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*#ifndef ONLINE_JUDGE 8 freopen("1.txt","r",stdin); 9 #endif */ 10 11 string str[3]; 12 13 int Check(){ 14 int cha=0,quan=0; 15 if(str[0][0]==str[0][1]&&str[0][1]==str[0][2]&&str[0][0]==‘X‘) cha=1; 16 if(str[1][0]==str[1][1]&&str[1][1]==str[1][2]&&str[1][0]==‘X‘) cha=1; 17 if(str[2][0]==str[2][1]&&str[2][1]==str[2][2]&&str[2][0]==‘X‘) cha=1; 18 if(str[0][0]==str[1][1]&&str[1][1]==str[2][2]&&str[0][0]==‘X‘) cha=1; 19 if(str[0][0]==str[1][0]&&str[1][0]==str[2][0]&&str[0][0]==‘X‘) cha=1; 20 if(str[0][1]==str[1][1]&&str[1][1]==str[2][1]&&str[0][1]==‘X‘) cha=1; 21 if(str[0][2]==str[1][2]&&str[1][2]==str[2][2]&&str[0][2]==‘X‘) cha=1; 22 if(str[0][2]==str[1][1]&&str[1][1]==str[2][0]&&str[0][2]==‘X‘) cha=1; 23 24 if(str[0][0]==str[0][1]&&str[0][1]==str[0][2]&&str[0][0]==‘0‘) quan=1; 25 if(str[1][0]==str[1][1]&&str[1][1]==str[1][2]&&str[1][0]==‘0‘) quan=1; 26 if(str[2][0]==str[2][1]&&str[2][1]==str[2][2]&&str[2][0]==‘0‘) quan=1; 27 if(str[0][0]==str[1][1]&&str[1][1]==str[2][2]&&str[0][0]==‘0‘) quan=1; 28 if(str[0][0]==str[1][0]&&str[1][0]==str[2][0]&&str[0][0]==‘0‘) quan=1; 29 if(str[0][1]==str[1][1]&&str[1][1]==str[2][1]&&str[0][1]==‘0‘) quan=1; 30 if(str[0][2]==str[1][2]&&str[1][2]==str[2][2]&&str[0][2]==‘0‘) quan=1; 31 if(str[0][2]==str[1][1]&&str[1][1]==str[2][0]&&str[0][2]==‘0‘) quan=1; 32 if(quan&&cha) return -1; 33 if(cha) return 1; 34 if(quan) return 2; 35 return 0; 36 } 37 38 int main(){ 39 #ifndef ONLINE_JUDGE 40 // freopen("1.txt","r",stdin); 41 #endif 42 ///first second illegal the first player won the second player won draw 43 for(int i=0;i<3;i++) cin>>str[i]; 44 int cha=0,quan=0; 45 for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(str[i][j]==‘X‘) cha++; else if(str[i][j]==‘0‘) quan++; 46 if(abs(quan-cha)>=2||quan>cha) cout<<"illegal"<<endl; 47 else{ 48 int tmp=Check(); 49 if(tmp==-1) cout<<"illegal"<<endl; 50 else if(tmp==1) if(cha>quan) cout<<"the first player won"<<endl;else cout<<"illegal"<<endl; 51 else if(tmp==2) if(quan==cha) cout<<"the second player won"<<endl;else cout<<"illegal"<<endl; 52 else{ 53 if(quan+cha==9){ 54 cout<<"draw"<<endl; 55 } 56 else if(cha>quan) cout<<"second"<<endl; 57 else if(cha==quan) cout<<"first"<<endl; 58 } 59 } 60 }
D
括号匹配
个人感觉是道好题
先把所有的“?”变成“)”,然后当num<0时,把前面的右括号变成左括号,变的原则是b-a的最大值,最后判断是否括号匹配
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 typedef long long ll; 7 /*#ifndef ONLINE_JUDGE 8 freopen("1.txt","r",stdin); 9 #endif */ 10 struct sair{ 11 ll a,b; 12 int pos; 13 bool operator<(const sair&bb)const{ 14 return (b-a)<(bb.b-bb.a); 15 } 16 }; 17 18 int main(){ 19 #ifndef ONLINE_JUDGE 20 freopen("1.txt","r",stdin); 21 #endif 22 string str; 23 cin>>str; 24 ll a,b,ans=0; 25 priority_queue<sair>Q; 26 sair tmp; 27 int num=0; 28 for(int i=0;i<str.length();i++){ 29 if(str[i]==‘(‘){ 30 num++; 31 } 32 else if(str[i]==‘)‘){ 33 num--; 34 } 35 else{ 36 cin>>a>>b; 37 tmp.a=a,tmp.b=b,tmp.pos=i; 38 Q.push(tmp); 39 ans+=b; 40 str[i]=‘)‘; 41 num--; 42 } 43 while(num<0){ 44 if(Q.size()){ 45 ans-=Q.top().b-Q.top().a; 46 str[Q.top().pos]=‘(‘; 47 Q.pop(); 48 num+=2; 49 } 50 else{ 51 num=-0x3f3f3f3f; 52 break; 53 } 54 } 55 } 56 if(num!=0) cout<<-1<<endl; 57 else{ 58 cout<<ans<<endl; 59 cout<<str<<endl; 60 } 61 }
原文地址:https://www.cnblogs.com/Fighting-sh/p/10339466.html
时间: 2024-11-05 23:24:45