组队

图片说明
图片说明

#include<stdio.h>
int main(void) {
    freopen("team.txt","r",stdin);
    int a[21][6];
    for(int i=1; i<=20; ++i)
        for(int j=0; j<=5; ++j)//每个人前面有一个序号 
            scanf("%d",&a[i][j]);
    int ans=0;
    for(int i1=1; i1<=20; ++i1) {
        for(int i2=1; i2<=20; ++i2) {
            if(i2==i1)continue;
            for(int i3=1; i3<=20; i3++) {
                if(i3==i1||i3==i2)continue;
                for(int i4=1; i4<=20; ++i4) {
                    if(i4==i1||i4==i2||i4==i3)continue;
                    for(int i5=1; i5<=20; ++i5) {
                        if(i5==i1||i5==i2||i5==i3||i5==i4)continue;
                        if(a[i1][1]+a[i2][2]+a[i3][3]+a[i4][4]+a[i5][5]>ans)ans=a[i1][1]+a[i2][2]+a[i3][3]+a[i4][4]+a[i5][5];
                    }
                }
            }
        }
    }
    printf("%d\n",ans);//490 
    return 0;
}
#include<algorithm>
#include<cstdio>
#include<iostream>
using namespace std;
const int n=20;
const int m=5;
int a[n+1][m+1];
int vis[m+1];
int ans=0;
void dfs(int lv,int sum) {
    if(lv==6) {
        ans=max(ans,sum);
        return;
    }
    for(int i=1; i<=n; ++i) {
        bool f=0;
        for(int j=1; j<lv; ++j) {
            if(vis[j]==i) {
                f=1;
            }
        }
        if(!f) {
            vis[lv]=i;
            dfs(lv+1,sum+a[i][lv]);
        }
    }
}
int main() {
    for(int i=1; i<=20; ++i)
        for(int j=0; j<=5; ++j)//每个人前面有一个序号
            scanf("%d",&a[i][j]);
    dfs(1,0);
    cout << ans << endl;
}

年号字串

图片说明

#include<algorithm>
#include<iostream>
using namespace std;
int main() {
    int a;
    while(cin>>a) {
        string ans="";
        while(a) {
            --a;//精髓
            ans+='A'+a%26;
            a/=26;
        }
        reverse(ans.begin(),ans.end());
        cout<<ans<<endl;//BYQ
    }
    return 0;
}

数列求值

图片说明

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int a=1,b=1,c=1,d;
int main() {
    for(int i=4;i<=20190324;++i){
        d=(a+b+c)%10000;
        a=b,b=c,c=d;
    }cout<<d<<endl;//4659
    return 0;
}

数的分解

图片说明

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
bool chk(int n) {
    for (; n; n /= 10)
        if (n % 10 == 2 || n % 10 == 4) return 0;
    return 1;
}
int main() {
    int cnt = 0;
    for (int i = 1999; i >= 673; --i) {
        if (chk(i))
            for (int j = 2; j < i && i + j < 2019; ++j) {
                int k = 2019 - i - j;
                if (i > j && j > k && chk(j) && chk(k)) ++cnt;
            }
    }
    printf("%d\n", cnt);  // 40785
    return 0;
}

迷宫

图片说明

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
#include <cstring>
#include <iostream>
#include <queue>
#define sc(x) scanf("%d", &(x))
const int ax[4] = {1, 0, 0, -1};
const int ay[4] = {0, -1, 1, 0};
const char dir[5] = {'D', 'L', 'R', 'U'};
int maze[105][105];
int X, Y;
using namespace std;
struct point {
    int x, y;
    string ans;
    point(int a, int b, string c) : x(a), y(b), ans(c) {}
};
bool judge(int x, int y) {
    if (x > 0 && x <= X && y > 0 && y <= Y && !maze[x][y]) return true;
    return false;
}
void bfs() {
    queue<point> ans;
    ans.push(point(1, 1, ""));
    while (!ans.empty()) {
        point temp = ans.front();
        ans.pop();
        if (temp.x == X && temp.y == Y) {//抵达终点
            cout << temp.ans << endl;
            return;
        }
        for (int i = 0; i < 4; i++) {
            int tempx = temp.x + ax[i];
            int tempy = temp.y + ay[i];
            if (judge(tempx, tempy)) {
                maze[tempx][tempy] = 1;//不走回头路
                ans.push(point(tempx,tempy,temp.ans+dir[i]));
            }
        }
    }
}
int main() {
    freopen("maze.txt","r",stdin);
    X = 30, Y = 50;
    for (int i = 1; i <= X; i++) {
        for (int j = 1; j <= Y; j++) {
            char t= getchar();
            maze[i][j] = t-'0';
        }
        getchar();
    }
    bfs();
    return 0;
}

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

特别数的和

图片说明

