codeforces——962

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lose__way/article/details/79953124

http://codeforces.com/problemset/problem/962/A

当前缀最短是多长时,前缀和为全串和的一半以上

#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;

const int kInt_Positive_Infinity_ = 0x3f3f3f3f;
const int kInt_Negative_Infinity_ = 0xcfcfcfcf;

#define kPI kInt_Positive_Infinity_
#define kNI kInt_Negative_Infinity_
#define LL long long
#define ULL unsigned long long
double sum;
template <typename cinType>
void getArr ( cinType *a, int n ) {
    for ( int i = 0; i < n; i++ ) {
        cin >> a[i];
        sum += a[i];
    }
}
template <typename coutType>
void putArr ( coutType *a, int n ) {
    for ( int i = 0; i < n; i++ )
        if ( i == n - 1 ) cout << a[i] << endl;
        else cout << a[i] << ' ';
}
template <typename reSetType>
void reSet ( reSetType *a, int n, reSetType val ) {
    for ( int i = 0; i < n; i++ )
        a[i] = val;
}

int main() {
    std::ios::sync_with_stdio ( false );
    cin.tie ( 0 );
    int n, in[200002];
    while ( cin >> n ) {
        sum = 0;
        getArr<int> ( in, n );
        sum = sum / 2.0;
        for ( int i = 0; i < n; i++ ) {
            sum -= in[i];
            if ( sum <= 0.01 ) {
                cout << i + 1 << endl;
                break;
            }
        }
    }
    return 0;
}

http://codeforces.com/problemset/problem/962/B

有ab两种学生,两种学生不能相邻而坐,问最多能坐几个学生下去

#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;

const int kInt_Positive_Infinity_ = 0x3f3f3f3f;
const int kInt_Negative_Infinity_ = 0xcfcfcfcf;

#define kPI kInt_Positive_Infinity_
#define kNI kInt_Negative_Infinity_
#define LL long long
#define ULL unsigned long long

char in[200004];
int main() {
    int n, m1, m2;
    while ( ~scanf ( "%d%d%d", &n, &m1, &m2 ) ) {
        scanf ( "%s", &in );
        int x1 = 0, x2 = 0;
        for ( int i = 0; i < strlen ( in ); i++ )
            if ( in[i] == '.' ) {
                int sum = 1;
                while ( in[i + 1] == '.' )
                    sum++, i++;
                x1 += sum / 2;
                x2 += sum % 2;
            }
        cout << min ( x1, m1 ) + min ( x1, m2 ) + min ( x2, ( x1 < m1 ? m1 - x1 : 0 ) + ( x1 < m2 ? m2 - x1 : 0 ) ) << endl;
    }
    return 0;
}

http://codeforces.com/problemset/problem/962/C

在所给数字中拆出一个平方数,且该平方数长度最长,若没有,输出-1。(数字无前导0)

#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;

const int kInt_Positive_Infinity_ = 0x3f3f3f3f;
const int kInt_Negative_Infinity_ = 0xcfcfcfcf;

#define kIPI kInt_Positive_Infinity_
#define kINI kInt_Negative_Infinity_
#define LL long long
#define ULL unsigned long long

char in[20];
int main() {
    while ( ~scanf ( "%s", &in ) ) {
        int len = pow ( 2, strlen ( in ) ), ans = kIPI;
        for ( int i = 1; i < len; i++ ) {
            int temp = i, k = 0, t = 0;
            char str[20];
            for ( int j = 0; j < strlen ( in ); j++, temp = temp >> 1 )
                if ( temp & 1 && ( k == 0 ? ( in[j] != '0' ) : true ) ) str[k++] = in[j];
                else t++;
            str[k++] = 0;
            sscanf ( str, "%d", &temp );
            if ( ( int ) sqrt ( temp ) * ( int ) sqrt ( temp ) == temp && temp ) 
                ans = min ( t, ans );
        }
        if ( ans == kIPI ) cout << "-1" << endl;
        else cout << ans << endl;
    }
    return 0;
}

http://codeforces.com/problemset/problem/962/D

数组中若有两个相同的值,删除左边的值并将右边的值乘2,输出最后结果。

#include <cstring>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;

const int kInt_Positive_Infinity_ = 0x3f3f3f3f;
const int kInt_Negative_Infinity_ = 0xcfcfcfcf;

#define kIPI kInt_Positive_Infinity_
#define kINI kInt_Negative_Infinity_
#define LL long long
#define ULL unsigned long long

char in[20];
int main() {
    std::ios::sync_with_stdio ( false );
    cin.tie ( 0 );
    LL n, in[150006];
    while ( cin >> n ) {
        for ( int i = 1; i <= n; i++ )
            cin >> in[i];
        map<LL, int>ma;
        int sum = n;
        for ( int i = 1; i <= n; )
            if ( ma[in[i]] == 0 ) {
                ma[in[i]] = i;
                i++;
            } else {
                in[ma[in[i]]] = 0;
                ma[in[i]] = 0;
                in[i] *= 2;
                sum--;
            }
        cout << sum << endl;
        for ( int i = 1; i <= n; i++ )
            if ( in[i] != 0 ) cout << in[i] << ' ';
        cout << endl;
    }
    return 0;
}





阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页