Codeforces - 思维(第2周)

1、Sea Battle

In order to make the "Sea Battle" game more interesting, Boris decided to add a new ship type to it. The ship consists of two rectangles. The first rectangle has a width of ?1w1 and a height of ℎ1h1, while the second rectangle has a width of ?2w2 and a height of ℎ2h2, where ?1≥?2w1≥w2. In this game, exactly one ship is used, made up of two rectangles. There are no other ships on the field.

The rectangles are placed on field in the following way:

  • the second rectangle is on top the first rectangle;
  • they are aligned to the left, i.e. their left sides are on the same line;
  • the rectangles are adjacent to each other without a gap.

See the pictures in the notes: the first rectangle is colored red, the second rectangle is colored blue.

Formally, let's introduce a coordinate system. Then, the leftmost bottom cell of the first rectangle has coordinates (1,1)(1,1), the rightmost top cell of the first rectangle has coordinates (?1,ℎ1)(w1,h1), the leftmost bottom cell of the second rectangle has coordinates (1,ℎ1+1)(1,h1+1) and the rightmost top cell of the second rectangle has coordinates (?2,ℎ1+ℎ2)(w2,h1+h2).

After the ship is completely destroyed, all cells neighboring by side or a corner with the ship are marked. Of course, only cells, which don't belong to the ship are marked. On the pictures in the notes such cells are colored green.

Find out how many cells should be marked after the ship is destroyed. The field of the game is infinite in any direction.

Input

Four lines contain integers ?1,ℎ1,?2w1,h1,w2 and ℎ2h2 (1≤?1,ℎ1,?2,ℎ2≤1081≤w1,h1,w2,h2≤108, ?1≥?2w1≥w2) — the width of the first rectangle, the height of the first rectangle, the width of the second rectangle and the height of the second rectangle. You can't rotate the rectangles.

Output

Print exactly one integer — the number of cells, which should be marked after the ship is destroyed.

Examples

input

Copy

2 1 2 1

output

Copy

12

input

Copy

2 2 1 2

output

Copy

16

Note

In the first example the field looks as follows (the first rectangle is red, the second rectangle is blue, green shows the marked squares):

In the second example the field looks as:

 

题意:w1 h1 代表第一个长方形的宽和高,w2 h2 代表第二个长方形的宽和高,都是有单位长度为1的小正方形组成的

每一个小正方形都会影响到周围正方形,然后求影响的正方形的数目

思路:

首先呢这两个长方形的最左边是垂直状态的,会产生 h1+h2 + 2 个影响数

其次最下底和最上底的宽各回增加 w1+1   w2+1 个影响数

最右边产生 h1 + h2 

关键是拐角处的:  产生拐角处的肯定是上宽与下宽的差值数目(画图简单明了)

 

CODE:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
typedef long long LL;
using namespace std;
//#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f

int main()
{
    int w1,h1,w2,h2;
    int sum=0;
    cin>>w1>>h1>>w2>>h2;
    sum=h1+h2+2;
    sum+=w1+w2+2;
    sum+=h1+h2;
    sum+=abs(w1-w2);
    cout<<sum<<endl;
}

 

 

2、Be Positive

题目来源

题意:

给n个数,然后让这n个数去除d,求得到的商是正数(不包括0) 的个数,要求个数至少满足  x=  \left \lceil \frac{n}{2} \right \rceil 个(n/2向上取整)

思路:

首先判断如果非零数的个数小于x个,不可能出现x个正数,输出0

其次:

如果正数的个数大于x个,输出 “1”(除1还是等于其本身,也是正数,肯定满足题意)

如果负数的个数大于x个,输出 ”-1”,(由负数变成正数,个数满足)

都不满足就输出 0

 

CODE:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
typedef long long LL;
using namespace std;
//#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f

int main()
{
    int n,a[105];
    int p=0,q=0,r=0;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        if(a[i]<0)
            p++;
        else if(a[i]==0)
            q++;
        else
            r++;
    }

    int temp=n/2;
    if(n%2!=0)
        temp++;

    int num;
    num=n-q;
    if(num<temp){
        cout<<"0"<<endl;
        return 0;
    }

    if(r>=temp){
        cout<<"1"<<endl;
        return 0;
    }

    if(p>=temp){
        cout<<"-1"<<endl;
        return 0;
    }

    cout<<"0"<<endl;



}

 

