Codeforces #Round 376 部分题解

A:

题目传送门:http://codeforces.com/problemset/problem/731/A

直接根据题意模拟即可

 1 #include "bits/stdc++.h"
 2
 3 using namespace std ;
 4 typedef long long QAQ ;
 5
 6 char s[ 1010 ] ;
 7
 8 inline int Calc ( const char x , const char y ) {
 9         if ( x > y ) return x - y ;
10         else return y -x ;
11 }
12
13 inline int gmin ( int x , int y ) {
14         return x > y ? y : x ;
15 }
16
17 int main ( ) {
18         scanf ( "%s" , s + 1 ) ;
19         int len = strlen ( s + 1 ) ;
20
21         char now = ‘a‘ ;
22         QAQ Ans = 0  ;
23         for ( int i=1 ; i<=len ; ++i ) {
24                 char next = s[ i ] ;
25                 int cost = gmin( Calc ( now , next ) , 26 - Calc ( now , next ) ) ;
26                 Ans += cost ;
27                 now = s[ i ] ;
28         }
29         cout << Ans << endl ;
30         return 0 ;
31 } 

B:

题目传送门:http://codeforces.com/problemset/problem/731/B

贪心

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <fstream>
 4 #include <sstream>
 5
 6 using namespace std ;
 7 const int maxN = 2e5 + 1e3 ;
 8 const double eps = 1e-5 ;
 9
10 int arr [ maxN ] ;
11
12 int INPUT ( ) {
13         int x = 0 , f = 1 ; char ch = getchar ( ) ;
14         while ( ch < ‘0‘ || ch > ‘9‘ ) { if ( ch == ‘-‘ ) f = -1 ; ch = getchar ( ) ; }
15         while ( ch <=‘9‘ && ch >= ‘0‘ ){ x = ( x << 1 ) + ( x << 3 ) + ch - ‘0‘ ; ch = getchar ( ) ; }
16         return x * f ;
17 }
18
19 int main ( ) {
20         int N = INPUT ( ) ;
21         for ( int i=1 ; i<=N ; ++i ) {
22                 arr[ i ] = INPUT ( ) ;
23         }
24         for ( int i=1 ; i<=N+1 ; ++i ) {
25                 if ( arr[ i ] < 0 ) {
26                         goto Fail ;
27                 }
28                 if ( arr[ i ] % 2 == 1 ){
29                         -- arr[ i + 1 ] ;
30                 }
31         }
32         cout << "YES" << endl ;
33         goto End ;
34         Fail :
35                 cout << "NO" << endl ;
36         End:
37                 return 0 ;
38 }

C:

题目传送门:http://codeforces.com/problemset/problem/731/C

将每个联通袜子分量加入一个冰炸鸡,用带权的冰炸鸡维护。最小染色数等于总共个数 - 颜色袜子最多的个数。

 1 #include "bits/stdc++.h"
 2
 3 using namespace std ;
 4 const int maxN = 2e5 + 1e3 ;
 5 typedef long long QAQ ;
 6
 7 int c[ maxN ] , t1[ maxN ] , t2[ maxN ] , father[ maxN ] , size[ maxN ] ;
 8 map <int ,int>mp[ maxN ] ;
 9 QAQ Ans ;
10
11 int getfa ( const int x ) { return father[ x ] == x ? x : father[ x ] = getfa ( father[ x ] ) ; }
12 inline int gmin ( const int x , const int y ) { return x > y ? y : x ; }
13 inline int gmax ( const int x , const int y ) { return x > y ? x : y ; }
14 void Set_Init ( const int n ) { for ( int i=1 ; i<=n ; ++i ) father[ i ] = i  , size[ i ] = 1 ; }
15 inline void Union_Set ( int x , int y ) { father[ x ] = y ; size[ y ] += size [ x ] ; }
16
17 inline int INPUT ( ) {
18         int ret = 0 , f = 1 ; char ch = getchar( ) ;
19         while ( ch < ‘0‘ || ‘9‘ < ch ) { if ( ch == ‘-‘ ) f = -1 ; ch = getchar ( ) ; }
20         while ( ‘0‘ <= ch && ch <= ‘9‘ ) { ret = ( ret << 1 ) + ( ret << 3 ) + ch - ‘0‘ ; ch = getchar ( ) ; }
21         return ret * f ;
22 }
23
24
25 int main ( ) {
26         int N = INPUT ( ) , M = INPUT ( ) , K = INPUT ( ) ;
27         for ( int i=1 ; i<=N ; ++i )
28                 c[ i ] = INPUT ( ) ;
29         Set_Init ( N ) ;
30         for ( int i=1 ; i<=M ; ++i ) {
31                 t1[ i ] = INPUT ( ) , t2[ i ] = INPUT ( ) ;
32                 int px = getfa ( t1[ i ] ) ;
33                 int py = getfa ( t2[ i ] ) ;
34                 if ( px != py ) Union_Set ( px , py ) ;
35         }
36         for ( int i=1 ; i<=N ; ++i ) ++mp[ getfa ( i ) ][ c[ i ] ] ;
37         for ( int i=1 ; i<=N ; ++i ) {
38                 if ( getfa ( i ) == i ) {
39                         int temp = 0 ;
40                         map <int , int>::iterator It ;
41                         for ( It = mp[ i ].begin( ) ; It != mp[ i ].end ( ) ; ++It ) {
42                                 temp = gmax( temp, It -> second ) ;
43                         }
44                         Ans += size[ i ] - temp ;
45                 }
46         }
47         cout << Ans << endl ;
48         return 0 ;
49 } 

