codeforces Round #215 div2 题解

A. Sereja and Coat Rack
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sereja owns a restaurant for n people. The restaurant hall has a coat rack with n hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the i-th hook costs ai rubles. Only one person can hang clothes on one hook.

Tonight Sereja expects m guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a d ruble fine to the guest.

Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the m guests is visiting Sereja's restaurant tonight.

Input

The first line contains two integers n and d (1 ≤ n, d ≤ 100). The next line contains integers a1a2...an (1 ≤ ai ≤ 100). The third line contains integer m (1 ≤ m ≤ 100).

Output

In a single line print a single integer — the answer to the problem.

Sample test(s)
input
2 1
2 1
2
output
3
input
2 1
2 1
10
output
-5
Note

In the first test both hooks will be used, so Sereja gets 1 + 2 = 3 rubles.

In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3 - 8 =  - 5.

排序即可。。

 

/****************************************************
* author:crazy_石头
* Pro:codeforces Round #215 div2 A
* algorithm:sort
* Time:47ms
* Judge Status:Accepted
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

#define rep(i,h,n) for(int i=(h);i<=(n);i++)
#define ms(a,b) memset((a),(b),sizeof(a))
#define eps 1e-6
#define INF 1<<29
#define LL __int64

using namespace std;

int n,m,d,a[100];

int main()
{
    cin>>n>> d;
    for (int i=0;i<n;i++)
        cin>>a[i];
    cin>>m;
    sort(a,a+n);
    int cnt=0;
    for(int i=0;i<min(m, n);i++)
        cnt+=a[i];
    if(m>n)
        cnt-=d*(m - n);
    cout<<cnt<<endl;
    return 0;
}

 

 

B. Sereja and Suffixes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sereja has an array a, consisting of n integers a1a2...an. The boy cannot sit and do nothing, he decided to study an array. Sereja took a piece of paper and wrote out m integers l1, l2, ..., lm (1 ≤ li ≤ n). For each number li he wants to know how many distinct numbers are staying on the positions lili + 1, ..., n. Formally, he want to find the number of distinct numbers among ali, ali + 1, ..., an.?

Sereja wrote out the necessary array elements but the array was so large and the boy was so pressed for time. Help him, find the answer for the described question for each li.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 105). The second line contains n integers a1a2...an (1 ≤ ai ≤ 105) — the array elements.

Next m lines contain integers l1, l2, ..., lm. The i-th line contains integer li (1 ≤ li ≤ n).

Output

Print m lines — on the i-th line print the answer to the number li.

Sample test(s)
input
10 10
1 2 3 4 1 2 3 4 100000 99999
1
2
3
4
5
6
7
8
9
10
output
6
6
6
6
6
5
4
3
2
1


从a[l]开始到后面的数字找出所有不同元素的个数,标记一下就行了;

/****************************************************
* author:crazy_石头
* Pro:codeforces Round #215 div2 B
* algorithm:
* Time:47ms
* Judge Status:Accepted
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

#define rep(i,h,n) for(int i=(h);i<=(n);i++)
#define ms(a,b) memset((a),(b),sizeof(a))
#define eps 1e-6
#define INF 1<<29
#define LL __int64

const int maxn=100000+20;

int a[maxn],vis[maxn],cnt[maxn];
int n,m;

int main()
{
     scanf("%d%d",&n,&m);
     rep(i,0,n-1)
        scanf("%d",&a[i]);
     ms(vis,0);
     ms(cnt,0);
     for(int i=n-1;i>=0;i--)
     {
        if(!vis[a[i]])
        {
            cnt[i]=cnt[i+1]+1;
            vis[a[i]]=1;
        }
        else
            cnt[i]=cnt[i+1];
     }
     rep(i,0,m-1)
     {
        int k;
        scanf("%d",&k);
        cout<<cnt[k-1]<<endl;
     }
    return 0;
}


 

C. Sereja and Algorithm
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps:

  1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2.
  2. Rearrange the letters of the found subsequence randomly and go to step 1.

Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.

Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≤ li ≤ ri ≤ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not.

Input

The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'.

The second line contains integer m (1 ≤ m ≤ 105) — the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers liri (1 ≤ li ≤ ri ≤ n).

Output

For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise.

Sample test(s)
input
zyxxxxxxyyz
5
5 5
1 3
1 11
1 4
3 6
output
YES
YES
NO
YES
NO
Note

In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly.

 

/****************************************************
* author:crazy_石头
* Pro:codeforces Round #215 div2 C
* algorithm:
* Time:47ms
* Judge Status:Accepted
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

#define rep(i,h,n) for(int i=(h);i<=(n);i++)
#define ms(a,b) memset((a),(b),sizeof(a))
#define eps 1e-6
#define INF 1<<29
#define LL __int64

using namespace std;

const int maxn=100010;
char s[maxn];
int a[maxn][5];
int c[3];
int m;
int main()
{
    scanf("%s",s+1);
    int n=strlen(s+1);
    memset(a,0,sizeof(a));
    for(int i=1;i<=n;i++)
    {
         for(int j=0;j<3;j++)
            a[i][j]=a[i-1][j];
            ++a[i][s[i]-'x'];
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        for(int i=0;i<3;i++)
            c[i]=a[r][i]-a[l-1][i];
        int Max=c[0],Min=c[0];
        for(int i=1;i<3;i++)
            Max=max(Max,c[i]),Min=min(Min,c[i]);
        puts(r-l+1<3 || Max-Min<=1? "YES": "NO");
    }
    return 0;
}

 


 

D. Sereja ans Anagrams
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sereja has two sequences a and b and number p. Sequence a consists of n integers a1, a2, ..., an. Similarly, sequence b consists of mintegers b1, b2, ..., bm. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions q (q + (m - 1)·p ≤ nq ≥ 1), such that sequence b can be obtained from sequence aq, aq + p, aq + 2p, ..., aq + (m - 1)p by rearranging elements.

Sereja needs to rush to the gym, so he asked to find all the described positions of q.

Input

The first line contains three integers nm and p (1 ≤ n, m ≤ 2·105, 1 ≤ p ≤ 2·105). The next line contains n integers a1a2...an (1 ≤ ai ≤ 109). The next line contains m integers b1b2...bm (1 ≤ bi ≤ 109).

Output

In the first line print the number of valid qs. In the second line, print the valid values in the increasing order.

Sample test(s)
input
5 3 1
1 2 3 2 1
1 2 3
output
2
1 3
input
6 3 2
1 3 2 2 3 1
1 2 3
output
2
1 2


 

啊啊啊啊啊,D别人好像是离散+数组就过了,我艹啊,我是STL,结果被T成渣,贴上我的超时代码九野巨巨的和AC代码:

 

题意简单就是:给一串数字  给个n m  p  然后输入a【i从1到n】 然后让你找到q  使得a[q]  a[q+p],a[q+2p],a[q+(m-1)p]这些数字  都在那些输入的数字中出现  记录符合条件的q的个数 并以此从小到大输出
。。叙述混乱。。。望理解。。

TLE代码:

/****************************************************
* author:crazy_石头
* Pro:codeforces Round #215 div2 D
* algorithm:STL-set
* Time:47ms
* Judge Status:Accepted
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

#define rep(i,h,n) for(int i=(h);i<=(n);i++)
#define ms(a,b) memset((a),(b),sizeof(a))
#define eps 1e-6
#define INF 1<<29
#define LL __int64

using namespace std;

const int maxn=100010<<1;

multiset<int> myset,s;
vector<int> q;
int n,m,p;
int a[maxn],b[maxn];

int main()
{
    scanf("%d%d%d",&n,&m,&p);
    rep(i,1,n)scanf("%d",&a[i]);
    rep(i,1,m)
    {
        scanf("%d",&b[i]);
        myset.insert(b[i]);
    }
    q.clear();
    int cnt=0;
    rep(i,1,n)
    {
        s.clear();
        for(int j=i,q=1;q<=m&&j<=n;q++,j+=p)s.insert(a[j]);
        if(myset==s)q.push_back(i);
    }
    printf("%d\n",q.size());
    sort(q.begin(),q.end());
    printf("%d",q[0]);
    rep(i,1,q.size()-1)
        printf(" %d",q[i]);
    printf("\n");
    return 0;
}

 

九野巨巨的AC代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<set>
#include<string>
#include<map>
#include<iterator>
#include<vector>
using namespace std;
inline int Max(int a,int b){return a>b?a:b;}
inline int Min(int a,int b){return a<b?a:b;}

int a[1000000],b[1000000];
map<int,int > aa,bb;
vector<int> ans;
int get(map<int, int>& mp, int v) { return mp.find(v)==mp.end()?0:mp[v]; }
int main()
{
       int n,m,p,i,j,pp;
        cin>>n>>m>>p;
      for(i=0;i<n;i++)cin>>a[i];
      for(i=0;i<m;i++)cin>>b[i],++bb[b[i]];
     for(i=0;i<min(n,p);i++)
     {
       aa.clear(), pp = 0;
       for(int k = 0, j = i; j < n; j += p, ++k)
       {
           int ta = ++aa[a[j]], tb = get(bb, a[j]);
           if(ta == tb  ) ++pp;
           else if(ta == tb+1) --pp;
           if(k >= m-1)
           {
             if(k > m-1)
             {
                int ta = --aa[a[j-m*p]], tb = get(bb, a[j-m*p]);
                if(ta == tb  ) ++pp;
                else if(ta == tb-1) --pp;
                if(ta == 0) aa.erase(aa.find(a[j-m*p]));
             }
             if(pp==bb.size() && aa.size()==bb.size()) ans.push_back(j);
          }
       }
    }
    sort(ans.begin(),ans.end());
    printf("%d\n", ans.size()); 
    for(i=0;i<ans.size();i++)printf("%d%c", ans[i]-(m-1)*p+1, i<ans.size()-1?' ':'\n');
  return 0;
}



Orz,又掉了,啊啊啊啊

E题好像有公式,北大牛直接秒了,我艹,太弱。。。

 

 

 

  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值