Codeforces 898 贪心关闭最少闹钟 优先队列最少操作构造N/2squares 讨论情况哈希数字串分割a+b=c

A

/* Huyyt */
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
#define pb push_back
const int dir[8][2] = {{0, 1}, {1, 0}, {0, -1}, { -1, 0}, {1, 1}, {1, -1}, { -1, -1}, { -1, 1}};
using namespace std;
typedef long long ll;
int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        ll n;
        cin >> n;
        if(n%10<=5)
        {
                n-=n%10;
        }
        else
        {
                n+=10-n%10;
        }
        cout<<n<<endl;
        return 0;
}
View Code

B

/* Huyyt */
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
#define pb push_back
const int dir[8][2] = {{0, 1}, {1, 0}, {0, -1}, { -1, 0}, {1, 1}, {1, -1}, { -1, -1}, { -1, 1}};
using namespace std;
typedef long long ll;
int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        ll n;
        cin >> n;
        ll a,b;
        cin >> a >> b;
        ll bei=n/a;
        ll now;
        for(ll i=0;i<=bei;i++)
        {
               now=n-i*a;
               if(now%b==0)
               {
                       cout<<"YES"<<endl;
                       cout<<i<<" ";
                       cout<<now/b<<endl;
                       return 0;
               }
        }
        cout<<"NO"<<endl;
        return 0;
}
View Code

C

/* Huyyt */
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
#define pb push_back
const int dir[8][2] = {{0, 1}, {1, 0}, {0, -1}, { -1, 0}, {1, 1}, {1, -1}, { -1, -1}, { -1, 1}};
using namespace std;
typedef long long ll;
map<int, string> mpb;
map<string, int> mp;
string p[25][205];
int ok[25][205];
int num[25];
int ans[25];
int pop = 0;
bool check(string a, string b)
{
        if (b.size() > a.size())
        {
                return false;
        }
        for (int i = 0; i < b.size(); i++)
        {
                if (a[a.size() - i - 1] != b[b.size() - i - 1])
                {
                        return false;
                }
        }
        return true;
}
int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        int flag;
        int n;
        cin >> n;
        int now;
        string ch;
        string ch2;
        for (int i = 1; i <= n; i++)
        {
                cin >> ch >> now;
                if (!mp[ch])
                {
                        mp[ch] = ++pop;
                        mpb[pop] = ch;
                }
                for (int j = 1; j <= now; j++)
                {
                        cin >> ch2;
                        flag=1;
                        for(int k=1;k<=num[mp[ch]];k++)
                        {
                                if(ch2==p[mp[ch]][k])
                                {
                                        flag=0;
                                        break;
                                }
                        }
                        if(flag)
                        p[mp[ch]][++num[mp[ch]]] = ch2;
                }
        }
        for (int i = 1; i <= pop; i++)
        {
                ans[i] = num[i];
                //cout<<ans[i]<<endl;
                for (int j = 1; j <= num[i]; j++)
                {
                        for (int k = 1; k <= num[i]; k++)
                        {
                                if (j == k)
                                {
                                        continue;
                                }
                                if (check(p[i][k], p[i][j]))
                                {
                                        ok[i][j] = 1;
                                        ans[i]--;
                                        break;
                                }
                        }
                }
        }
        cout << pop << endl;
        for (int i = 1; i <= pop; i++)
        {
                cout << mpb[i] << " ";
                cout << ans[i] << " ";
                for (int j = 1; j <= num[i]; j++)
                {
                        if (!ok[i][j])
                        {
                                cout << p[i][j] << " ";
                        }
                }
                cout << endl;
        }
        return 0;
}
View Code

D

/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll LLmaxn = 2e18;
const int N = 1e6 + 5;
int num[N];
int visit[N];
int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        int n, m, k;
        cin >> n >> m >> k;
        for (int i = 1; i <= n; i++)
        {
                cin >> num[i];
                visit[num[i]] = 1;
        }
        int anser = 0;
        int now = 0;
        for (int i = 1; i <= 1000000; i++)
        {
                if (!visit[i])
                {
                        if ((i - m + 1 >= 1) && visit[i - m + 1])
                        {
                                now--;
                        }
                        continue;
                }
                if (now < k - 1)
                {
                        now++;
                }
                else
                {
                        visit[i] = 0;
                        //cout << i << endl;
                        anser++;
                }
                if ((i - m + 1 >= 1) && visit[i - m + 1])
                {
                        now--;
                }
        }
        cout << anser << endl;
        return 0;
}
View Code

E

/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll LLmaxn = 2e18;
const int N = 2e5 + 5;
ll num[N];
ll cha[N];
priority_queue<ll, vector<ll>, greater<ll> >q;
int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        int n;
        cin >> n;
        int sum = 0;
        int zero = 0;
        for (int i = 1; i <= n; i++)
        {
                cin >> num[i];
                ll s = sqrt(num[i]);
                cha[i] = min(abs(s * s - num[i]), (s + 1) * (s + 1) - num[i]);
                if (cha[i] == 0)
                {
                        sum++;
                }
                else
                {
                        q.push(cha[i]);
                }
                if (num[i] == 0)
                {
                        zero++;
                }
        }
        if (sum >= n / 2)
        {
                if (sum == n / 2)
                {
                        cout << 0 << endl;
                        return 0;
                }
                if (zero <= n / 2)
                {
                        cout << sum - n / 2 << endl;
                }
                else
                {
                        cout << sum - zero + (zero - n / 2) * 2 << endl;
                }
                return 0;
        }
        int t = n / 2 - sum;
        ll anser = 0;
        while (t--)
        {
                anser += q.top();
                q.pop();
        }
        cout << anser << endl;
        return 0;
}
View Code

F

题意为给你一个长度不超过1e6的数字串 要求你插入'+'和'='使得串被分为三个无前导零的数 a,b,c 且a+b=c

解:

分情况讨论 有且只有四种情况

①/② a.size/b.size=c.size ③/④ a.size/b.size=c.size-1 枚举的复杂度为n

每次直接用暴力来check的话 很明显check的复杂度是len 而len和n是一个级别的 总复杂度就是n2 不行

所以我们需要hash一下 先用hash判断 如果符合的话 再用字符串模拟大数check

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const int M = 1e6 + 44;
const int sed = 10;
const int mod = 1e9 + 7;
int n;
char str[M];  //原数字串
int s[M];  //数字数组
int suf[M];  //
int bas[M];
int inv[M];
long long pr(int a, int b) //快速幂
{
        long long r = 1, base = a;
        while (b != 0)
        {
                if (b & 1)
                {
                        r = (r * base) % mod;
                }
                base = (base * base) % mod;
                b >>= 1;
        }
        return r;
}
bool check(int la, int lb, int lc)
{
        int sava = la, savb = lb, savc = lc;
        if (la > lc || lb > lc || la < 1 || lb < 1 || lc < 1)
        {
                return false;
        }
        int lia = 1, lib = la + 1, lic = la + lb + 1;
        if ((la > 1 && s[lia] == 0) || (lb > 1 && s[lib] == 0) || (lc > 1 && s[lic] == 0))
        {
                return false;
        }
        int ria = la, rib = la + lb, ric = la + lb + lc;
        int vala, valb, valc, up = 0, tmp;
        while (lc > 0)
        {
                if (la > 0)
                {
                        vala = s[ria];
                }
                else
                {
                        vala = 0;
                }
                if (lb > 0)
                {
                        valb = s[rib];
                }
                else
                {
                        valb = 0;
                }
                tmp = up + vala + valb;
                up = tmp / 10;
                tmp %= 10;
                if (tmp != s[ric])
                {
                        return false;
                }
                la--;
                lb--;
                lc--;
                ria--;
                rib--;
                ric--;
        }
        if (up)
        {
                return false;
        }
        for (int i = 1; i <= n; i++)
        {
                printf("%d", s[i]);
                if (i == sava)
                {
                        printf("+");
                }
                if (i == sava + savb)
                {
                        printf("=");
                }
        }
        return true;
}
int getval(int li, int ri)
{
        int ret, tmp;
        tmp = (0ll + suf[li] - suf[ri + 1]) % mod;
        tmp = (1ll * tmp * inv[n - ri]) % mod;
        ret = (tmp % mod + mod) % mod;
        return ret;
}
bool solve(int la, int lb, int lc)
{
        int sava = la, savb = lb, savc = lc;
        if (la > lc || lb > lc || la < 1 || lb < 1 || lc < 1)
        {
                return false;
        }
        int lia = 1, lib = la + 1, lic = la + lb + 1;
        if ((la > 1 && s[lia] == 0) || (lb > 1 && s[lib] == 0) || (lc > 1 && s[lic] == 0))
        {
                return false;
        }
        int ria = la, rib = la + lb, ric = la + lb + lc;
        int vala, valb, valc;
        vala = getval(lia, ria);
        valb = getval(lib, rib);
        valc = getval(lic, ric);
        //  cout<<la<<' '<<lb<<' '<<lc<<endl;
        //  cout<<vala<<' '<<valb<<' '<<valc%mod<<endl;
        if ((0ll + vala + valb) % mod == valc % mod && check(la, lb, lc))
        {
                return true;
        }
        else
        {
                return false;
        }
}
int main()
{
        scanf("%s", str + 1);
        n = strlen(str + 1);
        for (int i = 1; i <= n; i++)
        {
                s[i] = str[i] - '0';
        }
        bas[0] = 1;
        for (int i = 1; i <= n; i++) //每一位的hash值
        {
                bas[i] = 1ll * bas[i - 1] * sed % mod;
        }
        for (int i = 0; i <= n; i++) //处理逆元
        {
                inv[i] = pr(bas[i], mod - 2);
        }
        suf[n + 1] = 0;
        for (int i = n; i >= 1; i--)  //从后往前hash
        {
                suf[i] = (suf[i + 1] + 1ll * bas[n - i] * s[i] % mod) % mod;
        }
        for (int i = 1; i <= n; i++)  //枚举
        {
                //四种情况 如果有的话就结束
                //lc-1,lc-1,lc    lc,lc,lc    lc
                if (solve(i - 1, n - i - (i - 1), i) || solve(i, n - i - i, i) || solve(n - i - (i - 1), i - 1, i) || solve(n - i - i, i, i))
                {
                        return 0;
                }
        }
        return 0;
}
View Code

 

posted @ 2018-05-28 17:38  Aragaki  阅读(206)  评论(0编辑  收藏  举报