首页 > 技术文章 > Codeforces Round #274 (Div. 2)

gaolzzxin 2014-10-20 22:24 原文

http://codeforces.com/contest/479/problem/A

枚举情况 

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 int main(){
 5     int a,b,c;
 6     while(~scanf("%d%d%d",&a,&b,&c)){
 7         int ans=0;
 8         ans=max(ans,a+b+c);
 9         ans=max(ans,a*b*c);
10         ans=max(ans,a*b+c);
11         ans=max(ans,a+b*c);
12         ans=max(ans,(a+b)*c);
13         ans=max(ans,a*(b+c));
14         printf("%d\n",ans);
15     }
16     return 0;
17 }
View Code

 

http://codeforces.com/contest/479/problem/B

暴力,每次拿一个,然后排序。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<vector>
 4 using namespace std;
 5 const int inf=0x3f3f3f3f;
 6 struct G{
 7     int val,id;
 8     friend bool operator <(const G &a,const G &b){
 9         return a.val<b.val;
10     }
11 }g[128];
12 struct A{
13     int x,y;
14 }now;
15 vector<A> ans;
16 int main(){
17     int n,k;
18     while(~scanf("%d%d",&n,&k)){
19         int big=0,sma=inf,cha;
20         for(int i=0;i<n;i++){
21             scanf("%d",&g[i].val);
22             g[i].id=i+1;
23             big=max(big,g[i].val);
24             sma=min(sma,g[i].val);
25         }
26         cha=big-sma;
27         ans.clear();
28         while(k--&&cha){
29             sort(g,g+n);
30             g[0].val++;
31             g[n-1].val--;
32             big=0;
33             sma=inf;
34             for(int i=0;i<n;i++){
35                 big=max(big,g[i].val);
36                 sma=min(sma,g[i].val);
37             }
38             if(big-sma>cha) break;
39             cha=big-sma;
40             now.x=g[n-1].id;
41             now.y=g[0].id;
42             ans.push_back(now);
43         }
44         printf("%d %d\n",cha,ans.size());
45         int len=ans.size();
46         for(int i=0;i<len;i++){
47             printf("%d %d\n",ans[i].x,ans[i].y);
48         }
49     }
50     return 0;
51 }
View Code

 

 c 

贪心

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int M=5010;
 5 struct G{
 6     int a,b;
 7     friend bool operator <(const G &a,const G &b){
 8         return a.a<b.a;
 9     }
10 }g[M];
11 int main(){
12     int n;
13     while(~scanf("%d",&n)){
14         for(int i=0;i<n;i++){
15             scanf("%d%d",&g[i].a,&g[i].b);
16         }
17         sort(g,g+n);
18         int now=0;
19         for(int i=0;i<n;){
20             int s=i,t;
21             int big=0;
22             int sma=0x3f3f3f3f;
23             for(int j=i;j<n;j++){
24                 if(g[i].a==g[j].a){
25                     t=j;
26                     big=max(big,g[j].b);
27                     sma=min(sma,g[j].b);
28                 }
29                 else{
30                     break;
31                 }
32             }
33             if(big<g[i].a&&sma>=now){
34                 now=big;
35             }
36             else{
37                 now=g[i].a;
38             }
39             i=t+1;
40         }
41         printf("%d\n",now);
42     }
43     return 0;
44 }
View Code

 

 

d

map 判断是否存在

 1 #include<cstdio>
 2 #include<map>
 3 using namespace std;
 4 const int M=1e5+10;
 5 int a[M];
 6 int l;
 7 map<int,bool> mp;
 8 bool has(int x,int y) {
 9     if(mp[x+y]||mp[x-y]) return true;
10     return false;
11 }
12 bool in(int x) {
13     if(x>=0&&x<=l) return true;
14     return false;
15 }
16 int main() {
17     int n,x,y;
18     while(~scanf("%d%d%d%d",&n,&l,&x,&y)) {
19         mp.clear();
20         for(int i=0; i<n; i++) {
21             scanf("%d",&a[i]);
22             mp[a[i]]=true;
23         }
24         bool fx=false,fy=false;
25         for(int i=0; i<n; i++) {
26             if(has(a[i],x)) {
27                 fx=true;
28             }
29             if(has(a[i],y)) {
30                 fy=true;
31             }
32         }
33         if(fx&&fy) {
34             puts("0");
35             continue;
36         }
37         if(!fx&&fy) {
38             puts("1");
39             printf("%d\n",x);
40             continue;
41         }
42         if(fx&&!fy) {
43             puts("1");
44             printf("%d\n",y);
45             continue;
46         }
47         int ans=-1;
48         for(int i=0; i<n; i++) {
49             if(in(a[i]-x)&&has(a[i]-x,y)) {
50                 ans=a[i]-x;
51                 break;
52             }
53             if(in(a[i]+x)&&has(a[i]+x,y)) {
54                 ans=a[i]+x;
55                 break;
56             }
57             if(in(a[i]-y)&&has(a[i]-y,x)) {
58                 ans=a[i]-y;
59                 break;
60             }
61             if(in(a[i]+y)&&has(a[i]+y,x)) {
62                 ans=a[i]+y;
63                 break;
64             }
65         }
66         if(ans!=-1) {
67             puts("1");
68             printf("%d\n",ans);
69         } else {
70             puts("2");
71             printf("%d %d\n",x,y);
72         }
73     }
74     return 0;
75 }
View Code

 

 

 

end

 

推荐阅读