Codeforces Beta Round #3

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-08-26 19:40:03

Codeforces Beta Round #3的相关文章

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

CodeForces Beta Round #1

Codeforces Beta Round #1 A. Theatre Square [题意]一个n*m的矩形广场,用a*a的方形石板铺设,问最少需要多少块石板能铺满广场. [思路]水题,从n方向来看能能够铺设ceil(n/a)块,从m方向来看能能够铺设ceil(m/a)块,总共有ceil(n/a)*ceil(m/a)块. 1 /* 2 ** CodeForces 1A Theatre Square 3 ** Created by Rayn @@ 2014/05/18 4 */ 5 #inclu

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

Codeforces Beta Round #1 B. Spreadsheets

Codeblocks坏掉了,我不知道该怎么修,只能过两天重装系统了. 没办法.这个题是用Java写的,代码风格不好不要骂我~~ 题目大意: Excel表格那种坐标系统,和正常的坐标系统.用代码实现转换. 就是模拟题啊,代码量比较小. 下面是代码: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); i

Codeforces Beta Round #1 C. Ancient Berland Circus

果然Java还是不靠谱啊,一个NaN把我整了半天~~ 题目大意: 有一个正多边形,给出任意三个顶点的坐标,求这个正多边形的最小面积. 解题思路: 首先要知道这三个顶点组成的三角形的外接圆一定是这个正多边形的外接圆. 用过计算出三角形的三边长,可以计算出三角型面积,进而推出外接圆半径. 可以得到三个圆心角,找出最大公约数,那就是最大角度. 就可以计算出多边形面积了~~ 下面是代码: import java.text.DecimalFormat; import java.util.Scanner;

codeforces Beta Round #19 D. Point (线段树 + set)

题目大意: 对平面上的点进行操作. add x y 在 (x,y )上加一个点. remove x y 移除 (x,y)上的点. find x y 求出在(x,y)右上角离他最近的点,优先级是靠左,靠下. 思路分析: find 操作 比较麻烦. 要保证x大的同时还要确保x最小,而且该x上还要有点. 这样要找大的时候要小的,就是在线段树上选择性的进入左子树还是右子树. 所以核心就是,用set维护叶子节点. 然后查找的时候去叶子节点找,如果这个叶子节点有蛮子的 x y  就输出,否则回溯去另外一个子

Codeforces Beta Round #12 D. Ball (线段树)

题目大意: n个女性中,如果有一个女性的三维比这个女性的三维都大,这个女性就要自杀.问要自杀多少个. 思路分析: 先按照第一维排序. 然后离散化第二维,用第二维的下标建树,树上的值是第三维,更新的时候取最大值. 注意是按照第一维度从大到小进入线段树. 而且还要严格递增. 所以处理第一维度比较大小的时候要分开处理,要把相同的先判断,再更新入树. 那么如何判断这个女性是否自杀. 首先,你知道第一维度是从大到小的,所以先进树了的节点的第一维度一定更大. 再次,我们考虑第二维度,我们去树上第二维度下标大