狡猾的商人 HYSBZ - 1202 带权并查集

刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的。账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3…n-1,n), 。当 Ai大于0时表示这个月盈利Ai 元,当 Ai小于0时表示这个月亏损Ai 元。所谓一段时间内的总收入,就是这段时间内每个月的收入额的总和。 刁姹的任务是秘密进行的,为了调查商人的账本,她只好跑到商人那里打工。她趁商人不在时去偷看账本,可是她无法将账本偷出来,每次偷看账本时她都只能看某段时间内账本上记录的收入情况,并且她只能记住这段时间内的总收入。 现在,刁姹总共偷看了m次账本,当然也就记住了m段时间内的总收入,你的任务是根据记住的这些信息来判断账本是不是假的。
Input

第一行为一个正整数w,其中w < 100,表示有w组数据,即w个账本,需要你判断。每组数据的第一行为两个正整数n和m,其中n < 100,m < 1000,分别表示对应的账本记录了多少个月的收入情况以及偷看了多少次账本。接下来的m行表示刁姹偷看m次账本后记住的m条信息,每条信息占一行,有三个整数s,t和v,表示从第s个月到第t个月(包含第t个月)的总收入为v,这里假设s总是小于等于t。
Output

包含w行,每行是true或false,其中第i行为true当且仅当第i组数据,即第i个账本不是假的;第i行为false当且仅当第i组数据,即第i个账本是假的。
Sample Input
2
3 3
1 2 10
1 3 -5
3 3 -15
5 3
1 5 100
3 5 50
1 2 51
Sample Output
true
false


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 1e9 + 7;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
const int N = 1505;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

int w;
int fa[maxn];
int v[200];
int tmp;
int n, m;
int flag;
int findfa(int x) {
	if (x == fa[x])return x;
	tmp = findfa(fa[x]);
	v[x] += v[fa[x]]; fa[x] = tmp;
	return fa[x];
}

void check(int x, int y, int w) {
	int p = findfa(x), q = findfa(y);
	if (p == q) {
		if (v[y] - v[x] != w)flag = 1;
	}
	else {
		fa[p] = q;
		v[p] = v[y] - v[x] - w;
	}
}

int main()
{
	//ios::sync_with_stdio(false);
	rdint(w);
	while (w--) {
		ms(v); flag = 0;
		rdint(n); rdint(m);
		for (int i = 0; i <= n; i++)fa[i] = i;
		while (m--) {
			int x, y, z; rdint(x); rdint(y); rdint(z);
			check(x - 1, y, z);
		}
		if (flag)cout << "false" << endl;
		else cout << "true" << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你可以参考以下示例代码实现Java销售额查询和营业额统计: ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class SalesStatistics { private Map<String, Double> salesData = new HashMap<>(); public SalesStatistics() { // 初始化销售数据 salesData.put("2021-01-01", 1000.0); salesData.put("2021-01-02", 1500.0); salesData.put("2021-01-03", 2000.0); salesData.put("2021-01-04", 1200.0); salesData.put("2021-01-05", 1800.0); } // 查询指定日期的销售额 public double getSalesByDate(String date) { if (salesData.containsKey(date)) { return salesData.get(date); } else { return 0.0; } } // 统计指定日期范围内的营业额 public double getTotalSales(String startDate, String endDate) { double totalSales = 0.0; for (String date : salesData.keySet()) { if (date.compareTo(startDate) >= 0 && date.compareTo(endDate) <= 0) { totalSales += salesData.get(date); } } return totalSales; } // 获取所有销售日期 public List<String> getAllSalesDates() { return new ArrayList<>(salesData.keySet()); } } ``` 使用示例: ```java public static void main(String[] args) { SalesStatistics salesStatistics = new SalesStatistics(); // 查询指定日期的销售额 double sales = salesStatistics.getSalesByDate("2021-01-02"); System.out.println("2021-01-02的销售额为:" + sales); // 统计指定日期范围内的营业额 double totalSales = salesStatistics.getTotalSales("2021-01-02", "2021-01-04"); System.out.println("2021-01-02到2021-01-04的营业额为:" + totalSales); // 获取所有销售日期 List<String> salesDates = salesStatistics.getAllSalesDates(); System.out.println("所有销售日期为:" + salesDates); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值