HackerRank - organizing-containers-of-balls

David has  containers and  different types of balls, both of which are numbered from  to . The distribution of ball types per container are described by an  matrix of integers, , where each  is the number of balls of type  in container . For example, consider the following diagram for :

image

In a single operation, David can swap two balls located in different containers (i.e., one ball is moved from container  to  and the other ball is moved from to ).

For example, the diagram below depicts a single swap operation:

image

David wants to perform some number of swap operations such that both of the following conditions are satisfied:

  • Each container contains only balls of the same type.
  • No two balls of the same type are located in different containers.

You must perform  queries where each query is in the form of a matrix, . For each query, print Possible on a new line if David can satisfy the conditions above for the given matrix; otherwise, print Impossible instead.

Input Format

The first line contains an integer denoting  (the number of queries). The subsequent lines describe each query in the following format:

  1. The first line contains an integer denoting  (the number of containers and ball types).
  2. Each line  of the  subsequent lines contains  space-separated integers describing row  in matrix .

Constraints

Scoring

  • For  of score, .
  • For  of score, .

Output Format

For each query, print Possible on a new line if David can satisfy the conditions above for the given matrix; otherwise, print Impossible instead.

Sample Input 0

2
2
1 1
1 1
2
0 2
1 1

Sample Output 0

Possible
Impossible

Explanation 0

We perform the following  queries:

  1. The diagram below depicts one possible way to satisfy David's requirements for the first query: image 
    Thus, we print Possible on a new line.
  2. The diagram below depicts the matrix for the second query: image 
    No matter how many times we swap balls of type  and  between the two containers, we'll never end up with one container only containing type  and the other container only containing type . Thus, we print Impossible on a new line.


这个题一开始不知道怎么弄得,意思半路当成了种类1的球只能待在容器1里面,导致一直wa.....QAQ。

当然意思是只要每种种类的球全待在一个容器里,而且每个容器只有一种种类的球就可以了。

先说一下比较重要的一点就是能不能交换成功,这个其实从大面上想就很简单了,比如说种类1的球有n个,那么至少有一个容器里面有n个球,才能完成交换,不必管它怎么交换的,因为如果你没有一个容器有n个球,那么纵然不可能成功变成题目要求的那样。而只要都能找到相对应的,那么最后必定存在能交换成功的方法。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
using namespace std;
int n,m,q;
const int MAXN=1e5+7;
const int mod=1e9+7;
long long ball[110];
long long tong[110];
long long tu[110][110];
int main()
{
    int i,j;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;++i)
            for(j=0;j<n;++j)scanf("%I64d",&tu[i][j]);
            memset(tong,0,sizeof(tong));
            memset(ball,0,sizeof(ball));
        for(i=0;i<n;++i)
            for(j=0;j<n;++j)
        {
            tong[i]+=tu[i][j];
            ball[j]+=tu[i][j];
        }
        int flag=1;
        sort(tong,tong+n);
        sort(ball,ball+n);
        for(i=0;i<n;++i)
        {
            if(ball[i]!=tong[i])
            {
                flag=0;
                break;
            }
        }
        if(flag)puts("Possible");
        else puts("Impossible");
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值