Codeforces Round #316 (Div. 2) (ABC题)

A - Elections

题意:

每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利;

最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序),第一位获得胜利;

求最后选举获胜者。

思路:

直接模拟即可。

代码:

/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;
const int maxn = 105;
struct C{
    int v;
    int no;
}c[maxn][maxn];

int cnt[maxn];
bool cmp(C a , C b){
    if(a.v != b.v) return a.v < b.v;
    return a.no > b.no;
}
int main(){
  //freopen("input.txt","r",stdin);
    int n,m;
    while( cin >> n >> m){
        cls(cnt);
        for(int i = 1 ; i <= m ; i++){
            for(int j = 1 ; j <= n ; j++){
                c[i][j].no = j;
                scanf("%d",&c[i][j].v);
            }
            sort(c[i]+1 , c[i]+n+1 , cmp);
            for(int j = 1 ; j <= n ; j++){
            }
            cnt[c[i][n].no]++;
        }
        int ans = n;
        int tmp = cnt[n];
        for(int i = n ; i >= 1 ; i--){
            if(tmp <= cnt[i]){
                ans = i;
                tmp = cnt[i];
            }
        }
        cout << ans << endl;
    }
    return 0;
}

B - Simple Game

题意:

Misha 与 Andrew 玩游戏,两人在1~n范围内各选一个数字(可相同),然后在1~n范围内随机出一个数字x,Misha 和 Andrew 的数字减去x的绝对值较小者获胜,若一致,则 Misha 获胜,现在已知 n 与 Misha 选择的数字,求 Andrew 胜率最高(同等胜率取最小)的数字。

思路:

分类讨论,只需要考虑 Misha 左右的位置即可,

注意 n = 1 时的情况特判。

代码:

/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;

int main(){
//  freopen("input.txt","r",stdin);
    int m,n;
    while(cin >> n >> m){
        if(m == 1){
            if(n == 1)
                cout << 1 << endl;
            else{
                cout << m+1 << endl;
            }
            continue;
        }
        if(m == n){
            if(n == 1)
                cout << 1 << endl;
            else{
                cout << m-1 << endl;
            }
            continue;
        }
        int ans;
        if(n&1){
            int tmp = (n+1) / 2;
            if(m < tmp){
                ans = m+1;
            }
            else if(m > tmp)
                ans = m-1;
            else if(m == tmp)
                ans = m-1;
        }
        else{
            int tmp = n / 2;
            if(m < tmp){
                ans = m+1;
            }
            else if(m > tmp)
                ans = m-1;
            else if(m == tmp)
                ans = m+1;
        }
        cout << ans << endl;
    }
    return 0;
}

C - Replacement

题意:

输入一个含 ‘.’ 与小写英文字母的字符串。

定义一种操作为: 将字符串中的 “..” 替代为 “.” ;

定义字符串的价值等于最大操作次数。

现在有 m 个询问 , 每个询问都将改变字符串指定位置上的字符为指定字符,计算询问后的字符串价值。

思路:

这题神似线段树的风格(用线段树也确实可以做。

这题的关键是简化不同情况的分类讨论,我之前的想法一直是记录每一个 ‘.’ 区间的情况,然后询问时二分在哪个区间即可,结果发现写起来思路混乱毫无逻辑,又是set又是map的。。

直到在standing榜看到第二名的神牛的代码。。怒删原来代码重写了一份,简单了很多很多。

代码:

/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;

int main(){
  //freopen("input.txt","r",stdin);
    int n , m ;
    string s;
    cin >> n >> m >> s;
    int cnt = 0;
    for( int i = 1 ; i < s.length() ; i++ ){
        if( s[i] == ‘.‘ && s[i-1] == ‘.‘) cnt ++;
    }
    for( int i = 1 ; i <= m ; i++ ){
        int p ;
        char c ;
        scanf( "%d %c" , &p , &c );
        p--;
        if( p > 0 && s[p] == ‘.‘ && s[p - 1] == ‘.‘ ) cnt--;
        if( p < s.length() && s[p] == ‘.‘ && s[p + 1] == ‘.‘ ) cnt--;
        s[p] = c;
        if( p < s.length() && s[p] == ‘.‘ && s[p + 1] == ‘.‘ ) cnt++;
        if( p > 0 && s[p] == ‘.‘ && s[p - 1] == ‘.‘ ) cnt++;
        cout << cnt << endl;
    }
    return 0;
}

版权声明:博主表示授权一切转载:)

时间: 2024-10-13 12:03:53

Codeforces Round #316 (Div. 2) (ABC题)的相关文章

Codeforces Round #247 (Div. 2) ABC

Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431 代码均已投放:https://github.com/illuz/WayToACM/tree/master/CodeForces/431 A - Black Square 题目地址 题意: Jury玩别踩白块,游戏中有四个区域,Jury点每个区域要消耗ai的卡路里,给出踩白块的序列,问要消耗多少卡路里. 分析: 模拟水题.. 代码: /* * Author: illuz

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to

Codeforces Round #243 (Div. 1) A题

http://codeforces.com/contest/425/problem/A 题目链接: 然后拿出这道题目是很多人不会分析题目,被题目吓坏了,其中包括我自己,想出复杂度,一下就出了啊!真是弱! 直接暴力求出矩阵数值,然后枚举每一个[I,J];再O[N]判断,分配好在[I,J]区间的数和之内的数,再排序下SOLO了 CODE:#include <cstdio> #include <cstring>#include <queue>#include <vect

Codeforces Round #634 (Div. 3) 补题

A. Candies and Two Sisters 签到题,直接输出即可 代码 #include<bits/stdc++.h> #define INF 0x3f3f3f3f typedef long long ll; using namespace std; inline void read(int &p) { p=0;int flag=1;char c=getchar(); while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();} w

Codeforces Round #366 (Div. 2) ABC

Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 1 #I hate that I love that I hate it 2 n = int(raw_input()) 3 s = "" 4 a = ["I hate that ","I love that ", "I hate it","I love it"] 5 fo

Codeforces Round #396 (Div. 2) D题Mahmoud and a Dictionary(并查集)解题报告

Mahmoud wants to write a new dictionary that contains n words and relations between them. There are two types of relations: synonymy (i. e. the two words mean the same) and antonymy (i. e. the two words mean the opposite). From time to time he discov

Codeforces Round #451 (Div. 2) ABC

A. Rounding Vasya has a non-negative integer n. He wants to round it to nearest integer, which ends up with 0. If n already ends up with 0, Vasya considers it already rounded. For example, if n = 4722 answer is 4720. If n = 5 Vasya can round it to 0 

Codeforces Round #316 (Div. 2) D计算在一棵子树内某高度的节点

题:https://codeforces.com/contest/570/problem/D 题意:给定一个以11为根的n个节点的树,每个点上有一个字母(a~z),每个点的深度定义为该节点到11号节点路径上的点数.每次询问a,ba,b查询以aa为根的子树内深度为bb的节点上的字母重新排列之后是否能构成回文串.分析:很明显是个树上启发式合并.显然,只要深度为bb结点的所有颜色中,至多有一种的数量为奇数就可以构成回文串了. #include<bits/stdc++.h> using namespa

Codeforces Round #396 (Div. 2) C题Mahmoud and a Message(dp)解题报告

Mahmoud wrote a message s of length n. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That's because th