[codeforces 293]A. Weird Game

[codeforces 293]A. Weird Game

试题描述

Yaroslav, Andrey and Roman can play cubes for hours and hours. But the game is for three, so when Roman doesn't show up, Yaroslav and Andrey play another game.

Roman leaves a word for each of them. Each word consists of n binary characters "0" or "1". After that the players start moving in turns. Yaroslav moves first. During a move, a player must choose an integer from 1 to n, which hasn't been chosen by anybody up to that moment. Then the player takes a piece of paper and writes out the corresponding character from his string.

Let's represent Yaroslav's word as s = s1s2... s2n. Similarly, let's represent Andrey's word as t = t1t2... t2n. Then, if Yaroslav choose number k during his move, then he is going to write out character sk on the piece of paper. Similarly, if Andrey choose number r during his move, then he is going to write out character tr on the piece of paper.

The game finishes when no player can make a move. After the game is over, Yaroslav makes some integer from the characters written on his piece of paper (Yaroslav can arrange these characters as he wants). Andrey does the same. The resulting numbers can contain leading zeroes. The person with the largest number wins. If the numbers are equal, the game ends with a draw.

You are given two strings s and t. Determine the outcome of the game provided that Yaroslav and Andrey play optimally well.

输入

The first line contains integer n (1 ≤ n ≤ 106). The second line contains string s — Yaroslav's word. The third line contains string t — Andrey's word.

It is guaranteed that both words consist of n characters "0" and "1".

输出

Print "First", if both players play optimally well and Yaroslav wins. If Andrey wins, print "Second" and if the game ends with a draw, print "Draw". Print the words without the quotes.

输入示例

4
01010110
00101101

输出示例

First

数据规模及约定

见“输入

题解

每个人都可以贪心地取上下都是 1 的位置,取完了再取自己位置是 1 的位置,再完了就取对方是 1 的位置,最后取都是 0 的位置,最后谁 1 多谁赢。(就是遵循“利己损人”的贪心策略)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;

const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
    if(Head == Tail) {
        int l = fread(buffer, 1, BufferSize, stdin);
        Tail = (Head = buffer) + l;
    }
    return *Head++;
}
int read() {
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

#define maxn 2000010
int n;
char s1[maxn], s2[maxn];

int main() {
	n = (read() << 1);
	scanf("%s%s", s1 + 1, s2 + 1);
	
	int cnt10 = 0, cnt01 = 0, cnt11 = 0, fir = 0, sec = 0;
	for(int i = 1; i <= n; i++)
		cnt10 += (s1[i] == '1' && s2[i] == '0'),
		cnt01 += (s1[i] == '0' && s2[i] == '1'),
		cnt11 += (s1[i] == '1' && s2[i] == '1');
	sec = (cnt11 >> 1);
	fir = cnt11 - sec;
	if(cnt11 & 1) {
		if(cnt01 < cnt10) {
			fir += cnt01 + (cnt10 - cnt01 >> 1);
			sec += cnt01;
		}
		if(cnt01 > cnt10) {
			fir += cnt10;
			sec += cnt10 + (cnt01 - cnt10 - (cnt01 - cnt10 >> 1));
		}
		if(cnt01 == cnt10) {
			fir += cnt10;
			sec += cnt01;
		}
	}
	else {
		if(cnt01 < cnt10) {
			fir += cnt01 + (cnt10 - cnt01 - (cnt10 - cnt01 >> 1));
			sec += cnt01;
		}
		if(cnt01 > cnt10) {
			fir += cnt10;
			sec += cnt10 + (cnt01 - cnt10 >> 1);
		}
		if(cnt01 == cnt10) {
			fir += cnt10;
			sec += cnt01;
		}
	}
	
	if(fir > sec) puts("First");
	if(fir < sec) puts("Second");
	if(fir == sec) puts("Draw");
	
	return 0;
}

 

posted @ 2016-08-27 16:16  xjr01  阅读(392)  评论(0编辑  收藏  举报