Codeforces Round #424(Div.2)
来源:互联网 发布:神之浩劫数据迁移 编辑:程序博客网 时间:2024/06/18 03:26
题目链接
Array of integers is unimodal, if:
- it is strictly increasing in the beginning;
- after that it is constant;
- after that it is strictly decreasing.
The first block (increasing) and the last block (decreasing) may be absent. It is allowed that both of this blocks are absent.
For example, the following three arrays are unimodal: [5, 7, 11, 11, 2, 1], [4, 4, 2], [7], but the following three are not unimodal: [5, 5, 6, 6, 1], [1, 2, 1, 2], [4, 5, 5, 6].
Write a program that checks if an array is unimodal.
The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1 000) — the elements of the array.
Print "YES" if the given array is unimodal. Otherwise, print "NO".
You can output each letter in any case (upper or lower).
61 5 5 5 4 2
YES
510 20 30 20 10
YES
41 2 1 2
NO
73 3 3 3 3 3 3
YES
【思路】
求序列是否单峰,暴力即可。时间复杂度O(n)
【程序】
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <string>#include <ctime>#include <fstream>#include <map>#include <queue>#include <deque>#include <stack>#include <set>#include <vector>using namespace std;int a[110],n;int main(){scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);int l = 1;int r = n;for(l=1;a[l] < a[l+1];l++);for(r=n;a[r] < a[r-1];r--);for(;l<r;l++)if(a[l] != a[l+1]){printf("NO");return 0;} printf("YES\n");return 0;}
【思路】
同样模拟即可。。。大水题。。。
【程序】
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <string>#include <ctime>#include <fstream>#include <map>#include <queue>#include <deque>#include <stack>#include <set>#include <vector>using namespace std;char s1[1010],s2[1010],s[1010];int a[30];int len;int main(){scanf("%s%s",s1,s2);for(int i=0;i<26;i++)a[s1[i]-'a'] = s2[i]-'a';scanf("%s",s);int len = strlen(s);for(int i=0;i<len;i++)if(s[i] >= 'A' && s[i] <= 'Z') printf("%c",a[s[i]-'A']+'A');else if(s[i] >= 'a' && s[i] <= 'z') printf("%c",a[s[i]-'a'] + 'a');elseprintf("%c",s[i]);return 0;}
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain number of points (may be negative, i. e. points were subtracted). Initially the participant had some score, and each the marks were one by one added to his score. It is known that the i-th jury member gave ai points.
Polycarp does not remember how many points the participant had before this k marks were given, but he remembers that among the scores announced after each of the k judges rated the participant there were n (n ≤ k) values b1, b2, ..., bn (it is guaranteed that all values bj are distinct). It is possible that Polycarp remembers not all of the scores announced, i. e. n < k. Note that the initial score wasn't announced.
Your task is to determine the number of options for the score the participant could have before the judges rated the participant.
The first line contains two integers k and n (1 ≤ n ≤ k ≤ 2 000) — the number of jury members and the number of scores Polycarp remembers.
The second line contains k integers a1, a2, ..., ak ( - 2 000 ≤ ai ≤ 2 000) — jury's marks in chronological order.
The third line contains n distinct integers b1, b2, ..., bn ( - 4 000 000 ≤ bj ≤ 4 000 000) — the values of points Polycarp remembers. Note that these values are not necessarily given in chronological order.
Print the number of options for the score the participant could have before the judges rated the participant. If Polycarp messes something up and there is no options, print "0" (without quotes).
4 1-5 5 0 2010
3
2 2-2000 -20003998000 4000000
1
【思路】
把a的前缀和与b分别排序,然后一个一个试就可以了(注意处理重叠的情况)
【程序】
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <string>#include <ctime>#include <fstream>#include <map>#include <queue>#include <deque>#include <stack>#include <set>#include <vector>using namespace std;#define Maxn 2010#define OFFSET 8000010int n,k,ans;int a[Maxn],b[Maxn];bool bo[OFFSET << 1];int main(){scanf("%d%d",&k,&n);int x;for(int i=1;i<=k;i++){scanf("%d",&x);a[i] = a[i-1] + x;}for(int i=1;i<=n;i++) scanf("%d",&b[i]);sort(a+1,a+k+1); sort(b+1,b+n+1);for(int i=1;i<=k;i++){int st = b[1] - a[i],p = 2;for(int j=i+1;j<=k;j++) if(st + a[j] == b[p])p++;if(p == n + 1){if(!bo[st + OFFSET]) ans++;bo[st + OFFSET] = true;}}printf("%d\n",ans);return 0;}
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Print the minimum time (in seconds) needed for all n to reach the office with keys.
2 4 5020 10060 10 40 80
50
1 2 101115 7
7
【思路】
这很明显是一道Dp题。先把人和钥匙按大小排序,然后设f[i][j]为前i个人有j个钥匙的最少时间。则:f[i][j] = min(f[i][j-1] , max(f[i-1][j-1] , dis(a[i],b[j]) + dist(b[j],p)));
时间复杂度O(nk)。
【程序】
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <string>#include <ctime>#include <fstream>#include <map>#include <queue>#include <deque>#include <stack>#include <set>#include <vector>using namespace std;typedef long long LL;#define Maxn 1010#define Maxk 2010#define INF 10000000000LLLL n,k,p;LL a[Maxn];LL b[Maxk];LL f[Maxn][Maxk];int main(){cin>>n>>k>>p;for(int i=1;i<=n;i++)cin >> a[i];for(int i=1;i<=k;i++)cin >> b[i];sort(a+1,a+n+1); sort(b+1,b+k+1);memset(f,0x3f3f3f3f,sizeof(f));memset(f[0],0,sizeof(f[0]));for(int i=1;i<=n;i++)for(int j=1;j<=k;j++)f[i][j] = min(f[i][j],min(f[i][j-1],max(f[i-1][j-1],abs(a[i]-b[j]) + abs(b[j]-p))));cout<<f[n][k]<<endl;return 0;}
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.
Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.
You are to determine the total number of times Vasily takes the top card from the deck.
The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.
The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000), where ai is the number written on the i-th from top card in the deck.
Print the total number of times Vasily takes the top card from the deck.
46 3 1 2
7
11000
1
73 3 3 3 3 3 3
7
【思路】
解法1:暴力模拟,对于每一个要拿出的数,暴力算出与上一个拿出的之间共有多少数。时间复杂度O(n^2)。
解法2:从解法1可以看出,这相当于求动态区间里有多少数,用树状数组优化即可。时间复杂度O(nlogn)。
【程序】
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <vector>#include <map>#include <set>#include <cstdlib>using namespace std;typedef long long LL;#define Maxn 200010#define lowbit(x) x & (-x)vector<int> g[Maxn];int n;int a[Maxn];LL ans;int f[Maxn];void Add(int x,int d){for(;x<=n;x += lowbit(x)) f[x] += d;}int Sum(int x){int s = 0;for(;x;x -= lowbit(x)) s += f[x];return s;}int dist(int l,int r){if(l<r) return Sum(r) - Sum(l);else return Sum(n) - Sum(l) + Sum(r);}int main(){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);Add(i,1);g[a[i]].push_back(i);}int now = 0;for(int i=0;i<Maxn;i++)if(!g[i].empty()){int Maxx = 0,id = 0 , l;int siz = g[i].size();for(int j=0;j < siz;j++){l = dist(now,g[i][j]);if(l > Maxx){Maxx = l;id = j;}}ans = ans + 0LL + Maxx;now = g[i][id];for(int j=0;j < siz;j++)Add(g[i][j] , -1);}cout << ans << endl;return 0;}
【思路】
设每个d天看一次。则总共需要剪掉的米数就是
化简得:
同时除以d:(设S = k + sum(ai))
由此,如果有 且,可以得到:
则如果当d取d1时原不等式成立,则d2也一定成立。
求出所有的d2,并判断该方案是否可行即可。时间复杂度
【程序】
#include <cstdio>#include <iostream>#include <algorithm>#include <vector>#include <cmath>using namespace std;#define Maxn 110typedef long long LL;LL a[Maxn];int n;LL k,Maxx,ans;bool check(LL d){LL sum = 0;for(int i=0;i<n;i++)sum += (a[i] + d - 1LL) / d;sum *= d;return sum <= Maxx;}int main(){cin >> n >> k;Maxx = k;for(int i=0;i<n;i++){cin >> a[i];Maxx += a[i];}for(LL st=1,en;st <= Maxx;st = en + 1LL){en = Maxx / (Maxx / st);if(check(en)) ans = en;}cout << ans << endl;return 0;}
- Codeforces Round #424(Div.2)
- Codeforces Round #424 Div.2 A
- Codeforces Round #424 Div.2 B 【map】
- Codeforces Round #424 Div. 2专题
- Codeforces Round #424Div. 2题解
- Codeforces Round #102 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #104 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #107 (Div. 2)
- Codeforces Round #108 (Div. 2)
- Codeforces Round #110 (Div. 2)
- Codeforces Round #122 (Div. 2)
- Codeforces Round #121 (Div. 2)
- Codeforces Round #124 (Div. 2)
- Codeforces Round #124 (Div. 2)
- Activity的启动模式一些小重点
- ElasticSearch中From / Size介绍
- iOS用runtime给一个类动态添加方法 ---class_addMethod
- 一道内存的硬件选料题
- Spring-Aop
- Codeforces Round #424(Div.2)
- Codeforces 831D Office Keys 二分+贪心
- 在LaTeX中优雅地插入代码,并使用Consolas字体!
- Codeforces Round #424(A+B)
- Oracle OGG安装,很简单,监控比较复杂
- 自定义gradle插件
- Spring RMI API调用 不用配置文件xml
- [leetcode]120. Triangle(Java)
- [POJ2376]Cleaning Shifts