Codeforces Round #FF(255) div2题解(未包含E)

这场比赛还是挺好的,C题没被hack但是最后system test的时候fail了,,另外E是个线段树,这个我得去先做一些线段树专题了回来再补

首先是A题(DZY Loves Hash) ,是个明显的水题,对于输入的n个x如果 x % p 对应的值已经出现过,就输出 第一次出现冲突的x的编号, 若没有冲突则输出 -1;

用一个大小为p的数组来记录对应的模值是否出现过即可

代码如下:

Result  :  Accepted     Memory  :  4 KB     Time  :  31 ms

/*
 * Author: Gatevin
 * Created Time:  2014/7/13 21:01:47
 * File Name: A.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;


int p,n;
bool se[330];


int main()
{
    cin>>p>>n;
    memset(se, 0 , sizeof(se));
    int tmp;
    for(int i = 1; i <= n; i++)
    {
        cin>>tmp;
        tmp %= p;
        if(!se[tmp])
        {
            se[tmp] = true;
        }
        else
        {
            cout<<i<<endl;
            return 0;
        }
    }
    cout<<"-1"<<endl;
    
    return 0;
}


然后是B(DZY Loves Strings) ,给出26个字母 所对应的值,然后有一个字符串,你要在这个字符串中加入k个字母使得这个字符串的和(即每个字母的值乘上其所在位置编号的和)最大

根据贪心的思想,将26个字母当中值最大的字母放在整个字符的末尾,连续放k个会使得字符串的和最大

代码如下:

Result  :  Accepted     Memory  :  8 KB     Time  :  31 ms

/*
 * Author: Gatevin
 * Created Time:  2014/7/13 21:07:55
 * File Name: B.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

int ma[30];
string s;
int k;
int main()
{
    cin>>s;
    cin>>k;
    int ans = 0;
    int tmp = 0;
    for(int i = 1; i <= 26; i++)
    {
        cin>>ma[i];
        tmp = max(tmp,ma[i]);
    }
    for(int i = 0; i <= s.length() - 1; i++)
    {
        ans += ma[s[i] - 'a' + 1]*(i + 1);
    }
    for(int i = 1; i <= k; i++)
    {
        ans += (i + s.length()) * tmp;
    }
    cout<<ans<<endl;
    return 0;
}

然后是C题(DZY Loves Sequences),这道题我是直接讨论的,每次都找出相邻的两个严格递增子列,然后如果可以将前一个子列的最后一个数字更改至满足要求或者将后一个子列的第一个数字更改至满足要求,就将他们的长度和与当前的答案相比从而更新答案

此题就是细节蛮多的吧,容易出错

代码如下:

Result  :  Accepted     Memory  :  400 KB     Time  :  93 ms

/*
 * Author: Gatevin
 * Created Time:  2014/7/15 12:44:16
 * File Name: test.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

int n;
int a[100100];


int main()
{
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        cin>>a[i];
    }
    int last1,last2;
    int cnt1 = 0, cnt2 = 0;
    int now = 0;
    int answer = 0;
    a[0] = 0;
    while(now < n)
    {
        if(a[now] < a[now + 1])
        {
            cnt1++;
            last1 = now + 1;
        }
        else
        {
            answer = max(answer, cnt1 + 1);
            cnt2 = 1;
            last2 = now + 1;
            for(int i = now + 1; i < n; i++)
            {
                if(a[i] < a[i + 1])
                {
                    cnt2++;
                    last2 = i + 1;
                }
                else
                {
                    break;
                }
            }
            if(now >= 2 && a[now + 1] - a[now - 1] >= 2)
            {
                answer = max(answer, cnt1 + cnt2);
            }
            else
            {
                answer = max(answer, cnt1 + 1);
                answer = max(answer, cnt2 + 1);
            }
            if(now <= n - 2 && a[now + 2] - a[now] >= 2)
            {
                answer = max(cnt1 + cnt2, answer);
            }
            else
            {
                answer = max(answer, cnt2 + 1);
                answer = max(answer, cnt1 + 1);
            }
            cnt1 = cnt2;
            last1 = last2;
            now = last2;
            continue;
        }
        now++;
    }
    if(cnt1 ==  n)
    answer = n;
    cout<<answer<<endl;
    return 0;
}

然后是D题(DZY Loves Modification) 这个题就是要想到将行和列的影响单独拿出来,即如果选了 i 次行,k - i 次列,那么如果每次选了之后处理时将行的选择视为不影响列的值,则最后得到的结果比原来的结果多了 i*(k - i)*p

然后将这个提出来之后,就只需要用优先队列找出去 i 次行得到的最大值和 取 i 次列得到的最大值即可;这里需要预处理,不然在枚举 k 时将会超时

代码如下:

Result  :  Accepted     Memory  :  15696 KB     Time  :  327 ms

/*
 * Author: Gatevin
 * Created Time:  2014/7/17 14:20:01
 * File Name: test.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
const lint inf = -99999999999999999;
lint row,col,k,p;
lint sumr[1001];
lint sumc[1001];
lint answerR[1000001];
lint answerC[1000001];

priority_queue <lint> Rtmp,Ctmp;

int main()
{
    scanf("%I64d %I64d %I64d %I64d",&row, &col, &k, &p);
    memset(sumr, 0, sizeof(sumr));
    memset(sumc, 0, sizeof(sumc));
    lint tmp;
    for(int i = 1; i <= row; i++)
    {
        for(int j = 1; j <= col; j++)
        {
            scanf("%I64d", &tmp);
            sumr[i] += tmp;
            sumc[j] += tmp;
        }
    }
    for(int i = 1; i <= row; i++)
    {
       Rtmp.push(sumr[i]);
    }
   for(int i = 1; i <= col; i++)
   {
      Ctmp.push(sumc[i]);
   } 
   memset(answerR, 0, sizeof(answerR));
   memset(answerC, 0, sizeof(answerC));
   lint ans = 0;
   for(int j = 1; j <= k; j++)
   {
       ans += Rtmp.top();
       tmp = Rtmp.top();
       tmp -= col*p;
       Rtmp.pop();
       Rtmp.push(tmp);
       answerR[j] = ans;
   }
   ans = 0;
   for(int j = 1; j <= k; j++)
   {
       ans += Ctmp.top();
       tmp = Ctmp.top();
       tmp -= row*p;
       Ctmp.pop();
       Ctmp.push(tmp);
       answerC[j] = ans;
   }
   lint answer = inf;
   for(int i = 0; i <= k; i++)
   {
       answer = max(answer, answerR[i] + answerC[k - i] - i*(k - i)*p);
   }
   printf("%I64d\n",answer);
    return 0;
}

 E题以后再补吧,,线段树也要开始认真学了~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值