目录
1120 Friend Numbers (20分) (水题)
1121 Damn Single (25分) (水题)
1122 Hamiltonian Cycle (25分)
1124 Raffle for Weibo Followers (20分)
1125 Chain the Ropes (25分) (贪心)
1126 Eulerian Path (25分)(欧拉路-连通图)
1128 N Queens Puzzle (20分)
1129 Recommendation System (25分) (set,运算符重载)
#include
#include
#include
#include
#include
#include
#include
#include
#include
思路:unordered_map
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=100005;
int n,m,cnt;
unordered_map mp;
unordered_set st;
vector ans;
int main(){
cin>>n;
int a,b;
for(int i=0;i>a>>b;
mp[a]=b; mp[b]=a;
}
cin>>m;
for(int i=0;i>a;
st.insert(a);
}
int couples;
for(int x: st){
if(mp.find(x)!=mp.end()){
couples=mp[x];
if(st.find(couples)!=st.end()){
cnt++;
}else{
ans.pb(x);
}
}else{
ans.pb(x);
}
}
cnt=m-cnt;
sort(ans.begin(),ans.end());
cout<
哈密顿环路(无向图:经过所有点(一次且仅一次)的环路)
判断依据:
01:回路路径上有n+1个点,
02:回路的起点=终点
03:回路上(除起点终点外)点不重复
04:路径上每条边都存在
#include
#include
#include
#include
using namespace std;
int main() {
int n, m, cnt, k, a[210][210] = {0};
cin >> n >> m;
for(int i = 0; i < m; i++) {
int t1, t2;
cin>>t1>>t2;
a[t1][t2] = a[t2][t1] = 1;
}
cin >> cnt;
while(cnt--) {
cin >> k;
vector v(k);
set s;
int flag1 = 1, flag2 = 1;
for(int i = 0; i < k; i++) {
cin>>v[i];
s.insert(v[i]);
}
if(s.size() != n || k - 1 != n || v[0] != v[k-1]) flag1 = 0;
for(int i = 0; i < k - 1; i++)
if(a[v[i]][v[i+1]] == 0) flag2 = 0;
printf("%s",flag1 && flag2 ? "YES\n" : "NO\n");
}
return 0;
}
John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers on Weibo -- that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are supposed to help him generate the list of winners.
题⽬⼤意:⼩明PAT考了满分,⾼兴之余决定发起微博转发抽奖活动,从转发的⽹友中按顺序每隔N个⼈就发出⼀个红包。请你编写程序帮助他确定中奖名单。注意:可能有⼈转发多次,但不能中奖多次。所以如果处于当前中奖位置的⽹友已经中过奖,则跳过他顺次取下⼀位。按照输⼊的顺序输出中奖名单,每个昵称占⼀⾏。如果没有⼈中奖,则输出“Keep going…”
分析:⽤mapp存储当前⽤户有没有已经中奖过~当输⼊的时候,判断当前字符串是否已经在mapp中出现过,如果出现过就将s+1。每次判断i是否等于s,如果等于s且当前⽤户没有中过奖,就将它的名字输出,并且s = s + n~并将mapp[str]标记为1,且flag标记为true表示有过⼈中奖。最后flag如果依然是false说明要输出Keep going
#include
#include
#include
#include
#include
#include
Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fold(折叠) two segments into loops and chain them into one piece, as shown by the figure. The resulting chain will be treated as another segment of rope and can be folded again. After each chaining, the lengths of the original two segments will be halved.
Your job is to make the longest possible rope(绳索) out of N given segments.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (2≤N≤10^4). Then N positive integer lengths of the segments are given in the next line, separated by spaces. All the integers are no more than 104.
Output Specification:
For each case, print in a line the length of the longest possible rope that can be made by the given segments. The result must be rounded to the nearest integer that is no greater than the maximum length.
Sample Input:
8
10 15 12 3 4 13 1 15
Sample Output:
14
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=100005;
int n;
int num[10005];
int main(){
scanf("%d",&n);
for(int i=0;i
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=100005;
int n,m,cnt=0;
int vis[505];
vector g[505];
int degree[505]; //顶点度
void dfs(int x){
cnt++;
vis[x]=1;
for(int i=0;i>n>>m;
int a,b;
for(int i=0;i>a>>b;
g[a].pb(b); g[b].pb(a);
}
dfs(1);
int x=check();
for(int i=1;i<=n;i++){
if(i!=1) cout<<' ';
cout<
Here you are NOT asked to solve the puzzles. Instead, you are supposed to judge whether or not a given configuration of the chessboard is a solution. To simplify the representation of a chessboard, let us assume that no two queens will be placed in the same column. Then a configuration can be represented by a simple integer sequence (Q1,Q2,⋯,QN), where Qi is the row number of the queen in the i-th column. For example, Figure 1 can be represented by (4, 6, 8, 2, 7, 1, 3, 5) and it is indeed a solution to the 8 queens puzzle; while Figure 2 can be represented by (4, 6, 7, 2, 8, 1, 9, 5, 3) and is NOT a 9 queens' solution.
题意:给出⼀个皇后图,以这样的⽅式给出:⼀个数组包含n个数字,每个数字表示该列的皇后所在的⾏数~判断给出的皇后图是否满⾜不会互相攻击(任意两个皇后都要不在同⼀⾏或者同⼀列,且不在斜对⻆线上~
判断在斜对角线上 :abs(num[j]-num[t]) == abs(j-t)
#include
#include
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=100005;
int n,m,tmp,Hash[1005];
vector num(1005);
bool check(int m){
//检查斜对角线上可不可以相互攻击
//和在斜对角线上的(abs(v[j]-v[t]) == abs(j-t))
for(int i=1;i<=m;i++){
for(int j=i+1;j<=m;j++){
if(abs(num[i]-num[j])==abs(i-j))
return false;
}
}
return true;
}
int main(){
cin>>n;
int flag;
while(n--){
cin>>m;
flag=0;
num.clear();
memset(Hash,0,sizeof(Hash));
for(int i=1;i<=m;i++){
cin>>tmp;
num[i]=tmp;
if(Hash[tmp]==0){
Hash[tmp]++;
}else{
flag=1;
}
}
if(flag==0 && check(m)) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
set 容器模版需要3个泛型参数,如下:
template
第一个T 是元素类型,必选;
第二个C 指定元素比较方式,缺省为 Less, 即使用 < 符号比较;
第三个A 指定空间分配对象,一般使用默认类型。
因此:
(1) 如果第2个泛型参数你使用默认值的话,你的自定义元素类型需要重载 < 运算操作;
(2)如果你第2个泛型参数不使用默认值的话,则比较对象必须具有 () 操作,即:
bool operator()(const T &a, const T &b)
#include
#include
#include
#include
#include
#include
#include
#define pb push_back
using namespace std;
const int maxn=100005;
int n,k,fre[50005];
struct node{
int value,freq;
node(int v,int f){
value=v; freq=f;
}
bool operator < (const node &a) const {
return (freq != a.freq) ? freq > a.freq : value < a.value;
}
};
int main(){
scanf("%d%d",&n,&k);
set st;
int tmp,cnt;
for(int i=0;ivalue);
cnt++;
}
printf("\n");
}
node t=node(tmp,fre[tmp]);
fre[tmp]++;
if(st.find(t)!=st.end())
st.erase(t);
st.insert(node(tmp,fre[tmp]));
}
return 0;
}
或者:
node *t=new node(tmp,fre[tmp]);
fre[tmp]++;
if(st.find(*t)!=st.end())
st.erase(*t);
st.insert(node(tmp,fre[tmp]));