打表可以进行常数级别的优化。大约可以把时间降低到1/3到1/4。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
bool chk(int n) {
    for (; n; n /= 10)
        if (n % 10 == 2 ||n % 10 ==0||n % 10 ==1|| n % 10 == 9) return 1;
    return 0;
}
int main() {
    ll n;
    while(~scanf("%lld",&n)) {
        ll ans=0;
        for(int i=1; i<=n; ++i)
            if(chk(i))ans+=i;
        printf("%lld\n",ans);
    }
    return 0;
}

完全二叉树的权值

图片说明
图片说明
图片说明

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int N=1e5+7;
ll a[N];
int main() {
    ll n;
    while(~scanf("%lld",&n)) {
        for(int i=1; i<=n; ++i)scanf("%lld",&a[i]);
        ll ans=1,maxx=a[1],p=2,lv=1;
        while(p<=n) {
            ++lv;
            ll now=0;
            for(int i=0; i<(1<<(lv-1))&&p<=n; ++i)now+=a[p++];
            if(now>maxx)ans=lv,maxx=now;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

等差数列

图片说明
图片说明

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int N=1e5+7;
ll a[N],d[N];
int main() {
    ll n,m;
    while(~scanf("%lld",&n)) {
        for(int i=1; i<=n; ++i)scanf("%lld",&a[i]);
        sort(a+1,a+1+n);
        for(int i=1; i<n; ++i) d[i]=a[i+1]-a[i];
        if(!d[1]) {
            printf("%lld\n",n);
            continue;
        }
        ll c=__gcd(d[1],d[2]);
        for(int i=3; i<n; ++i) c=__gcd(c,d[i]);
        ll ans=(a[n]-a[1])/c+1;
        printf("%lld\n",ans);
    }
    return 0;
}

后缀表达式

图片说明

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
ll n, m, a[N];
int main() {
    while (cin >> n >> m) {
        for (int i = 0; i <= n + m; ++i) scanf("%lld", &a[i]);
        sort(a, a + n + m + 1);
        int L = 0, R = n + m;
        ll ans = 0;
        bool f = a[L] < 0 && m;
        while (a[L] < 0 && m) {
            if (m > 1 || a[L + 1] >= 0 && m == 1 || !n) --m;
            else --n;
            ans -= a[L++];
        }
        ans += a[R--];
        while (R >= L && n) ans += a[R--], --n;
        if (m) {
            if (!f) ans -= a[L++], --m;
            while (R >= L && m) ans += a[R--], --m;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
#include <algorithm>
#include <cstdio>
#include <iostream>
#define sc(x) scanf("%lld", &(x))
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
ll n, m, a[N];
int main() {
    while (cin >> n >> m) {
        ll sum = n + m + 1;
        for (int i = 0; i < sum; ++i) sc(a[i]);
        sort(a, a + sum);
        int L = 0, R = sum - 1;
        ll ans = 0;
        bool f = 0;
        while (a[L] < 0 && m) {
            f = 1;
            if (m > 1)  // 当减号数量大于1的时候,优先用减号,将负数转化为正数
                ans -= a[L++], --m;
            else if (a[L + 1] >= 0 && m == 1 || !n)  //如果下一个数是正数而且负号只剩一个
                                                     //那就用这个减号来把手上的负数变号
                                                     //或者正号耗尽 别无选择
                ans -= a[L++], --m;
            else if (n)  //有减号的话,就可以把加号当减号用,将负数转化为正数
                ans -= a[L++], --n;
        }
        ans += a[R--];
        while (R >= L && n)
            ans += a[R--], --n;  //如果还有加号剩余,那么就用加号加大的数
        if (m) {                 //如果还有减号
            if (f)  //如果之前对负数用过减号,那么就可以把减号当加号用,对正数操作
                while (R >= L && m) ans += a[R--], --m;
            else {  //如果没有,就减去最小的,这样就可以负负得正了
                ans -= a[L++], --m;
                while (R >= L && m) ans += a[R--], --m;
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

除了以下情况,其他情况均为绝对值相加:

  1. 没有减号(全部相加)
  2. 全是负数(减去最大的值 其余相加)
  3. 全是正数(减去最小的值 其余相加)

灵能传输

图片说明
图片说明
图片说明
图片说明
图片说明
图片说明

混分代码

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long ll;
const int N=3e5+7;
ll a[N];
void dr(int i) {
    a[i-1]+=a[i],a[i+1]+=a[i],a[i]=-a[i];
}
int main() {
    ll n,T;
    scanf("%lld",&T);
    while(T--) {
        scanf("%lld",&n);for(int i=1; i<=n; ++i)scanf("%lld",&a[i]);
        for(int i=2;i<n;++i){
            ll ori=max(abs(a[i-1]),abs(a[i+1]));
            ll aft=max(abs(a[i-1]+a[i]),abs(a[i+1]+a[i]));
            if(ori>aft)dr(i);
        }
        printf("%lld\n",*max_element(a+1,a+n+1));
    }
    return 0;
}