时间: 2024-10-08 08:16:52

Codeforces #Round 376 部分题解的相关文章

Educational Codeforces Round 64部分题解

Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点个数是否有限,如果有限,输出. 很明显当正方形套三角形或者三角形套正方形是交点个数是无限的(因为有一条边相交) 其他图形的嵌套交点个数比较好判断,不多赘述 但是注意坑点: 当按照矩形,园,三角这样的顺序是,三角与圆的一个交点是与圆和正方形的交点重合的,判一下就好了 #include<cstdio>

Codeforces Round #616 部分题解

老年选手诈尸? A,B 咕了. C - Prefix Enlightenment 很容易看出这个限制条件可以推出每个点最多被两个集合包含.按照套路,很容易联想到给这两个集合连一条边,表示他们的状态要相同/不同. 因为保证了有解,所以从左往右扫的时候拿并查集维护一下每个连通块的二分图情况,选较小的那一边. 如果只被一个集合覆盖,那么就相当于强制这个集合选/不选,在做并查集的时候特判一下即可. 代码咕了. D - Coffee Varieties (hard version) 作为一名憨憨,做法当然

Codeforces Round #376 (Div. 2) F. Video Cards

题解: 没想到是直接暴力求解..... 使用前缀和加速 代码: #include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=1000100; int a[maxn],num[maxn]; int n,x; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&

Codeforces Round #376 (Div. 2) E. Funny Game

题解: 预处理前缀和后, 题意就转变成了.对于一个数组a[n],有两个人,第一个人选了第i(i>=2)个数,第二个人必须选第j(j>i)个数.依此循环,知道选了第n个数 求第一个人选的数的总和减去第二个人选的数的总和的最大值 分析: 对于第一个人,选了第k个数,那么从第k+1个数开始,要么继续选,要么给第二个人选,第二个人也是如此. 也据是使得a[i1]-a[i2]+a[i3]-a[i4].........最大.. 也就是一个人递推..但不知道怎么写.. 代码 #include<bits

Codeforces Round #467(Div2)题解

凌晨起来打CF,0:05,也是我第一次codeforces 第一题: 我刚开始怀疑自己读错题了,怎么会辣么水. 判除了0的数字种类 #include <cstdio> int n,ans=0; bool hsh[610]; int main(){ scanf("%d",&n);int i; for(i=1;i<=n;++i){ int x;scanf("%d",&x);if(!hsh[x] && x)++ans,hs

Educational Codeforces Round 16---部分题解

710A. King Moves 给你图中一点求出它周围有几个可达的点: 除边界之外都是8个,边界处理一下即可: #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cctype> #include<queue> #include<stack> #include<v

Codeforces Round#432 简要题解

来自FallDream的博客,未经允许,请勿转载,谢谢. Div2A 小判断题 Div2B 小判断题,合法的条件是|AB|=|BC|且三点不共线 Div1A 类比二维.三维空间,可以猜测n太大的时候没有答案.这样n小的时候我们就暴力就行啦. Div1B 首先枚举gcd,然后每个数要么删除,要么向上补到第一个倍数.对个数做前缀和,然后枚举之前枚举的gcd的倍数,统计答案.细节比较多. Div1C 不同质因数分开做.把1次是否出现.2次是否出现....压成一个数,求sg函数时枚举所有转移.这样n<=

Codeforces Round #376 (Div. 2)

Problem D  80-th Level Archeology 题目大意:有n行,每行有ki 个数,你没次能进行一次操作就是将所有数加1,这些数的上限是c,如果大于c则变成1, 问你有没有存在一个操作次数m使得每行的字典序上升. 思路:我们找出每两行之间决定他们字典序大小的两个数,每两个都能求出一次操作数的范围,然后就是看存不存在 一个范围所有区间都覆盖,差分标记一下就好啦. #include<bits/stdc++.h> #define pii pair<int,int> #

Educational Codeforces Round 75 ABCD题解

A. Broken Keyboard Description 给出一串小写字母字符序列,连续出现两次的字母为坏掉的,按字典序输出所有没有坏掉的字母. Solution 模拟暴力删除字母,注意相同字母的去重. 1 #include <algorithm> 2 #include <cctype> 3 #include <cmath> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <cst