Codeforces_AIM Tech Round 3 (Div. 1)_B

http://codeforces.com/problemset/problem/711/B

比较简单,过程有点繁琐,先找一行包含那个0的行,得到和,以此填出0位置的值,然后判断这个矩阵是否符合条件。

要注意的是,n=1的情况,数据超了int,结果不为负。

#include<iostream>
#include<cstdio>
using namespace std;
long long a[505][505];
int main()
{
    int n,x,y;
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= n;j++)
        {
            cin >> a[i][j];
            if(!a[i][j])
            {
                x = i;
                y = j;
            }
        }
    }
    if(n == 1)
    {
        printf("1\n");
        return 0;
    }
    long long sum = 0;
    for(int i = 1;i <= n;i++)
    {
        if(i != x)
        {
            for(int j = 1;j <= n;j++)
            {
                sum += a[i][j];
            }
            break;
        }
    }
    long long now = 0;
    for(int i = 1;i <= n;i++)
    {
        now += a[x][i];
    }
    a[x][y] = sum-now;
    for(int i = 1;i <= n;i++)
    {
        long long temp1 = 0,temp2 = 0;
        for(int j = 1;j <= n;j++)
        {
            temp1 += a[i][j];
            temp2 += a[j][i];
        }
        if(temp1 != sum || temp2 != sum)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    long long temp1 = 0,temp2 = 0;
    for(int i = 1;i <=n;i++)
    {
        temp1 += a[i][i];
        temp2 += a[i][n-i+1];
    }
    if(temp1 != sum || temp2 != sum)
    {
        cout << -1 << endl;
        return 0;
    }
    if(a[x][y] <= 0)    cout << -1 << endl;
    else                cout << a[x][y] << endl;
    return 0;
}
时间: 2024-08-24 06:38:00

Codeforces_AIM Tech Round 3 (Div. 1)_B的相关文章

Codeforces_AIM Tech Round 3 (Div. 1)_A

http://codeforces.com/problemset/problem/708/A 贪心,先把前面连续的一串'a'排除,再向后知道找到第一个'a',注意特殊情况. #include<iostream> #include<cstdio> #include<string> using namespace std; int main() { string s; cin >> s; int i; for(i = 0;i < s.size() &

AIM Tech Round 3 (Div. 2) B

Description Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x1, x2, ..., xn. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finis

AIM Tech Round 3 (Div. 2) E. Centroids

题解: 树形dp 非常好的一道题目 题意: 对于每个点.更改一条边,能否使得这个点成为树的重心 题解: 所谓重心:指去掉这个点后,最大的连通分量的点数<=n/2 对于每个点,分为向下分析,向上分析 向下分析:找寻点u的子节点的最大节点v.然后找寻节点v的子节点的小于等于n/2的最大子节点,连接到u上 向上分析:找寻点u的父节点的最大节点v.如果v==u那么.找寻次大节点w.然后找寻该点的子节点的小于等于n/2的最大子节点,连接到u上 向下分析和向上分析只需要判断一个,因为大于n/2的点只有一个

AIM Tech Round 4 (Div. 2)

A题 分析:暴力 1 #include "iostream" 2 #include "cstdio" 3 #include "cstring" 4 #include "string" 5 using namespace std; 6 const int maxn=100+10; 7 int vis[maxn],n; 8 string s; 9 int main() 10 { 11 cin>>s; 12 cin>

[卿学姐带飞系列]-Codeforces Round #410 (Div. 2)_B - Mike and strings

1 #include<bits/stdc++.h> 2 #define inf 0x3f3f3f3f 3 using namespace std; 4 const int maxn=55; 5 string s[maxn]; 6 int main() 7 { 8 int n; 9 cin>>n; 10 for(int i=0;i<n;i++){ 11 cin>>s[i]; 12 } 13 int ans=inf,tem; 14 for(int i=0;i<s

AIM Tech Round 3 (Div. 2)

5/5 这一场是比较水的一场(当然我是指div2),所以前面三题就略过吧... 题D D. Recover the String 题意:让你构造一个01串,给你00,01,10,11的子序列个数,问你有没有满足的串. 题解:这题实际上并不难, 只是分类讨论有点麻烦. 首先是00和11一定是满足n * (n - 1)  / 2,先判定一下 然后得到0的个数x,1的个数y 然后我们注意到,现一开始所有的0放在一起,然后插入一个位置k,那么我们发现01多了k,10多了x – k:假设这y个1 的位置是

AIM Tech Round 4 (Div. 2) A B C

A. Diversity 题意:给出一个字符串,和n,问最多改变多少个字符,使其不同字符最少为n 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=1e5+10; 5 6 map<char ,int >ma; 7 int main(){ 8 string s; 9 cin>>s; 10 int n; 11 cin>>n; 12 int

AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)

A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Calculate the minimum number of characters you need to change in the string s, so that it contains at least k different letters,

【AIM Tech Round 4 (Div. 2) D Prob】

·题目:D. Interactive LowerBound ·英文题,述大意:       有一个长度为n(n<=50000)的单链表,里面的元素是递增的.链表存储在一个数组里面,给出长度n.表头在数组的下标和一个值w.题目要求求出链表中大于等于w值的元素中的最小元素.注意,这道题是一道interactive.由于链表是未知的,最多可以进行1999个询问,询问形式:?i.表示询问数组下标为i,询问后,会得到一个答案组(val,Next),表示询问的元素是val,链表下一位所在的数组下标是Next