Codeforces Round #437 (Div. 2)

Codeforces Round #437 (Div. 2)

codeforces 867 A. Between the Offices(水)

题意:已知白天所在地(晚上可能坐飞机飞往异地),问是否从西雅图飞到旧金山次数更多。

题解:只要判断第一天和最后一天状态即可。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int N = 101;
 6 int n;
 7 char s[N];
 8 int main() {
 9     scanf("%d %s", &n, s);
10     if(s[0] == ‘S‘ && s[n-1] == ‘F‘) puts("YES");
11     else puts("NO");
12     return 0;
13 }

15ms

codeforces 865 A. Save the problem!(构造)

题意:原本是知道所需钱数和有多少种类的面额以及各面额的价值,要求有多少种方式可以从给定的面额中选取若干拼成所需的钱数,,现在反过来已知多少方式,来构造输入样例。

题解:直接用1和2面额来拼。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int n;
 6 int main() {
 7     scanf("%d", &n);
 8     printf("%d 2\n1 2\n", (n-1)*2+1);
 9     return 0;
10 }

15ms

codeforces 865 B. Ordering Pizza(贪心)

题意:有两种类型的披萨,已知有N个人要吃披萨,每块披萨有S片;第i个人吃si片,如果吃类型1,每片能获得ai幸福度,吃类型2则是bi幸福度。现在要购买最少数量的披萨满足N个人所需的数量,并求能获得的最大幸福度。

题解:贪心吃幸福度大的类型,记录1、2类型最优各吃几片,如果1、2类型披萨所需数量之和≤总共所需披萨数量,则直接输出答案,否则给 1和2类型幸福度差值较小的赋予高优先级,排序后 将多余的1类型换成2类型披萨,或2换成1,以满足最少数量披萨数目。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 typedef long long ll;
 6 const int N = 1e5+1;
 7 ll n, S;
 8 ll s[N],a[N],b[N];
 9 struct node {
10     ll x;
11     int id;
12     bool operator < (const node&r)const{
13         return x < r.x;
14     }
15 }c[N];
16 int main() {
17     int i, f = 0;
18     ll ans = 0, sum = 0, a1=0, a2=0;
19     ll s1=0, s2=0;
20     scanf("%lld %lld", &n, &S);
21     for(i = 1; i <= n; ++i) {
22         scanf("%lld %lld %lld", &s[i], &a[i], &b[i]);
23         if(a[i] > b[i]) s1 += s[i];
24         else if(a[i] < b[i]) s2 += s[i];
25         sum += s[i];
26         ans += max(a[i], b[i]) * s[i];
27         c[i].x = a[i] - b[i]; c[i].id = i;
28     }
29     ll cnt = (sum + S-1) /S;
30     if((s1 + S-1)/S + (s2 + S-1)/S <= cnt) {printf("%lld", ans); return 0;}
31     sort(c+1, c+1+n);
32     s1 %= S; s2 %= S;
33     for(i = 1; i <= n; ++i) {
34         if(c[i].x <= 0) {f = i; continue;}
35         if(!s1) break;
36         ll t = min(s[c[i].id], s1);
37         a1 += t * c[i].x;
38         s1 -= t;
39     }
40     for(i = f; i >= 1; --i) {
41         if(!c[i].x) continue;
42         if(!s2) break;
43         ll t = min(s[c[i].id], s2);
44         a2 -= t * c[i].x;
45         s2 -= t;
46     }
47     printf("%lld\n", ans - min(a1, a2));
48     return 0;
49 }

78ms

codeforces 865 D. Buy Low Sell High(优先队列,模拟)

题意:知道N天的股票的价格,一开始有0股,每天可以买一股或卖一股或什么都不做,要在N天后继续是0股,但希望在这N天内赚尽量多的钱。

题解:开个优先队列模拟。注意优先队列默认数据大的优先级高,所以加个负号。给每个值插入两遍,一是为了给前面小的值升值,二是为了将自己插入队列等待被买入。比如:4、7、9,4被升值为7进而被升值为9(实际选择是-4+9),第二步取出的一个7是为了给4进一步升值,7还要有一个在队列中等待被买入。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 using namespace std;
 6 priority_queue<int> q;
 7 int main() {
 8     int i, n, x;
 9     long long ans = 0;
10     scanf("%d", &n);
11     for(i = 1; i <= n; ++i) {
12         scanf("%d", &x);
13         if(!q.empty() && -q.top() < x) {
14             ans += x + q.top(); q.pop(); q.push(-x);
15         }
16         q.push(-x);
17     }
18     printf("%lld", ans);
19     return 0;
20 }

109ms

时间: 2024-08-11 01:29:45

Codeforces Round #437 (Div. 2)的相关文章

Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) E. Buy Low Sell High [贪心 II][数据结构 I]

题目:http://codeforces.com/contest/867/problem/E 题意:模拟股票操作,每天只能买一只股票或者卖一只股票或者什么也不做,求最大利润. 题解:仔细想想是非常简单的一个贪心问题,理解为连续的多次贪心买入卖出可以合并为一次的买入卖出,且值为最优.只需要用一个最小堆每次寻找前面的最小且没有被标记为购买点的值即可.如果自己为最小值,continue.如果那个值没有被选过,为买入点,pop.如果那个值被选为了售出的点,那么取消售出的标记,把当前点标记为售出点,利润直

Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2)

A 签到 B 题意 分析 C 题意 现有两种pizza, 每张pizza可分为s块,有n个人,分别给出n的人需要的块数,吃第一种1块获得的价值,吃第二种1块获得的价值,问在需要最少的pizza的数量下的可以获得最大价值为多少 分析 关键点:每个人都取最优,两种pizza余下的不会超过两张pizza 故可以将所有取最优,如果余下的可以组成一张,分别考虑第一种转为第二种和第二种转为第一种取最优即可 处理余下的最优的方法: 将第一种价值和第二种价值的差值进行排序 D 较难 E. Buy Low Sel

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un