Codeforces Beta Round #4 (Div. 2 Only)

Codeforces Beta Round #4 (Div. 2 Only)

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     int n;
23     cin>>n;
24     if(n%2==0&&n!=2&&n!=0) cout<<"YES"<<endl;
25     else cout<<"NO"<<endl;
26 }
View Code

 

B

判断给定时间在不在最大值之和和最小值之和之间即可

 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 a[1005],b[1005];
19 
20 int main(){
21     #ifndef ONLINE_JUDGE
22      //   freopen("1.txt","r",stdin);
23     #endif
24     int n,m;
25     cin>>n>>m;
26     int Min=0,Max=0;
27     for(int i=0;i<n;i++){
28         cin>>a[i]>>b[i];
29         Min+=a[i];
30         Max+=b[i];
31     }
32     if(Min<=m&&m<=Max){
33         cout<<"YES"<<endl;
34         vector<int>ans;
35         for(int i=0;i<n;i++){
36             ans.push_back(a[i]);
37             m-=a[i];
38         }
39         for(int i=0;i<ans.size();i++){
40             if(m==0) break;
41             int tmp=b[i]-a[i];
42             if(m>tmp) m-=tmp;
43             else {tmp=m,m=0;}
44             ans[i]+=tmp;
45         }
46         for(int i=0;i<ans.size();i++){
47             cout<<ans[i]<<" ";
48         }
49         cout<<endl;
50     }
51     else{
52         cout<<"NO"<<endl;
53     }
54 
55 }
View Code

 

C

直接上map即可

 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 a[1005],b[1005];
19 
20 string Change(int x){
21     string str="";
22     while(x){
23         str+=char(x%10+'0');
24         x/=10;
25     }
26     for(int i=0;i<str.length()/2;i++){
27         char tmp=str[i];
28         str[i]=str[str.length()-1-i];
29         str[str.length()-1-i]=tmp;
30     }
31     return str;
32 }
33 
34 int main(){
35     #ifndef ONLINE_JUDGE
36         freopen("1.txt","r",stdin);
37     #endif
38     int n;
39     map<string,int>mp;
40     cin>>n;
41     string str;
42     for(int i=1;i<=n;i++){
43         cin>>str;
44         if(mp[str]==0){
45             cout<<"OK"<<endl;
46             mp[str]=1;
47         }
48         else{
49             string tmp=Change(mp[str]);
50             mp[str]++;
51             str+=tmp;
52             mp[str]=1;
53             cout<<str<<endl;
54         }
55     }
56 
57 }
View Code

 

D

找最长上升子序列,DP水题

因为是二维排序的关系,所以最后一个位置的值不一定是最大的,所以要遍历一遍数组找最大值

 

 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 n,w,h;
12 struct sair{
13     int w,h,pos;
14 }a[5005];
15 
16 bool cmp(sair aa,sair bb){
17     if(aa.h==bb.h) return aa.w<bb.w;
18     return aa.h<bb.h;
19 }
20 
21 int dp[5005],Index[5005];
22 
23 void dfs(int pos){
24     if(!a[pos].pos) return;
25     dfs(Index[pos]);
26     cout<<a[pos].pos<<" ";
27 }
28 
29 int main(){
30     #ifndef ONLINE_JUDGE
31       //  freopen("1.txt","r",stdin);
32     #endif
33     int tot;
34     cin>>tot>>a[1].w>>a[1].h;
35     int i;
36     n=1;
37     for(i=1;i<=tot;i++){
38         cin>>w>>h;
39         if(w>a[1].w&&h>a[1].h){
40             a[++n].w=w;
41             a[n].h=h;
42             a[n].pos=i;
43         }
44     }
45     sort(a+1,a+n+1,cmp);
46 
47     for(int i=2;i<=n;i++){
48         for(int j=1;j<=i;j++){
49             if(a[i].w>a[j].w&&a[i].h>a[j].h&&dp[i]<=dp[j]){
50                 dp[i]=dp[j]+1;
51                 Index[i]=j;
52             }
53         }
54     }
55     int Max=0;
56     int pos=0;
57     for(int i=1;i<=n;i++){
58         if(Max<dp[i]){
59             Max=dp[i];
60             pos=i;
61         }
62     }
63     cout<<dp[pos]<<endl;
64     dfs(pos);
65 }
View Code

 

当然,因为第一个比所有的都小,所以也可以从后往前DP,相当于DP最长下降子序列,这样下标为1的位置的值是最大的

 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 n,w,h;
12 struct sair{
13     int w,h,pos;
14 }a[5005];
15 
16 bool cmp(sair aa,sair bb){
17     if(aa.h==bb.h) return aa.w<bb.w;
18     return aa.h<bb.h;
19 }
20 
21 int dp[5005],Index[5005];
22 
23 void dfs(int pos){
24     if(!a[pos].pos) return;
25     dfs(Index[pos]);
26     cout<<a[pos].pos<<" ";
27 }
28 
29 int main(){
30     #ifndef ONLINE_JUDGE
31         freopen("1.txt","r",stdin);
32     #endif
33     int tot;
34     cin>>tot>>a[1].w>>a[1].h;
35     int i;
36     n=1;
37     for(i=1;i<=tot;i++){
38         cin>>w>>h;
39         if(w>a[1].w&&h>a[1].h){
40             a[++n].w=w;
41             a[n].h=h;
42             a[n].pos=i;
43         }
44     }
45     sort(a+1,a+n+1,cmp);
46     for(int i=n-1;i>=1;i--){
47         for(int j=i+1;j<=n;j++){
48             if(a[i].w<a[j].w&&a[i].h<a[j].h&&dp[i]<=dp[j]){
49                 dp[i]=dp[j]+1;
50                 Index[i]=j;
51             }
52         }
53     }
54     cout<<dp[1]<<endl;
55     int pos=Index[1];
56     while(pos){
57         cout<<a[pos].pos<<" ";
58         pos=Index[pos];
59     }
60 }
View Code

 

转载于:https://www.cnblogs.com/Fighting-sh/p/10341026.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值