3、Technogoblet of Fire

 

Everybody knows that the ?m-coder Tournament will happen soon. ?m schools participate in the tournament, and only one student from each school participates.

There are a total of ?n students in those schools. Before the tournament, all students put their names and the names of their schools into the Technogoblet of Fire. After that, Technogoblet selects the strongest student from each school to participate.

Arkady is a hacker who wants to have ?k Chosen Ones selected by the Technogoblet. Unfortunately, not all of them are the strongest in their schools, but Arkady can make up some new school names and replace some names from Technogoblet with those. You can't use each made-up name more than once. In that case, Technogoblet would select the strongest student in those made-up schools too.

You know the power of each student and schools they study in. Calculate the minimal number of schools Arkady has to make up so that ?kChosen Ones would be selected by the Technogoblet.

Input

The first line contains three integers ?n, ?m and ?k (1≤?≤1001≤n≤100, 1≤?,?≤?1≤m,k≤n) — the total number of students, the number of schools and the number of the Chosen Ones.

The second line contains ?n different integers ?1,?2,…,??p1,p2,…,pn (1≤??≤?1≤pi≤n), where ??pi denotes the power of ?i-th student. The bigger the power, the stronger the student.

The third line contains ?n integers ?1,?2,…,??s1,s2,…,sn (1≤??≤?1≤si≤m), where ??si denotes the school the ?i-th student goes to. At least one student studies in each of the schools.

The fourth line contains ?k different integers ?1,?2,…,??c1,c2,…,ck (1≤??≤?1≤ci≤n)  — the id's of the Chosen Ones.

Output

Output a single integer  — the minimal number of schools to be made up by Arkady so that ?k Chosen Ones would be selected by the Technogoblet.

Examples

input

7 3 1
1 5 3 4 6 7 2
1 3 1 2 1 2 3
3

output

1

input

8 4 4
1 2 3 4 5 6 7 8
4 3 2 1 4 3 2 1
3 4 5 6

output

2

 

题意:

每一个id号(1<= id <= n )对应一个学生的体重,和该学生所在的学校编号,一个学校里有多个学生

指定 k 个学生的编号,需要满足这个学生是他所在学校里最强壮的一个

如果不满足,则新开辟一个新学校编号,将该学生放入新的学校里,求需要开辟几个新的学校?

 

比如:

8 4 4
1 2 3 4 5 6 7 8
4 3 2 1 4 3 2 1
3 4 5 6
 

输出 2


指定的学生编号是 3、4、5、6,他所在的学校分别是 2 1 4 3

id = 3时,第二所学校里最强壮的是 7 号,所以对3号新开辟一个学校

id = 4时,第一所学校里最强壮的是 8号,所以对4号新开辟一个

id = 5,6时,对应的体重都是该学校最重的,不用新增加

 

思路:

找到该编号对应的体重 v 和学校编号  -->  去找该学校编号对应的所有体重  -->  去判断该体重是不是这个学校里最强壮的,如果不是,就新增一个学校,把该学生放入新的学校里边

CODE:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
typedef long long LL;
using namespace std;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f

int a[105],b[105];
int n,ans;
int Judge(int pos,int temp)
{
    for(int i=1;i<=n;i++){
        if(b[i]==pos){
            if(a[i]>temp){
                ans++;
                break;
            }
        }
    }

}

int main()
{
    int m,k;
    cin>>n>>m>>k;
    memset(a,0);
    memset(b,0);
    for(int i=1;i<=n;i++)
        cin>>a[i];

    for(int i=1;i<=n;i++)
        cin>>b[i];

    int pos;
    ans=0;
    for(int i=0;i<k;i++){
        scanf("%d",&pos);
        int temp=a[pos];
        int temp1=b[pos];
        Judge(temp1,temp);
    }
    cout<<ans<<endl;
}

 

 

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值