code1083,Codeforces Round #243 (Div. 1)

 2023-10-21 阅读 24 评论 0

摘要:---恢復內容開始--- A 枚舉l,r 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set&g

---恢復內容開始---

A

枚舉l,r

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<stdlib.h>
 6 #include<vector>
 7 #include<cmath>
 8 #include<queue>
 9 #include<set>
10 using namespace std;
11 #define N 210
12 #define LL long long
13 #define INF 0xfffffff
14 const double eps = 1e-8;
15 const double pi = acos(-1.0);
16 const double inf = ~0u>>2;
17 int a[N],b[N],c[N];
18 int main()
19 {
20     int i,j,n,k,g;
21     cin>>n>>k;
22     for(i = 1; i<= n; i++)
23     cin>>a[i];
24     int s = -INF;
25     for(i = 1 ;i <= n ;i++)
26         for(j = i ; j <= n ;j++)
27         {
28             int ts = 0;
29             int o = 0 ;
30             for(g = i ; g <= j; g++)
31             {
32                 b[o++] = a[g];
33             }
34             int e = 0;
35             for(g = 1; g < i; g++)
36             c[e++] = a[g];
37             for(g = j+1 ; g <= n ;g++)
38             c[e++] = a[g];
39             sort(b,b+o);
40             sort(c,c+e);
41             int ko = e-1,kk=0;
42             for(g = 0 ;g <= o ; g++)
43             {
44                 if(ko==-1||kk==k) break;
45                 if(c[ko]>b[g])
46                 {
47                     swap(c[ko],b[g]);
48                     ko--;
49                     kk++;
50                 }
51                 else break;
52             }
53             for(g = 0 ;g < o ; g++)
54             ts+=b[g];
55             s = max(ts,s);
56         }
57     cout<<s<<endl;
58     return 0;
59 }
View Code

B

code1083?使每個連通塊 變成矩形 所需改變的最小次數。

如果某一行的狀態或某一列的狀態確定了,整體的劃分是確定的。如果列數小于等于K狀壓枚舉列的狀態,否則肯定有一列的狀態是不變的 枚舉那一列的狀態。

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<stdlib.h>
 6 #include<vector>
 7 #include<cmath>
 8 #include<queue>
 9 #include<set>
10 using namespace std;
11 #define N 105
12 #define LL long long
13 #define INF 0xfffffff
14 const double eps = 1e-8;
15 const double pi = acos(-1.0);
16 const double inf = ~0u>>2;
17 int a[N][N];
18 int main()
19 {
20     int i,j,n,m,k,g;
21     cin>>n>>m>>k;
22     if(m<n)
23     {
24         for(i = 0 ; i < n ; i++)
25             for(j = 0 ;j < m ;j++)
26             cin>>a[i][j];
27     }
28     else
29     {
30         for(i = 0; i < n ;i++)
31             for(j = 0; j < m ;j++)
32             cin>>a[j][i];
33         swap(n,m);
34     }
35     int cnt = INF;
36     if(m<=k)
37     {
38         for(i = 0; i < (1<<m) ;i++)
39         {
40             int ans = 0;
41             for(j = 0 ; j < n; j++)
42             {
43                 int o = 0;
44                 for(g = 0 ; g < m ; g++)
45                 {
46                     if((a[j][g]<<g)!=(i&(1<<g)))
47                     o++;
48                 }
49                 ans+=min(o,m-o);
50             }
51             cnt = min(cnt,ans);
52         }
53     }
54     else
55     {
56         for(i = 0;i < m ; i++)
57         {
58             int ans = 0;
59             for(j = 0;j < m ;j++)
60             {
61                 if(j==i) continue;
62                 int o = 0;
63                 for(g = 0; g < n ; g++)
64                 {
65                     if(a[g][j]!=a[g][i]) o++;
66                 }
67                 ans+=min(o,n-o);
68             }
69             cnt = min(cnt,ans);
70         }
71     }
72     if(cnt<=k)
73     cout<<cnt<<endl;
74     else
75     cout<<"-1\n";
76 
77     return 0;
78 }
View Code

?

?

轉載于:https://www.cnblogs.com/shangyu/p/3700199.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/155737.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息