How Many Answers Are Wrong
HDU - 3038
题面:
1 TT and FF are ... friends. Uh... very very good friends -________-b 2 3 FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored). 4 5 Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers. 6 7 Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose. 8 9 The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence. 10 11 However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed. 12 13 What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers. 14 15 But there will be so many questions that poor FF can't make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy) 16 Input 17 Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions. 18 19 Line 2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It's guaranteed that 0 < Ai <= Bi <= N. 20 21 You can assume that any sum of subsequence is fit in 32-bit integer. 22 23 Output 24 A single line with a integer denotes how many answers are wrong. 25 26 Sample Input 27 10 5 28 1 10 100 29 7 10 28 30 1 3 32 31 4 6 41 32 6 6 1 33 Sample Output 34 1
题意:
判断每个区间是否和之前的区间冲突,是的话ans++
注意:
多组输入,
dis需要清空
记录权值
int getf(int x) { if(f[x]==x) return x; int t=getf(f[x]); dis[x]+=dis[f[x]]; //当前点到父节点距离加上父节点到根节点距离就是当前点到根节点距离 return f[x]=t; // dis[x]+=dis[f[x]]; // return f[x]=getf(f[x]); WA }
1 #include<string.h> 2 #include3 #include 4 #include 5 #include 6 #include 7 #include
待解决:
针对上述题目:
a--不执行的话会wa
我的理解,避免端点重合不起来,左开右闭
但是对于这组数据又解释不通(a--后,我觉得是1,但是答案是0,不存在冲突)
10 3 1 10 10 1 4 2 4 8 10
POJ1611-The Suspects
题面:
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP). Once a member in a group is a suspect, all members in the group are suspects. However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects. Input The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space. A case with n = 0 and m = 0 indicates the end of the input, and need not be processed. Output For each case, output the number of suspects in one line. Sample Input 100 4 2 1 2 5 10 13 11 12 14 2 0 1 2 99 2 200 2 1 5 5 1 2 3 4 5 1 0 0 0 Sample Output 4 1 1
题意:
求和0有关的数字有多少个?(包括0)
思路:
求解同一祖先下有多少个节点。(包括祖先)
解法一:
关键代码:
void merge(int x,int y) { int t1,t2; t1=getf(x); t2=getf(y); if(t1!=t2) { f[t2]=t1; num[t1]=num[t1]+num[t2]; } return ; }
AC代码:
#include#include<string.h> #include #include #include #define N 31000 using namespace std; int f[N],num[N],dis[N],m,n; void init() { for(int i=0; i ) { f[i]=i; num[i]=1; } return ; } int getf(int x) { if(f[x]==x) return x; return f[x]=getf(f[x]); } void merge(int x,int y) { int t1,t2; t1=getf(x); t2=getf(y); if(t1!=t2) { f[t2]=t1; num[t1]=num[t1]+num[t2]; } return ; } //int dis[510][N]; int main() { while(~scanf("%d %d",&n,&m)&&(m+n)) { //??? ,n||m init(); if(m==0) { printf("1\n"); continue; } int i,j,k,a=0,b; while(m--) // for(j=0;i { // memset(dis,0,sizeof(dis)); scanf("%d",&b); for(i=0; i) // scanf("%d",&dis[a++][i]); scanf("%d",&dis[i]); for(i=0; i1; i++) //注意一下 merge(dis[i],dis[i+1]); } // int p,q=getf(0),sum=0; // for(i=0; i // { // p=getf(i); // if(p==q) // sum++; // } printf("%d\n",num[getf(0)]); // printf("%d\n",sum); } return 0; }
解法二:
AC代码:
1 #include2 #include 3 #include<string.h> 4 using namespace std; 5 #define inf 0x3f3f3f3f 6 7 int f[30020],a[30020],book[30020]; 8 9 int getf(int x) 10 { 11 if(f[x]==x) 12 return x; 13 return f[x]=getf(f[x]); 14 } 15 16 void merge(int x,int y) 17 { 18 int t1=getf(x); 19 int t2=getf(y); 20 if(t1!=t2) 21 f[t2]=t1; 22 } 23 24 int main() 25 { 26 int n,m; 27 while(cin>>n>>m) 28 { 29 if(n==0&&m==0) 30 break; 31 if(m==0) 32 { 33 cout<<1<<endl; 34 continue; 35 } 36 memset(book,0,sizeof(book)); 37 for(int i=0; i ) 38 f[i]=i; 39 for(int i=1; i<=m; i++) 40 { 41 int k; 42 cin>>k; 43 for(int j=1; j<=k; j++) 44 cin>>a[j],book[a[j]]=1; 45 for(int j=2; j<=k; j++) 46 merge(a[1],a[j]); 47 } 48 int ans=0; 49 book[0]=1; 50 for(int i=0; i ) 51 { 52 if(book[i]) 53 { 54 int t1=getf(i); 55 int t2=getf(0); 56 if(t1==t2) 57 ans++; 58 } 59 } 60 cout< endl; 61 } 62 return 0; 63 }
POJ1182-食物链
题目链接:POJ - 1182
思路:
把A看成1-N,B看成N+1~2N,C看成2N+1~3N
AC代码:
#include#include #include<string.h> using namespace std; #define inf 0x3f3f3f3f int f[150020]; int getf(int x) { if(f[x]==x) return x; else return f[x]=getf(f[x]); } void merge(int x,int y) { int t1=getf(x); int t2=getf(y); if(t1!=t2) f[t2]=t1; } int main() { int n,k; scanf("%d %d",&n,&k); int ans=0; for(int i=1; i<=3*n; i++) f[i]=i; for(int i=1; i<=k; i++) { int d,x,y; scanf("%d %d %d",&d,&x,&y); if(x>n||x<1||y<1||y>n) { ans++; continue; } int t1=getf(x); int t2=getf(y); int t3=getf(x+n); int t4=getf(y+n); int t5=getf(x+2*n); int t6=getf(y+2*n); if(d==1) { // if(t1==t4||t3==t6||t5==t2) if(t1==t4||t1==t6) ans++; else { merge(x,y); merge(x+n,y+n); merge(x+2*n,y+2*n); } } else { // if(t1==t2||t3==t4||t5==t6) if(t1==t2||t1==t6) ans++; else { merge(x,y+n); merge(x+n,y+2*n); merge(x+2*n,y); } } } printf("%d\n",ans); return 0; }
Thinking
所有的代码都会在原有基础上越写越简单,越写越精炼,越写越快,
三月份所有考试去小区,四月考位锁定,五月换题,只能安排在五月~
不知道接下来还会发生什么事情~
加油!!!