2016ccpc杭州赛 hdu 5934 K.Kingdom of Obsession

本文探讨了一个关于整数编号人员按特定条件排列的问题,通过分析给出了一种解决方案,利用二分图匹配理论来判断是否能按照每个人希望的位置进行排列。


Kingdom of Obsession

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
There is a kindom of obsession, so people in this kingdom do things very strictly.

They name themselves in integer, and there are  n  people with their id continuous  (s+1,s+2,,s+n)  standing in a line in arbitrary order, be more obsessively, people with id  x  wants to stand at  yth  position which satisfy

xmody=0


Is there any way to satisfy everyone's requirement?
 

Input
First line contains an integer  T , which indicates the number of test cases.

Every test case contains one line with two integers  n s .

Limits
1T100 .
1n109 .
0s109 .
 

Output
For every test case, you should output  'Case #x: y', where  x indicates the case number and counts from  1 and  y is the result string.

If there is any way to satisfy everyone's requirement,  y equals  'Yes', otherwise  y equals  'No'.
 

Sample Input
  
2 5 14 4 11
 

Sample Output
  
Case #1: No Case #2: Yes
 

Statistic |  Submit |  Clarifications |  Back

怎么感觉这次杭州是图论专场,居然两题涉及图论。

题意:n个人,id序号分别为是s+1到s+n,他们各自想站在第i个位置(id%i==0),问全部人能否站在想要的位置上。

思路:一看就是二分图,但数据很大不可能一一建边,然后想到了只要id里存在两个素数就不可能匹配成功,然后大概估算一下每1000个肯定有两个素数吧。然后就是暴力n^2建边,跑一遍二分图就好了。这里有个问题,区间重叠,如果重叠的话就算存在两个素数也是可以匹配了,因为可以一一对应。所以这里要特殊处理一下,区间重叠的部分默认匹配成功,剩下的部分跑二分图就好了,一开始这里没想到,一直wa。。下面给代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<utility>
#include<map>
#define maxn 2005
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;
const int mod = 1e9 + 7;
int head[maxn], vis[maxn];
bool book[maxn];
struct node{
	int v, next;
}p[maxn*maxn];
bool find(int x)
{
	for (int i = head[x]; ~i; i = p[i].next)
	{
		int y = p[i].v;
		if (!book[y])//防止同一个人被搜两次 ,不然会死循环 
		{
			book[y] = true;
			if (!vis[y] || find(vis[y]))//没人占用或者占用你的人可以找到新欢,太好了 
			{
				vis[y] = x;
				return true;
			}
		}
	}
	return false;
}
int main(){
	int t;
	scanf("%d", &t);
	for (int tcase = 1; tcase <= t; tcase++){
		memset(head, -1, sizeof(head));
		memset(vis, 0, sizeof(vis));
		int n, s, len = 0;
		scanf("%d%d", &n, &s);
		if (s + 1 <= n)
			swap(n, s);
		if (n > 1000)
			printf("Case #%d: No\n", tcase);
		else{
			for (int i = s + 1; i <= s + n; i++){
				for (int j = 1; j <= n; j++){
					if (i%j)
						continue;
					int u = i - s + 1000;
					p[len].v = j;
					p[len].next = head[u];
					head[u] = len++;
					p[len].v = u;
					p[len].next = head[j];
					head[j] = len++;
				}
			}
			int ans = 0;
			for (int i = 1; i <= n; i++)
			{
				memset(book, false, sizeof(book));//用来标记配偶是否被搜过一次 
				if (find(i))ans++;//找到配偶啦!好开心。对数++ 
			}
			if (ans == n)
				printf("Case #%d: Yes\n", tcase);
			else
				printf("Case #%d: No\n", tcase);
		}
	}
}


CCPC女生程序设计竞即中国大学生程序设计竞女生专场,不同年份的CPCP女生有不同的题目及解题思路。 2021年的CCPC女生有公交路线题,解题代码通过输入公交路线相关信息,根据不同条件判断输出结果,当`x < y`和`x >= y`时分别进行不同的判断逻辑,最终输出“Wrong”“Right”或“Unsure”[^3]。 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, x, y, arr[15]={0}; cin >> n >> x >> y; for (int i = 1; i <= n; i++) { cin >> arr[i]; } int m, xx, brr[15]={0}; cin >> m; if(x<y){ for(int i=1;i<=m;i++){ cin>>brr[i]; if(brr[i]!=arr[i+x]){ cout<<"Wrong"<<endl; return 0; } } if(x-1<1||x-m<1){ cout<<"Right"<<endl; return 0; } for(int i=1;i<=m;i++){ if(arr[x-i]!=brr[i]){ cout<<"Right"<<endl; return 0; } } cout<<"Unsure"<<endl; return 0; }else{ for(int i=1;i<=m;i++){ cin>>brr[i]; if(brr[i]!=arr[x-i]){ cout<<"Wrong"<<endl; return 0; } } if(x==n||x+m>n){ cout<<"Right"<<endl; return 0; } for(int i=1;i<=m;i++){ if(arr[x+i]!=brr[i]){ cout<<"Wrong"<<endl; return 0; } } cout<<"Unsure"<<endl; } return 0; } ``` 2023年的CCPC女生,有一道题的解题思路是对样例进行模拟,从最小数开始排,在数组中碰到前后相等的数就从后往前排,当数组中所给的数比它前边的数小,就判断为“ -1”。也可当成一个思维题,从最小的递增子序列长度从一开始排列,碰到一样长度的就从后往前排列,当递增序列断档不连续时该序列不合法[^2][^4]。 ```cpp #include "vector" using namespace std; const int N = 1e7; vector<int > v[N]; int a[N], b[N]; int main(){ int n; ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); int max = 0, flag = 0; cin>>n; for(int i = 1; i <= n; i++){ cin>>a[i]; if(a[i] > max) { if(a[i] - max > 1) flag = 1; max = a[i]; } v[a[i]].push_back(i); } if(flag) cout<<"-1"<<endl; else { int p = 1; for(int i = 1; i <= max; i++) for(int j = v[i].size() - 1; j >= 0; j --) { int t = v[i][j]; b[t] = p ++; } for(int i = 1; i <= n; i++) cout<<b[i]<<' '; cout<<endl; } } ``` 2024年的CCPC女生,有题目解题思路是观察到最优解是尽可能高效率地使用`C`与`P`,每个`CCPC`固定一个`P`,另外的`C`看公共前后缀,通过模拟实现,代码通过统计字符串中`C`和`P`的数量,输出`P`的数量和`(C的数量 - 1) / 2`中的较小值[^1]。 ```cpp string s; cin >> s; map<char,int> mp; for(int i = 0;i<sz(s);i++)mp[s[i]]++; cout << min(mp['P'] , (mp['C'] - 1) / 2) <<endl; return 0; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值