CodeForces 651B

CodeForces 651B

相邻后面的数比前面的数大,就加一,求最终的结果,比赛时做时就直接去模拟了,慢了许多,不过数不是很多就不要紧了。

下面是我的代码:

  

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 int a[1010],vis[1010];
 5 int main(){
 6     int n, num;
 7     cin >> n;
 8     for(int i = 0; i < n; i ++){
 9         cin >> a[i];
10     }
11     sort(a,a+n);
12     int ans = 0;
13     int cnt = 0;
14     while(true){
15         if(cnt == n)break;
16         int flag = 0,a1,a2;
17         for(int i = 0; i < n; i ++){
18             if(!vis[i] && !flag){
19                 a1 = a[i];
20                 flag = vis[i] = 1;
21                 cnt++;
22             }else if(!vis[i] && a1 < a[i] && flag){
23                 ans++;
24                 vis[i] = true;
25                 cnt++;
26                 a1 = a[i];
27             }
28         }
29     }
30     cout << ans << endl;
31     return 0;
32 }

其实这个只要求出出现次数最多的数,再让n去减下记行了。

1 #include <iostream>
2 using namespace std;
3 int t,i,a[1001],n,k;
4 main(){
5     cin>>n;
6     while(i++<n)
7     cin>>t,k=(++a[t]>k?a[t]:k);
8     cout<<n-k;
9 }
时间: 2024-08-01 10:45:04

CodeForces 651B的相关文章

[刷题codeforces]651B/651A

651B Beautiful Paintings 651A Joysticks 点击可查看原题 651B是一个排序题,只不过多了一步去重然后记录个数.每次筛一层,直到全为0.从这个题里学到一个正确姿势:给定一个排好序的的数组,怎么把它变成一个去重加权的新数组. 只需要一个while循环+一个指针. #define N 10 #include<iostream> using namespace std; int main() { int a[N]={2,2,3,5,7,7,7,8,11,11};

CodeForces 651B Beautiful Paintings 贪心

A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at

16.05.25-16.06.10 题集

继2016.05.24续: codeforces 651B. Beautiful Paintings-简单 http://codeforces.com/problemset/problem/651/B 大意:给出一个序列,求解其任意排列中满足ai?+?1?>?ai 的元素个数最大和. 分析:理想情况下,无重复元素的0从小到大的排列,满足条件的元素个数最多,是n-1. 非理想情况下还有重复元素,只要不断提取出重复的这些元素归到另一集合再这样讨论即可. #include <iostream>

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp