zoukankan      html  css  js  c++  java
  • codeforces #446 892A Greed 892B Wrath 892C Pride 891B Gluttony

    A  链接:http://codeforces.com/problemset/problem/892/A

    签到

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 long long a,b[100001];
     5 int main() {
     6     int n;
     7     long long sum1=0,sum2=0;
     8     cin>>n;
     9     for(int i=0; i<n; ++i) cin>>a,sum1+=a;
    10     for(int i=0; i<n; ++i) cin>>b[i];
    11     sort(b,b+n);
    12     sum2=b[n-2]+b[n-1];
    13     if(sum2>=sum1) cout<<"YES"<<endl;
    14     else cout<<"NO"<<endl;
    15     return 0;
    16 }

    B 链接:http://codeforces.com/problemset/problem/892/B

    O(n)倒退遍历,保存当前能到的最大长度

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int a[1000005];
     4 int main() {
     5     ios::sync_with_stdio(false);
     6     int n;
     7     cin>>n;
     8     for(int i=1; i<=n; ++i) cin>>a[i];
     9     int len=-1;
    10     for(int i=n; i>=1; --i) {
    11         int temp=a[i];
    12         if(len>=0) a[i]=-1;
    13         len=max(len,temp);
    14         len--;
    15     }
    16     int sum=0;
    17     for(int i=1; i<=n; ++i) {
    18         if(a[i]!=-1) sum++;
    19     }
    20     cout<<sum<<endl;
    21     return 0;
    22 }

     C 链接:http://codeforces.com/problemset/problem/891/A

    检测第一次出现1的时候经历了几次操作的最小值,只要出现一次1,那么再加上n-1次操作就可以全部翻转为1

    利用gcd(ai)是否等于1可以剪枝,return -1

    特别地,要注意全部为1的特殊情况 return 0

    代码:

     1 #include <iostream>
     2 using namespace std;
     3 int a[2005];
     4 int GCD(int a, int b) {
     5     if(!b) return a;
     6     return GCD(b,a%b);
     7 }
     8 int main() {
     9     ios::sync_with_stdio(false);
    10     cin.tie(0);
    11     cout.tie(0);
    12     int n,gcd,exist_one=0;
    13     cin>>n;
    14     cin>>a[1];
    15     gcd=a[1];
    16     if(a[1]==1) exist_one++;
    17     for(int i=2; i<=n; ++i) {
    18         cin>>a[i];
    19         if(a[i]==1) exist_one++;
    20         if(gcd<=a[i]) gcd=GCD(a[i],gcd);
    21         else gcd=GCD(gcd,a[i]);
    22     }
    23     if(gcd!=1) {
    24         cout<<"-1"<<endl;
    25         return 0;
    26     }
    27     if(exist_one) {
    28         cout<<n-exist_one<<endl;
    29         return 0;
    30     }
    31     int minnum=2005;
    32     for(int i=1; i<n; ++i) {
    33         gcd=a[i];
    34         for(int j=i+1; j<=n; ++j) {
    35             if(a[j]>gcd) gcd=GCD(a[j],gcd);
    36             else gcd=GCD(gcd,a[j]);
    37             if(gcd==1) {
    38                 minnum=min(j-i,minnum);
    39                 break;
    40             }
    41         }
    42     }
    43     cout<<minnum+n-1<<endl;
    44     return 0;
    45 }

     D 链接:http://codeforces.com/problemset/problem/891/B

    让每一个数都能够找到小于自己的最大值,同时假如这个数是最小值,则用最大值来替换它

    ∵任何两个数(最大、最小除外)的差值都小于最小值与最大值的差值

    首先排序,找到对应的值,输出打印

    代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int a[25],b[25];
     4 int main() {
     5     ios::sync_with_stdio(false);
     6     int n,minnum=1e9;
     7     cin>>n;
     8     for(int i=0; i<n; ++i) {
     9         cin>>a[i];
    10         b[i]=a[i];
    11         minnum=min(minnum,a[i]);
    12     }
    13     sort(a,a+n);
    14     int flag=0;
    15     for(int i=0; i<=n-1; ++i) {
    16         flag=0;
    17         if(b[i]==minnum) {
    18             flag=1;
    19             b[i]=a[n-1];
    20             continue;
    21         }
    22         for(int j=n-1; j>=0; --j) {
    23             if(a[j]<b[i]) {
    24                 flag=1;
    25                 b[i]=a[j];
    26                 break;
    27             }
    28         }
    29         if(!flag) {
    30             cout<<"-1"<<endl;
    31             return 0;
    32         }
    33     }
    34     for(int i=0; i<n-1; ++i) cout<<b[i]<<" ";
    35     cout<<b[n-1]<<endl;
    36     return 0;
    37 }
  • 相关阅读:
    [ZJOI2013]K大数查询 浅谈整体二分
    2019暑假杭二day1测试总结
    2019暑假杭二day2测试总结
    2019暑假杭二day3测试总结
    2019暑假杭二day4测试总结
    2019暑假杭二day5测试总结
    一些有趣的教学视频
    karatsuba乘法
    多项式求逆元
    FFT
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7855640.html
Copyright © 2011-2022 走看看