【Codeforces Round 345 (Div 1) B】【暴力 双指针】Image Preview 看照片、阅读、移动、反转最多看照片数

来源:互联网 发布:js mousewheel scroll 编辑:程序博客网 时间:2024/06/11 19:56

Beautiful Paintings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.

We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.

Input

The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.

Output

Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.

Examples
input
520 30 10 50 40
output
4
input
4200 100 100 200
output
2
Note

In the first sample, the optimal order is: 10, 20, 30, 40, 50.

In the second sample, the optimal order is: 100, 200, 100, 200.


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 1e6 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int n;int a, b, T;char s[N], t[N];int ss[N], tt[N];int ans;void solve(){int l = n - 1;for (int i = 0; i < n; ++i){int rst = T - ss[i];if (rst < 0)return;rst -= i*a;while (l >= 1 && tt[l] > rst)--l;gmax(ans, l + i);}}int main(){while (~scanf("%d%d%d%d", &n, &a, &b, &T)){scanf("%s", s);strcpy(t, s);reverse(t + 1, t + n);for (int i = 1; i < n; ++i){ss[i] = ss[i - 1] + a + 1 + (s[i] == 'w')*b;tt[i] = tt[i - 1] + a + 1 + (t[i] == 'w')*b;}T -= 1 + (s[0] == 'w')*b;if (T < 0){ puts("0"); continue; }ans = 0;solve();swap(ss, tt); solve();printf("%d\n", min(ans + 1, n));}return 0;}/*【trick&&吐槽】1,5e5的算法怎么做?大暴力!2,心态稳住,读题一定要读进去。不要明明在分心还强行读题。会让自己做得一塌糊涂。3,Phone is in the vertical orientation and can't be rotatedPhone!Phone!而不是picture。读题要读主语啊!4,LL可以用%d读入一个int,但是int不可以用%lld读入一个LL5,然而这题还是挂掉了,因为不一定是先向右后向左,还是有可能先向左后向右的。两者是不一样的,因为我们退回的距离存在差异。6,移动成本不要忘记*a【题意】有n张照片,形成一个环。现在看的照片是照片1。我们可以向左看或者向右看下一张照片。我们需要a的时间去移动到下一张照片上。需要1的时间去看一张没看过的照片需要b的时间去把照片翻转——照片为什么需要翻转呢?因为照片有垂直的和水平的,而手机是水平的且我们不能翻转手机。我们想要求出,在T秒内最多能看的照片数。【类型】暴力 双指针【分析】这道题我们发现,我们的解,一定是向右看了若干张,然后向左看了若干张。向右看的张数多了,自然向左看的张数就少了。于是我们直接暴力向右看了几张,这道题就做完了。= =然而要注意的是,先左后右和先右后左是不一样的。我们还要先左向右贪一波。【时间复杂度&&优化】O(n)【数据】Input10 2 3 32hhwwhwhwwh读1花1,移2花2,读2花1,移1花2,*/


0 0
原创粉丝点击